VirtualBox

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

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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