VirtualBox

Changeset 48835 in vbox


Ignore:
Timestamp:
Oct 3, 2013 1:29:58 PM (11 years ago)
Author:
vboxsync
Message:

IPRT: Added RTTimeFromString and RTTimeSpecFromString for simple ISO-like date+time string conversion.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/iprt/mangling.h

    r47762 r48835  
    16801680# define RTTimerStop                                    RT_MANGLER(RTTimerStop)
    16811681# define RTTimeSet                                      RT_MANGLER(RTTimeSet)
     1682# define RTTimeSpecFromString                           RT_MANGLER(RTTimeSpecFromString)
    16821683# define RTTimeSpecToString                             RT_MANGLER(RTTimeSpecToString)
    16831684# define RTTimeSystemMilliTS                            RT_MANGLER(RTTimeSystemMilliTS)
    16841685# define RTTimeSystemNanoTS                             RT_MANGLER(RTTimeSystemNanoTS)
     1686# define RTTimeFromString                               RT_MANGLER(RTTimeFromString)
    16851687# define RTTimeToString                                 RT_MANGLER(RTTimeToString)
    16861688# define RTTlsAlloc                                     RT_MANGLER(RTTlsAlloc)
  • trunk/include/iprt/time.h

    r44528 r48835  
    524524 */
    525525RTDECL(char *) RTTimeSpecToString(PCRTTIMESPEC pTime, char *psz, size_t cb);
     526
     527/**
     528 * Attempts to convert an ISO date string to a time structure.
     529 *
     530 * We're a little forgiving with zero padding, unspecified parts, and leading
     531 * and trailing spaces.
     532 *
     533 * @retval  pTime on success,
     534 * @retval  NULL on failure.
     535 * @param   pTime       The time spec.
     536 * @param   pszString   The ISO date string to convert.
     537 */
     538RTDECL(PRTTIMESPEC) RTTimeSpecFromString(PRTTIMESPEC pTime, const char *pszString);
    526539
    527540/** @} */
     
    710723
    711724/**
     725 * Attempts to convert an ISO date string to a time structure.
     726 *
     727 * We're a little forgiving with zero padding, unspecified parts, and leading
     728 * and trailing spaces.
     729 *
     730 * @retval  pTime on success,
     731 * @retval  NULL on failure.
     732 * @param   pTime       Where to store the time on success.
     733 * @param   pszString   The ISO date string to convert.
     734 */
     735RTDECL(PRTTIME) RTTimeFromString(PRTTIME pTime, const char *pszString);
     736
     737/**
    712738 * Checks if a year is a leap year or not.
    713739 *
  • trunk/src/VBox/Runtime/common/time/time.cpp

    r44528 r48835  
    3333#include "internal/iprt.h"
    3434
     35#include <iprt/ctype.h>
    3536#include <iprt/string.h>
    3637#include <iprt/assert.h>
     
    744745RT_EXPORT_SYMBOL(RTTimeSpecToString);
    745746
     747
     748
     749/**
     750 * Attempts to convert an ISO date string to a time structure.
     751 *
     752 * We're a little forgiving with zero padding, unspecified parts, and leading
     753 * and trailing spaces.
     754 *
     755 * @retval  pTime on success,
     756 * @retval  NULL on failure.
     757 * @param   pTime       Where to store the time on success.
     758 * @param   pszString   The ISO date string to convert.
     759 */
     760RTDECL(PRTTIME) RTTimeFromString(PRTTIME pTime, const char *pszString)
     761{
     762    /* Ignore leading spaces. */
     763    while (RT_C_IS_SPACE(*pszString))
     764        pszString++;
     765
     766    /*
     767     * Init non date & time parts.
     768     */
     769    pTime->fFlags = RTTIME_FLAGS_TYPE_LOCAL;
     770    pTime->offUTC = 0;
     771
     772    /*
     773     * The day part.
     774     */
     775
     776    /* Year */
     777    int rc = RTStrToInt32Ex(pszString, (char **)&pszString, 10, &pTime->i32Year);
     778    if (rc != VWRN_TRAILING_CHARS)
     779        return NULL;
     780
     781    bool const fLeapYear = rtTimeIsLeapYear(pTime->i32Year);
     782    if (fLeapYear)
     783        pTime->fFlags |= RTTIME_FLAGS_LEAP_YEAR;
     784
     785    if (*pszString++ != '-')
     786        return NULL;
     787
     788    /* Month of the year. */
     789    rc = RTStrToUInt8Ex(pszString, (char **)&pszString, 10, &pTime->u8Month);
     790    if (rc != VWRN_TRAILING_CHARS)
     791        return NULL;
     792    if (pTime->u8Month == 0 || pTime->u8Month > 12)
     793        return NULL;
     794    if (*pszString++ != '-')
     795        return NULL;
     796
     797    /* Day of month.*/
     798    rc = RTStrToUInt8Ex(pszString, (char **)&pszString, 10, &pTime->u8MonthDay);
     799    if (rc != VWRN_TRAILING_CHARS && rc != VINF_SUCCESS)
     800        return NULL;
     801    unsigned const cDaysInMonth = fLeapYear
     802                                ? g_acDaysInMonthsLeap[pTime->u8Month - 1]
     803                                : g_acDaysInMonthsLeap[pTime->u8Month - 1];
     804    if (pTime->u8MonthDay == 0 || pTime->u8MonthDay > cDaysInMonth)
     805        return NULL;
     806
     807    /* Calculate year day. */
     808    pTime->u16YearDay = pTime->u8MonthDay - 1
     809                      + (fLeapYear
     810                         ? g_aiDayOfYearLeap[pTime->u8Month - 1]
     811                         : g_aiDayOfYear[pTime->u8Month - 1]);
     812
     813    /*
     814     * The time part.
     815     */
     816    if (*pszString++ != 'T')
     817        return NULL;
     818
     819    /* Hour. */
     820    rc = RTStrToUInt8Ex(pszString, (char **)&pszString, 10, &pTime->u8Hour);
     821    if (rc != VWRN_TRAILING_CHARS)
     822        return NULL;
     823    if (pTime->u8Hour > 23)
     824        return NULL;
     825    if (*pszString++ != ':')
     826        return NULL;
     827
     828    /* Minute. */
     829    rc = RTStrToUInt8Ex(pszString, (char **)&pszString, 10, &pTime->u8Minute);
     830    if (rc != VWRN_TRAILING_CHARS)
     831        return NULL;
     832    if (pTime->u8Minute > 59)
     833        return NULL;
     834    if (*pszString++ != ':')
     835        return NULL;
     836
     837    /* Second. */
     838    rc = RTStrToUInt8Ex(pszString, (char **)&pszString, 10, &pTime->u8Minute);
     839    if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS && rc != VWRN_TRAILING_SPACES)
     840        return NULL;
     841    if (pTime->u8Second > 59)
     842        return NULL;
     843
     844    /* Nanoseconds is optional and probably non-standard. */
     845    if (*pszString == '.')
     846    {
     847        rc = RTStrToUInt32Ex(pszString + 1, (char **)&pszString, 10, &pTime->u32Nanosecond);
     848        if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS && rc != VWRN_TRAILING_SPACES)
     849            return NULL;
     850        if (pTime->u32Nanosecond >= 1000000000)
     851            return NULL;
     852    }
     853    else
     854        pTime->u32Nanosecond = 0;
     855
     856    /*
     857     * Time zone.
     858     */
     859    if (*pszString == 'Z')
     860    {
     861        *pszString++;
     862        pTime->fFlags &= ~RTTIME_FLAGS_TYPE_MASK;
     863        pTime->fFlags |= ~RTTIME_FLAGS_TYPE_UTC;
     864        pTime->offUTC = 0;
     865    }
     866    else if (   *pszString == '+'
     867             || *pszString == '-')
     868    {
     869        rc = RTStrToInt32Ex(pszString, (char **)&pszString, 10, &pTime->offUTC);
     870        if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS && rc != VWRN_TRAILING_SPACES)
     871            return NULL;
     872    }
     873    /* else: No time zone given, local with offUTC = 0. */
     874
     875    /*
     876     * The rest of the string should be blanks.
     877     */
     878    char ch;
     879    while ((ch = *pszString++) != '\0')
     880        if (!RT_C_IS_BLANK(ch))
     881            return NULL;
     882
     883    return pTime;
     884}
     885RT_EXPORT_SYMBOL(RTTimeFromString);
     886
     887
     888/**
     889 * Attempts to convert an ISO date string to a time structure.
     890 *
     891 * We're a little forgiving with zero padding, unspecified parts, and leading
     892 * and trailing spaces.
     893 *
     894 * @retval  pTime on success,
     895 * @retval  NULL on failure.
     896 * @param   pTime       The time spec.
     897 * @param   pszString   The ISO date string to convert.
     898 */
     899RTDECL(PRTTIMESPEC) RTTimeSpecFromString(PRTTIMESPEC pTime, const char *pszString)
     900{
     901    RTTIME Time;
     902    if (RTTimeFromString(&Time, pszString))
     903        return RTTimeImplode(pTime, &Time);
     904    return NULL;
     905}
     906RT_EXPORT_SYMBOL(RTTimeSpecFromString);
     907
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette