VirtualBox

Changeset 74069 in vbox for trunk


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

Location:
trunk
Files:
3 edited

Legend:

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

    r74027 r74069  
    23962396# define RTTimeFromString                               RT_MANGLER(RTTimeFromString)
    23972397# define RTTimeToString                                 RT_MANGLER(RTTimeToString)
     2398# define RTTimeToRfc2822                                RT_MANGLER(RTTimeToRfc2822)
    23982399# define RTTimeZoneGetInfoByUnixName                    RT_MANGLER(RTTimeZoneGetInfoByUnixName)
    23992400# define RTTimeZoneGetInfoByWindowsName                 RT_MANGLER(RTTimeZoneGetInfoByWindowsName)
  • trunk/include/iprt/time.h

    r72285 r74069  
    803803
    804804/**
     805 * Formats the given time on a RTC-2822 compliant format.
     806 *
     807 * @returns Output string length on success (positive), VERR_BUFFER_OVERFLOW
     808 *          (negative) on failure.
     809 * @param   pTime       The time. Caller should've normalized this.
     810 * @param   psz         Where to store the string.
     811 * @param   cb          The size of the buffer.
     812 * @sa      RTTIME_RTC2822_LEN
     813 */
     814RTDECL(ssize_t) RTTimeToRfc2822(PRTTIME pTime, char *psz, size_t cb);
     815
     816/** Suggested buffer length for RTTimeToRfc2822 output, including terminator. */
     817#define RTTIME_RTC2822_LEN      40
     818
     819/**
    805820 * Checks if a year is a leap year or not.
    806821 *
  • 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