VirtualBox

Changeset 74069 in vbox for trunk/src


Ignore:
Timestamp:
Sep 4, 2018 3:15:21 PM (6 years ago)
Author:
vboxsync
Message:

IPRT/time: Added RTTimeToRfc2822 for formatting time according to RFC-2822. bugref:9167

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/common/time/time.cpp

    r72172 r74069  
    237237*/
    238238
     239/** RFC-1123 week day names. */
     240static const char * const g_apszWeekDays[7] =
     241{
     242    "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"
     243};
     244/** RFC-1123 month of the year names. */
     245static const char * const g_apszMonths[1+12] =
     246{
     247    "000", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
     248};
     249
    239250
    240251/**
     
    983994
    984995/**
     996 * Formats the given time on a RTC-2822 compliant format.
     997 *
     998 * @returns Output string length on success (positive), VERR_BUFFER_OVERFLOW
     999 *          (negative) on failure.
     1000 * @param   pTime       The time. Caller should've normalized this.
     1001 * @param   psz         Where to store the string.
     1002 * @param   cb          The size of the buffer.
     1003 */
     1004RTDECL(ssize_t) RTTimeToRfc2822(PRTTIME pTime, char *psz, size_t cb)
     1005{
     1006    Assert(pTime->u8Month > 0 && pTime->u8Month <= 12);
     1007    Assert(pTime->u8WeekDay < 7);
     1008
     1009    /* (Default to UTC if not specified) */
     1010    if (   (pTime->fFlags & RTTIME_FLAGS_TYPE_MASK) == RTTIME_FLAGS_TYPE_LOCAL
     1011        && pTime->offUTC)
     1012    {
     1013        /* Calc the UTC offset part. */
     1014        int32_t offUtc = pTime->offUTC;
     1015        Assert(offUtc <= 840 && offUtc >= -840);
     1016        char     chSign;
     1017        if (offUtc >= 0)
     1018            chSign = '+';
     1019        else
     1020        {
     1021            chSign = '-';
     1022            offUtc = -offUtc;
     1023        }
     1024        uint32_t offUtcHour   = (uint32_t)offUtc / 60;
     1025        uint32_t offUtcMinute = (uint32_t)offUtc % 60;
     1026
     1027        /* Example:                       "Mon, 31 Aug 2018 00:00:00 +0200" */
     1028        size_t cch = RTStrPrintf(psz, cb, "%s, %u %s %04RI32 %02u:%02u:%02u %c%02u%02u", g_apszWeekDays[pTime->u8WeekDay],
     1029                                 pTime->u8MonthDay, g_apszMonths[pTime->u8Month], pTime->i32Year,
     1030                                 pTime->u8Hour, pTime->u8Minute, pTime->u8Second, chSign, offUtcHour, offUtcMinute);
     1031        if (   cch >= 27
     1032            && psz[cch - 5] == chSign)
     1033            return cch;
     1034    }
     1035    else
     1036    {
     1037        /* Example:                       "Mon, 1 Jan 1971 00:00:00 -0000" */
     1038        size_t cch = RTStrPrintf(psz, cb, "%s, %u %s %04RI32 %02u:%02u:%02u -0000", g_apszWeekDays[pTime->u8WeekDay],
     1039                                 pTime->u8MonthDay, g_apszMonths[pTime->u8Month], pTime->i32Year,
     1040                                 pTime->u8Hour, pTime->u8Minute, pTime->u8Second);
     1041        if (   cch >= 27
     1042            && psz[cch - 5] == '-')
     1043            return cch;
     1044    }
     1045    return VERR_BUFFER_OVERFLOW;
     1046}
     1047RT_EXPORT_SYMBOL(RTTimeToRfc2822);
     1048
     1049
     1050/**
    9851051 * Adds one day to @a pTime.
    9861052 *
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