VirtualBox

Changeset 96622 in vbox


Ignore:
Timestamp:
Sep 7, 2022 1:24:26 AM (2 years ago)
Author:
vboxsync
Message:

IPRT: Added RTTimeFormatDuration[Ex] for duration/interval formatting.

Location:
trunk
Files:
1 added
5 edited

Legend:

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

    r96407 r96622  
    42044204 * @{
    42054205 */
     4206/** 1 week expressed in nanoseconds (64-bit). */
     4207#define RT_NS_1WEEK             UINT64_C(604800000000000)
     4208/** 1 day expressed in nanoseconds (64-bit). */
     4209#define RT_NS_1DAY              UINT64_C(86400000000000)
    42064210/** 1 hour expressed in nanoseconds (64-bit). */
    42074211#define RT_NS_1HOUR             UINT64_C(3600000000000)
     
    42564260#define RT_NS_1US_64            UINT64_C(1000)
    42574261
     4262/** 1 week expressed in microseconds (64-bit). */
     4263#define RT_US_1WEEK             UINT64_C(604800000000)
     4264/** 1 day expressed in microseconds (64-bit). */
     4265#define RT_US_1DAY              UINT64_C(86400000000)
    42584266/** 1 hour expressed in microseconds. */
    42594267#define RT_US_1HOUR             UINT32_C(3600000000)
     
    43144322#define RT_US_1MS_64            UINT64_C(1000)
    43154323
     4324/** 1 week expressed in milliseconds. */
     4325#define RT_MS_1WEEK             UINT32_C(604800000)
     4326/** 1 day expressed in milliseconds. */
     4327#define RT_MS_1DAY              UINT32_C(86400000)
    43164328/** 1 hour expressed in milliseconds. */
    43174329#define RT_MS_1HOUR             UINT32_C(3600000)
     
    43374349#define RT_MS_1SEC              UINT32_C(1000)
    43384350
     4351/** 1 week expressed in milliseconds - 64-bit type. */
     4352#define RT_MS_1WEEK_64          UINT64_C(604800000)
     4353/** 1 day expressed in milliseconds - 64-bit type. */
     4354#define RT_MS_1DAY_64           UINT64_C(86400000)
    43394355/** 1 hour expressed in milliseconds - 64-bit type. */
    43404356#define RT_MS_1HOUR_64          UINT64_C(3600000)
  • trunk/include/iprt/mangling.h

    r96542 r96622  
    25872587# define RTTimeDbgRaces                                 RT_MANGLER(RTTimeDbgRaces)
    25882588# define RTTimeDbgSteps                                 RT_MANGLER(RTTimeDbgSteps)
     2589# define RTTimeFormatDuration                           RT_MANGLER(RTTimeFormatDuration)
     2590# define RTTimeFormatDurationEx                         RT_MANGLER(RTTimeFormatDurationEx)
    25892591# define RTTimeExplode                                  RT_MANGLER(RTTimeExplode)
    25902592# define RTTimeImplode                                  RT_MANGLER(RTTimeImplode)
  • trunk/include/iprt/time.h

    r96407 r96622  
    663663 */
    664664RTDECL(PRTTIMESPEC) RTTimeSpecFromString(PRTTIMESPEC pTime, const char *pszString);
     665
     666/**
     667 * Formats duration as best we can according to ISO-8601, with no fraction.
     668 *
     669 * See RTTimeFormatDurationEx for details.
     670 *
     671 * @returns Number of characters in the output on success. VERR_BUFFER_OVEFLOW
     672 *          on failure.
     673 * @param   pszDst          Pointer to the output buffer.  In case of overflow,
     674 *                          the max number of characters will be written and
     675 *                          zero terminated, provided @a cbDst isn't zero.
     676 * @param   cbDst           The size of the output buffer.
     677 * @param   pDuration       The duration to format.
     678 */
     679RTDECL(int) RTTimeFormatDuration(char *pszDst, size_t cbDst, PCRTTIMESPEC pDuration);
     680
     681/**
     682 * Formats duration as best we can according to ISO-8601.
     683 *
     684 * The returned value is on the form "[-]PnnnnnWnDTnnHnnMnn.fffffffffS", where a
     685 * sequence of 'n' can be between 1 and the given lenght, and all but the
     686 * "nn.fffffffffS" part is optional and will only be outputted when the duration
     687 * is sufficiently large.  The code currently does not omit any inbetween
     688 * elements other than the day count (D), so an exactly 7 day duration is
     689 * formatted as "P1WT0H0M0.000000000S" when @a cFractionDigits is 9.
     690 *
     691 * @returns Number of characters in the output on success. VERR_BUFFER_OVEFLOW
     692 *          on failure.
     693 * @retval  VERR_OUT_OF_RANGE if @a cFractionDigits is too large.
     694 * @param   pszDst          Pointer to the output buffer.  In case of overflow,
     695 *                          the max number of characters will be written and
     696 *                          zero terminated, provided @a cbDst isn't zero.
     697 * @param   cbDst           The size of the output buffer.
     698 * @param   pDuration       The duration to format.
     699 * @param   cFractionDigits Number of digits in the second fraction part. Zero
     700 *                          for whole no fraction. Max is 9 (nano seconds).
     701 */
     702RTDECL(ssize_t) RTTimeFormatDurationEx(char *pszDst, size_t cbDst, PCRTTIMESPEC pDuration, uint32_t cFractionDigits);
     703
     704/** Max length of a RTTimeFormatDurationEx output string. */
     705#define RTTIME_DURATION_STR_LEN     (sizeof("-P99999W7D23H59M59.123456789S") + 2)
    665706
    666707/** @} */
  • trunk/src/VBox/Runtime/Makefile.kmk

    r96579 r96622  
    731731        common/time/timesup.cpp \
    732732        common/time/timezoneinfo.cpp \
     733        common/time/RTTimeFormatDurationEx.cpp \
    733734        common/vfs/vfsbase.cpp \
    734735        common/vfs/vfschain.cpp \
  • trunk/src/VBox/Runtime/testcase/tstRTTimeSpec.cpp

    r96407 r96622  
    717717    RTTESTI_CHECK_FROM(RTTimeFromRfc2822(&T2, " 00006 Sep 2018 04:09:08 GMT "));
    718718
     719
     720    /*
     721     * Duration.
     722     */
     723    RTTestSub(hTest, "Duration Formatting");
     724    static struct { int64_t cNanoSecs; uint8_t cFractionDigits; const char *pszExpect; } const s_aDuration[] =
     725    {
     726        {   0, 0, "PT0S" },
     727        {   0, 9, "PT0S" },
     728        {   RT_NS_1WEEK*52 + RT_NS_1DAY*3 + RT_NS_1HOUR*11 + RT_NS_1MIN*29 + RT_NS_1SEC_64*42 + 123456789, 9,
     729            "P52W3DT11H29M42.123456789S" },
     730        {   RT_NS_1WEEK*52 + RT_NS_1DAY*3 + RT_NS_1HOUR*11 + RT_NS_1MIN*29 + RT_NS_1SEC_64*42 + 123456789, 0,
     731            "P52W3DT11H29M42S" },
     732        {   RT_NS_1WEEK*9999 + RT_NS_1SEC_64*22 + 905964245, 0,
     733            "P9999WT0H0M22S" },
     734        {   RT_NS_1WEEK*9999 + RT_NS_1SEC_64*22 + 905964245, 6,
     735            "P9999WT0H0M22.905964S" },
     736        {   -(int64_t)(RT_NS_1WEEK*9999 + RT_NS_1SEC_64*22 + 905964245), 7,
     737            "-P9999WT0H0M22.9059642S" },
     738        {   -(int64_t)(RT_NS_1WEEK*9999 + RT_NS_1SEC_64*22 + 905964245), 7,
     739            "-P9999WT0H0M22.9059642S" },
     740        {   RT_NS_1WEEK*1 + RT_NS_1DAY*1 + RT_NS_1HOUR*1 + RT_NS_1MIN*2 + RT_NS_1SEC_64*1 + 111111111, 9,
     741            "P1W1DT1H2M1.111111111S" },
     742        {   1, 9, "PT0.000000001S" },
     743        {   1, 3, "PT0.000S" },
     744    };
     745    for (size_t i = 0; i < RT_ELEMENTS(s_aDuration); i++)
     746    {
     747        RTTIMESPEC TimeSpec;
     748        RTTimeSpecSetNano(&TimeSpec, s_aDuration[i].cNanoSecs);
     749        ssize_t cchRet = RTTimeFormatDurationEx(szValue, sizeof(szValue), &TimeSpec, s_aDuration[i].cFractionDigits);
     750        if (   cchRet != (ssize_t)strlen(s_aDuration[i].pszExpect)
     751            || memcmp(szValue, s_aDuration[i].pszExpect, cchRet + 1) != 0)
     752            RTTestIFailed("RTTimeFormatDurationEx/#%u: cchRet=%zd\n"
     753                          "  szValue: '%s', length %zu\n"
     754                          " expected: '%s', length %zu",
     755                          i, cchRet, szValue, strlen(szValue), s_aDuration[i].pszExpect, strlen(s_aDuration[i].pszExpect));
     756    }
     757
     758
    719759    /*
    720760     * Check that RTTimeZoneGetCurrent works (not really timespec, but whatever).
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