Home | History | Annotate | Download | only in nspr
      1 /* Portions are Copyright (C) 2011 Google Inc */
      2 /* ***** BEGIN LICENSE BLOCK *****
      3  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
      4  *
      5  * The contents of this file are subject to the Mozilla Public License Version
      6  * 1.1 (the "License"); you may not use this file except in compliance with
      7  * the License. You may obtain a copy of the License at
      8  * http://www.mozilla.org/MPL/
      9  *
     10  * Software distributed under the License is distributed on an "AS IS" basis,
     11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
     12  * for the specific language governing rights and limitations under the
     13  * License.
     14  *
     15  * The Original Code is the Netscape Portable Runtime (NSPR).
     16  *
     17  * The Initial Developer of the Original Code is
     18  * Netscape Communications Corporation.
     19  * Portions created by the Initial Developer are Copyright (C) 1998-2000
     20  * the Initial Developer. All Rights Reserved.
     21  *
     22  * Contributor(s):
     23  *
     24  * Alternatively, the contents of this file may be used under the terms of
     25  * either the GNU General Public License Version 2 or later (the "GPL"), or
     26  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
     27  * in which case the provisions of the GPL or the LGPL are applicable instead
     28  * of those above. If you wish to allow use of your version of this file only
     29  * under the terms of either the GPL or the LGPL, and not to allow others to
     30  * use your version of this file under the terms of the MPL, indicate your
     31  * decision by deleting the provisions above and replace them with the notice
     32  * and other provisions required by the GPL or the LGPL. If you do not delete
     33  * the provisions above, a recipient may use your version of this file under
     34  * the terms of any one of the MPL, the GPL or the LGPL.
     35  *
     36  * ***** END LICENSE BLOCK ***** */
     37 
     38 /*
     39  * prtime.cc --
     40  * NOTE: The original nspr file name is prtime.c
     41  *
     42  *     NSPR date and time functions
     43  *
     44  * CVS revision 3.37
     45  */
     46 
     47 /*
     48  * The following functions were copied from the NSPR prtime.c file.
     49  * PR_ParseTimeString
     50  *   We inlined the new PR_ParseTimeStringToExplodedTime function to avoid
     51  *   copying PR_ExplodeTime and PR_LocalTimeParameters.  (The PR_ExplodeTime
     52  *   and PR_ImplodeTime calls cancel each other out.)
     53  * PR_NormalizeTime
     54  * PR_GMTParameters
     55  * PR_ImplodeTime
     56  *   This was modified to use the Win32 SYSTEMTIME/FILETIME structures
     57  *   and the timezone offsets are applied to the FILETIME structure.
     58  * All types and macros are defined in the base/third_party/prtime.h file.
     59  * These have been copied from the following nspr files. We have only copied
     60  * over the types we need.
     61  * 1. prtime.h
     62  * 2. prtypes.h
     63  * 3. prlong.h
     64  *
     65  * Unit tests are in base/time/pr_time_unittest.cc.
     66  */
     67 
     68 #include <limits.h>
     69 
     70 #include "base/logging.h"
     71 #include "base/third_party/nspr/prtime.h"
     72 #include "build/build_config.h"
     73 
     74 #if defined(OS_WIN)
     75 #include <windows.h>
     76 #elif defined(OS_MACOSX)
     77 #include <CoreFoundation/CoreFoundation.h>
     78 #elif defined(OS_ANDROID)
     79 #include <ctype.h>
     80 #include "base/os_compat_android.h"  // For timegm()
     81 #elif defined(OS_NACL)
     82 #include "base/os_compat_nacl.h"  // For timegm()
     83 #endif
     84 #include <errno.h>  /* for EINVAL */
     85 #include <time.h>
     86 
     87 /* Implements the Unix localtime_r() function for windows */
     88 #if defined(OS_WIN)
     89 static void localtime_r(const time_t* secs, struct tm* time) {
     90   (void) localtime_s(time, secs);
     91 }
     92 #endif
     93 
     94 /*
     95  *------------------------------------------------------------------------
     96  *
     97  * PR_ImplodeTime --
     98  *
     99  *     Cf. time_t mktime(struct tm *tp)
    100  *     Note that 1 year has < 2^25 seconds.  So an PRInt32 is large enough.
    101  *
    102  *------------------------------------------------------------------------
    103  */
    104 PRTime
    105 PR_ImplodeTime(const PRExplodedTime *exploded)
    106 {
    107     // This is important, we want to make sure multiplications are
    108     // done with the correct precision.
    109     static const PRTime kSecondsToMicroseconds = static_cast<PRTime>(1000000);
    110 #if defined(OS_WIN)
    111    // Create the system struct representing our exploded time.
    112     SYSTEMTIME st = {};
    113     FILETIME ft = {};
    114     ULARGE_INTEGER uli = {};
    115 
    116     st.wYear = exploded->tm_year;
    117     st.wMonth = static_cast<WORD>(exploded->tm_month + 1);
    118     st.wDayOfWeek = exploded->tm_wday;
    119     st.wDay = static_cast<WORD>(exploded->tm_mday);
    120     st.wHour = static_cast<WORD>(exploded->tm_hour);
    121     st.wMinute = static_cast<WORD>(exploded->tm_min);
    122     st.wSecond = static_cast<WORD>(exploded->tm_sec);
    123     st.wMilliseconds = static_cast<WORD>(exploded->tm_usec/1000);
    124      // Convert to FILETIME.
    125     if (!SystemTimeToFileTime(&st, &ft)) {
    126       NOTREACHED() << "Unable to convert time";
    127       return 0;
    128     }
    129     // Apply offsets.
    130     uli.LowPart = ft.dwLowDateTime;
    131     uli.HighPart = ft.dwHighDateTime;
    132     // Convert from Windows epoch to NSPR epoch, and 100-nanoseconds units
    133     // to microsecond units.
    134     PRTime result =
    135         static_cast<PRTime>((uli.QuadPart / 10) - 11644473600000000i64);
    136     // Adjust for time zone and dst.  Convert from seconds to microseconds.
    137     result -= (exploded->tm_params.tp_gmt_offset +
    138                exploded->tm_params.tp_dst_offset) * kSecondsToMicroseconds;
    139     // Add microseconds that cannot be represented in |st|.
    140     result += exploded->tm_usec % 1000;
    141     return result;
    142 #elif defined(OS_MACOSX)
    143     // Create the system struct representing our exploded time.
    144     CFGregorianDate gregorian_date;
    145     gregorian_date.year = exploded->tm_year;
    146     gregorian_date.month = exploded->tm_month + 1;
    147     gregorian_date.day = exploded->tm_mday;
    148     gregorian_date.hour = exploded->tm_hour;
    149     gregorian_date.minute = exploded->tm_min;
    150     gregorian_date.second = exploded->tm_sec;
    151 
    152     // Compute |absolute_time| in seconds, correct for gmt and dst
    153     // (note the combined offset will be negative when we need to add it), then
    154     // convert to microseconds which is what PRTime expects.
    155     CFAbsoluteTime absolute_time =
    156         CFGregorianDateGetAbsoluteTime(gregorian_date, NULL);
    157     PRTime result = static_cast<PRTime>(absolute_time);
    158     result -= exploded->tm_params.tp_gmt_offset +
    159               exploded->tm_params.tp_dst_offset;
    160     result += kCFAbsoluteTimeIntervalSince1970;  // PRTime epoch is 1970
    161     result *= kSecondsToMicroseconds;
    162     result += exploded->tm_usec;
    163     return result;
    164 #elif defined(OS_POSIX)
    165     struct tm exp_tm;
    166     memset(&exp_tm, 0, sizeof(exp_tm));
    167     exp_tm.tm_sec  = exploded->tm_sec;
    168     exp_tm.tm_min  = exploded->tm_min;
    169     exp_tm.tm_hour = exploded->tm_hour;
    170     exp_tm.tm_mday = exploded->tm_mday;
    171     exp_tm.tm_mon  = exploded->tm_month;
    172     exp_tm.tm_year = exploded->tm_year - 1900;
    173 
    174     time_t absolute_time = timegm(&exp_tm);
    175 
    176     // If timegm returned -1.  Since we don't pass it a time zone, the only
    177     // valid case of returning -1 is 1 second before Epoch (Dec 31, 1969).
    178     if (absolute_time == -1 &&
    179         !(exploded->tm_year == 1969 && exploded->tm_month == 11 &&
    180         exploded->tm_mday == 31 && exploded->tm_hour == 23 &&
    181         exploded->tm_min == 59 && exploded->tm_sec == 59)) {
    182       // If we get here, time_t must be 32 bits.
    183       // Date was possibly too far in the future and would overflow.  Return
    184       // the most future date possible (year 2038).
    185       if (exploded->tm_year >= 1970)
    186         return INT_MAX * kSecondsToMicroseconds;
    187       // Date was possibly too far in the past and would underflow.  Return
    188       // the most past date possible (year 1901).
    189       return INT_MIN * kSecondsToMicroseconds;
    190     }
    191 
    192     PRTime result = static_cast<PRTime>(absolute_time);
    193     result -= exploded->tm_params.tp_gmt_offset +
    194               exploded->tm_params.tp_dst_offset;
    195     result *= kSecondsToMicroseconds;
    196     result += exploded->tm_usec;
    197     return result;
    198 #else
    199 #error No PR_ImplodeTime implemented on your platform.
    200 #endif
    201 }
    202 
    203 /*
    204  * The COUNT_LEAPS macro counts the number of leap years passed by
    205  * till the start of the given year Y.  At the start of the year 4
    206  * A.D. the number of leap years passed by is 0, while at the start of
    207  * the year 5 A.D. this count is 1. The number of years divisible by
    208  * 100 but not divisible by 400 (the non-leap years) is deducted from
    209  * the count to get the correct number of leap years.
    210  *
    211  * The COUNT_DAYS macro counts the number of days since 01/01/01 till the
    212  * start of the given year Y. The number of days at the start of the year
    213  * 1 is 0 while the number of days at the start of the year 2 is 365
    214  * (which is ((2)-1) * 365) and so on. The reference point is 01/01/01
    215  * midnight 00:00:00.
    216  */
    217 
    218 #define COUNT_LEAPS(Y)   ( ((Y)-1)/4 - ((Y)-1)/100 + ((Y)-1)/400 )
    219 #define COUNT_DAYS(Y)  ( ((Y)-1)*365 + COUNT_LEAPS(Y) )
    220 #define DAYS_BETWEEN_YEARS(A, B)  (COUNT_DAYS(B) - COUNT_DAYS(A))
    221 
    222 /*
    223  * Static variables used by functions in this file
    224  */
    225 
    226 /*
    227  * The following array contains the day of year for the last day of
    228  * each month, where index 1 is January, and day 0 is January 1.
    229  */
    230 
    231 static const int lastDayOfMonth[2][13] = {
    232     {-1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364},
    233     {-1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365}
    234 };
    235 
    236 /*
    237  * The number of days in a month
    238  */
    239 
    240 static const PRInt8 nDays[2][12] = {
    241     {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
    242     {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
    243 };
    244 
    245 /*
    246  *-------------------------------------------------------------------------
    247  *
    248  * IsLeapYear --
    249  *
    250  *     Returns 1 if the year is a leap year, 0 otherwise.
    251  *
    252  *-------------------------------------------------------------------------
    253  */
    254 
    255 static int IsLeapYear(PRInt16 year)
    256 {
    257     if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
    258         return 1;
    259     else
    260         return 0;
    261 }
    262 
    263 /*
    264  * 'secOffset' should be less than 86400 (i.e., a day).
    265  * 'time' should point to a normalized PRExplodedTime.
    266  */
    267 
    268 static void
    269 ApplySecOffset(PRExplodedTime *time, PRInt32 secOffset)
    270 {
    271     time->tm_sec += secOffset;
    272 
    273     /* Note that in this implementation we do not count leap seconds */
    274     if (time->tm_sec < 0 || time->tm_sec >= 60) {
    275         time->tm_min += time->tm_sec / 60;
    276         time->tm_sec %= 60;
    277         if (time->tm_sec < 0) {
    278             time->tm_sec += 60;
    279             time->tm_min--;
    280         }
    281     }
    282 
    283     if (time->tm_min < 0 || time->tm_min >= 60) {
    284         time->tm_hour += time->tm_min / 60;
    285         time->tm_min %= 60;
    286         if (time->tm_min < 0) {
    287             time->tm_min += 60;
    288             time->tm_hour--;
    289         }
    290     }
    291 
    292     if (time->tm_hour < 0) {
    293         /* Decrement mday, yday, and wday */
    294         time->tm_hour += 24;
    295         time->tm_mday--;
    296         time->tm_yday--;
    297         if (time->tm_mday < 1) {
    298             time->tm_month--;
    299             if (time->tm_month < 0) {
    300                 time->tm_month = 11;
    301                 time->tm_year--;
    302                 if (IsLeapYear(time->tm_year))
    303                     time->tm_yday = 365;
    304                 else
    305                     time->tm_yday = 364;
    306             }
    307             time->tm_mday = nDays[IsLeapYear(time->tm_year)][time->tm_month];
    308         }
    309         time->tm_wday--;
    310         if (time->tm_wday < 0)
    311             time->tm_wday = 6;
    312     } else if (time->tm_hour > 23) {
    313         /* Increment mday, yday, and wday */
    314         time->tm_hour -= 24;
    315         time->tm_mday++;
    316         time->tm_yday++;
    317         if (time->tm_mday >
    318                 nDays[IsLeapYear(time->tm_year)][time->tm_month]) {
    319             time->tm_mday = 1;
    320             time->tm_month++;
    321             if (time->tm_month > 11) {
    322                 time->tm_month = 0;
    323                 time->tm_year++;
    324                 time->tm_yday = 0;
    325             }
    326         }
    327         time->tm_wday++;
    328         if (time->tm_wday > 6)
    329             time->tm_wday = 0;
    330     }
    331 }
    332 
    333 void
    334 PR_NormalizeTime(PRExplodedTime *time, PRTimeParamFn params)
    335 {
    336     int daysInMonth;
    337     PRInt32 numDays;
    338 
    339     /* Get back to GMT */
    340     time->tm_sec -= time->tm_params.tp_gmt_offset
    341             + time->tm_params.tp_dst_offset;
    342     time->tm_params.tp_gmt_offset = 0;
    343     time->tm_params.tp_dst_offset = 0;
    344 
    345     /* Now normalize GMT */
    346 
    347     if (time->tm_usec < 0 || time->tm_usec >= 1000000) {
    348         time->tm_sec +=  time->tm_usec / 1000000;
    349         time->tm_usec %= 1000000;
    350         if (time->tm_usec < 0) {
    351             time->tm_usec += 1000000;
    352             time->tm_sec--;
    353         }
    354     }
    355 
    356     /* Note that we do not count leap seconds in this implementation */
    357     if (time->tm_sec < 0 || time->tm_sec >= 60) {
    358         time->tm_min += time->tm_sec / 60;
    359         time->tm_sec %= 60;
    360         if (time->tm_sec < 0) {
    361             time->tm_sec += 60;
    362             time->tm_min--;
    363         }
    364     }
    365 
    366     if (time->tm_min < 0 || time->tm_min >= 60) {
    367         time->tm_hour += time->tm_min / 60;
    368         time->tm_min %= 60;
    369         if (time->tm_min < 0) {
    370             time->tm_min += 60;
    371             time->tm_hour--;
    372         }
    373     }
    374 
    375     if (time->tm_hour < 0 || time->tm_hour >= 24) {
    376         time->tm_mday += time->tm_hour / 24;
    377         time->tm_hour %= 24;
    378         if (time->tm_hour < 0) {
    379             time->tm_hour += 24;
    380             time->tm_mday--;
    381         }
    382     }
    383 
    384     /* Normalize month and year before mday */
    385     if (time->tm_month < 0 || time->tm_month >= 12) {
    386         time->tm_year += static_cast<PRInt16>(time->tm_month / 12);
    387         time->tm_month %= 12;
    388         if (time->tm_month < 0) {
    389             time->tm_month += 12;
    390             time->tm_year--;
    391         }
    392     }
    393 
    394     /* Now that month and year are in proper range, normalize mday */
    395 
    396     if (time->tm_mday < 1) {
    397         /* mday too small */
    398         do {
    399             /* the previous month */
    400             time->tm_month--;
    401             if (time->tm_month < 0) {
    402                 time->tm_month = 11;
    403                 time->tm_year--;
    404             }
    405             time->tm_mday += nDays[IsLeapYear(time->tm_year)][time->tm_month];
    406         } while (time->tm_mday < 1);
    407     } else {
    408         daysInMonth = nDays[IsLeapYear(time->tm_year)][time->tm_month];
    409         while (time->tm_mday > daysInMonth) {
    410             /* mday too large */
    411             time->tm_mday -= daysInMonth;
    412             time->tm_month++;
    413             if (time->tm_month > 11) {
    414                 time->tm_month = 0;
    415                 time->tm_year++;
    416             }
    417             daysInMonth = nDays[IsLeapYear(time->tm_year)][time->tm_month];
    418         }
    419     }
    420 
    421     /* Recompute yday and wday */
    422     time->tm_yday = static_cast<PRInt16>(time->tm_mday +
    423             lastDayOfMonth[IsLeapYear(time->tm_year)][time->tm_month]);
    424 
    425     numDays = DAYS_BETWEEN_YEARS(1970, time->tm_year) + time->tm_yday;
    426     time->tm_wday = (numDays + 4) % 7;
    427     if (time->tm_wday < 0) {
    428         time->tm_wday += 7;
    429     }
    430 
    431     /* Recompute time parameters */
    432 
    433     time->tm_params = params(time);
    434 
    435     ApplySecOffset(time, time->tm_params.tp_gmt_offset
    436             + time->tm_params.tp_dst_offset);
    437 }
    438 
    439 /*
    440  *------------------------------------------------------------------------
    441  *
    442  * PR_GMTParameters --
    443  *
    444  *     Returns the PRTimeParameters for Greenwich Mean Time.
    445  *     Trivially, both the tp_gmt_offset and tp_dst_offset fields are 0.
    446  *
    447  *------------------------------------------------------------------------
    448  */
    449 
    450 PRTimeParameters
    451 PR_GMTParameters(const PRExplodedTime* /* gmt */)
    452 {
    453     PRTimeParameters retVal = { 0, 0 };
    454     return retVal;
    455 }
    456 
    457 /*
    458  * The following code implements PR_ParseTimeString().  It is based on
    459  * ns/lib/xp/xp_time.c, revision 1.25, by Jamie Zawinski <jwz (at) netscape.com>.
    460  */
    461 
    462 /*
    463  * We only recognize the abbreviations of a small subset of time zones
    464  * in North America, Europe, and Japan.
    465  *
    466  * PST/PDT: Pacific Standard/Daylight Time
    467  * MST/MDT: Mountain Standard/Daylight Time
    468  * CST/CDT: Central Standard/Daylight Time
    469  * EST/EDT: Eastern Standard/Daylight Time
    470  * AST: Atlantic Standard Time
    471  * NST: Newfoundland Standard Time
    472  * GMT: Greenwich Mean Time
    473  * BST: British Summer Time
    474  * MET: Middle Europe Time
    475  * EET: Eastern Europe Time
    476  * JST: Japan Standard Time
    477  */
    478 
    479 typedef enum
    480 {
    481   TT_UNKNOWN,
    482 
    483   TT_SUN, TT_MON, TT_TUE, TT_WED, TT_THU, TT_FRI, TT_SAT,
    484 
    485   TT_JAN, TT_FEB, TT_MAR, TT_APR, TT_MAY, TT_JUN,
    486   TT_JUL, TT_AUG, TT_SEP, TT_OCT, TT_NOV, TT_DEC,
    487 
    488   TT_PST, TT_PDT, TT_MST, TT_MDT, TT_CST, TT_CDT, TT_EST, TT_EDT,
    489   TT_AST, TT_NST, TT_GMT, TT_BST, TT_MET, TT_EET, TT_JST
    490 } TIME_TOKEN;
    491 
    492 /*
    493  * This parses a time/date string into a PRTime
    494  * (microseconds after "1-Jan-1970 00:00:00 GMT").
    495  * It returns PR_SUCCESS on success, and PR_FAILURE
    496  * if the time/date string can't be parsed.
    497  *
    498  * Many formats are handled, including:
    499  *
    500  *   14 Apr 89 03:20:12
    501  *   14 Apr 89 03:20 GMT
    502  *   Fri, 17 Mar 89 4:01:33
    503  *   Fri, 17 Mar 89 4:01 GMT
    504  *   Mon Jan 16 16:12 PDT 1989
    505  *   Mon Jan 16 16:12 +0130 1989
    506  *   6 May 1992 16:41-JST (Wednesday)
    507  *   22-AUG-1993 10:59:12.82
    508  *   22-AUG-1993 10:59pm
    509  *   22-AUG-1993 12:59am
    510  *   22-AUG-1993 12:59 PM
    511  *   Friday, August 04, 1995 3:54 PM
    512  *   06/21/95 04:24:34 PM
    513  *   20/06/95 21:07
    514  *   95-06-08 19:32:48 EDT
    515  *   1995-06-17T23:11:25.342156Z
    516  *
    517  * If the input string doesn't contain a description of the timezone,
    518  * we consult the `default_to_gmt' to decide whether the string should
    519  * be interpreted relative to the local time zone (PR_FALSE) or GMT (PR_TRUE).
    520  * The correct value for this argument depends on what standard specified
    521  * the time string which you are parsing.
    522  */
    523 
    524 PRStatus
    525 PR_ParseTimeString(
    526         const char *string,
    527         PRBool default_to_gmt,
    528         PRTime *result_imploded)
    529 {
    530   PRExplodedTime tm;
    531   PRExplodedTime *result = &tm;
    532   TIME_TOKEN dotw = TT_UNKNOWN;
    533   TIME_TOKEN month = TT_UNKNOWN;
    534   TIME_TOKEN zone = TT_UNKNOWN;
    535   int zone_offset = -1;
    536   int dst_offset = 0;
    537   int date = -1;
    538   PRInt32 year = -1;
    539   int hour = -1;
    540   int min = -1;
    541   int sec = -1;
    542   int usec = -1;
    543 
    544   const char *rest = string;
    545 
    546   int iterations = 0;
    547 
    548   PR_ASSERT(string && result);
    549   if (!string || !result) return PR_FAILURE;
    550 
    551   while (*rest)
    552         {
    553 
    554           if (iterations++ > 1000)
    555                 {
    556                   return PR_FAILURE;
    557                 }
    558 
    559           switch (*rest)
    560                 {
    561                 case 'a': case 'A':
    562                   if (month == TT_UNKNOWN &&
    563                           (rest[1] == 'p' || rest[1] == 'P') &&
    564                           (rest[2] == 'r' || rest[2] == 'R'))
    565                         month = TT_APR;
    566                   else if (zone == TT_UNKNOWN &&
    567                                    (rest[1] == 's' || rest[1] == 'S') &&
    568                                    (rest[2] == 't' || rest[2] == 'T'))
    569                         zone = TT_AST;
    570                   else if (month == TT_UNKNOWN &&
    571                                    (rest[1] == 'u' || rest[1] == 'U') &&
    572                                    (rest[2] == 'g' || rest[2] == 'G'))
    573                         month = TT_AUG;
    574                   break;
    575                 case 'b': case 'B':
    576                   if (zone == TT_UNKNOWN &&
    577                           (rest[1] == 's' || rest[1] == 'S') &&
    578                           (rest[2] == 't' || rest[2] == 'T'))
    579                         zone = TT_BST;
    580                   break;
    581                 case 'c': case 'C':
    582                   if (zone == TT_UNKNOWN &&
    583                           (rest[1] == 'd' || rest[1] == 'D') &&
    584                           (rest[2] == 't' || rest[2] == 'T'))
    585                         zone = TT_CDT;
    586                   else if (zone == TT_UNKNOWN &&
    587                                    (rest[1] == 's' || rest[1] == 'S') &&
    588                                    (rest[2] == 't' || rest[2] == 'T'))
    589                         zone = TT_CST;
    590                   break;
    591                 case 'd': case 'D':
    592                   if (month == TT_UNKNOWN &&
    593                           (rest[1] == 'e' || rest[1] == 'E') &&
    594                           (rest[2] == 'c' || rest[2] == 'C'))
    595                         month = TT_DEC;
    596                   break;
    597                 case 'e': case 'E':
    598                   if (zone == TT_UNKNOWN &&
    599                           (rest[1] == 'd' || rest[1] == 'D') &&
    600                           (rest[2] == 't' || rest[2] == 'T'))
    601                         zone = TT_EDT;
    602                   else if (zone == TT_UNKNOWN &&
    603                                    (rest[1] == 'e' || rest[1] == 'E') &&
    604                                    (rest[2] == 't' || rest[2] == 'T'))
    605                         zone = TT_EET;
    606                   else if (zone == TT_UNKNOWN &&
    607                                    (rest[1] == 's' || rest[1] == 'S') &&
    608                                    (rest[2] == 't' || rest[2] == 'T'))
    609                         zone = TT_EST;
    610                   break;
    611                 case 'f': case 'F':
    612                   if (month == TT_UNKNOWN &&
    613                           (rest[1] == 'e' || rest[1] == 'E') &&
    614                           (rest[2] == 'b' || rest[2] == 'B'))
    615                         month = TT_FEB;
    616                   else if (dotw == TT_UNKNOWN &&
    617                                    (rest[1] == 'r' || rest[1] == 'R') &&
    618                                    (rest[2] == 'i' || rest[2] == 'I'))
    619                         dotw = TT_FRI;
    620                   break;
    621                 case 'g': case 'G':
    622                   if (zone == TT_UNKNOWN &&
    623                           (rest[1] == 'm' || rest[1] == 'M') &&
    624                           (rest[2] == 't' || rest[2] == 'T'))
    625                         zone = TT_GMT;
    626                   break;
    627                 case 'j': case 'J':
    628                   if (month == TT_UNKNOWN &&
    629                           (rest[1] == 'a' || rest[1] == 'A') &&
    630                           (rest[2] == 'n' || rest[2] == 'N'))
    631                         month = TT_JAN;
    632                   else if (zone == TT_UNKNOWN &&
    633                                    (rest[1] == 's' || rest[1] == 'S') &&
    634                                    (rest[2] == 't' || rest[2] == 'T'))
    635                         zone = TT_JST;
    636                   else if (month == TT_UNKNOWN &&
    637                                    (rest[1] == 'u' || rest[1] == 'U') &&
    638                                    (rest[2] == 'l' || rest[2] == 'L'))
    639                         month = TT_JUL;
    640                   else if (month == TT_UNKNOWN &&
    641                                    (rest[1] == 'u' || rest[1] == 'U') &&
    642                                    (rest[2] == 'n' || rest[2] == 'N'))
    643                         month = TT_JUN;
    644                   break;
    645                 case 'm': case 'M':
    646                   if (month == TT_UNKNOWN &&
    647                           (rest[1] == 'a' || rest[1] == 'A') &&
    648                           (rest[2] == 'r' || rest[2] == 'R'))
    649                         month = TT_MAR;
    650                   else if (month == TT_UNKNOWN &&
    651                                    (rest[1] == 'a' || rest[1] == 'A') &&
    652                                    (rest[2] == 'y' || rest[2] == 'Y'))
    653                         month = TT_MAY;
    654                   else if (zone == TT_UNKNOWN &&
    655                                    (rest[1] == 'd' || rest[1] == 'D') &&
    656                                    (rest[2] == 't' || rest[2] == 'T'))
    657                         zone = TT_MDT;
    658                   else if (zone == TT_UNKNOWN &&
    659                                    (rest[1] == 'e' || rest[1] == 'E') &&
    660                                    (rest[2] == 't' || rest[2] == 'T'))
    661                         zone = TT_MET;
    662                   else if (dotw == TT_UNKNOWN &&
    663                                    (rest[1] == 'o' || rest[1] == 'O') &&
    664                                    (rest[2] == 'n' || rest[2] == 'N'))
    665                         dotw = TT_MON;
    666                   else if (zone == TT_UNKNOWN &&
    667                                    (rest[1] == 's' || rest[1] == 'S') &&
    668                                    (rest[2] == 't' || rest[2] == 'T'))
    669                         zone = TT_MST;
    670                   break;
    671                 case 'n': case 'N':
    672                   if (month == TT_UNKNOWN &&
    673                           (rest[1] == 'o' || rest[1] == 'O') &&
    674                           (rest[2] == 'v' || rest[2] == 'V'))
    675                         month = TT_NOV;
    676                   else if (zone == TT_UNKNOWN &&
    677                                    (rest[1] == 's' || rest[1] == 'S') &&
    678                                    (rest[2] == 't' || rest[2] == 'T'))
    679                         zone = TT_NST;
    680                   break;
    681                 case 'o': case 'O':
    682                   if (month == TT_UNKNOWN &&
    683                           (rest[1] == 'c' || rest[1] == 'C') &&
    684                           (rest[2] == 't' || rest[2] == 'T'))
    685                         month = TT_OCT;
    686                   break;
    687                 case 'p': case 'P':
    688                   if (zone == TT_UNKNOWN &&
    689                           (rest[1] == 'd' || rest[1] == 'D') &&
    690                           (rest[2] == 't' || rest[2] == 'T'))
    691                         zone = TT_PDT;
    692                   else if (zone == TT_UNKNOWN &&
    693                                    (rest[1] == 's' || rest[1] == 'S') &&
    694                                    (rest[2] == 't' || rest[2] == 'T'))
    695                         zone = TT_PST;
    696                   break;
    697                 case 's': case 'S':
    698                   if (dotw == TT_UNKNOWN &&
    699                           (rest[1] == 'a' || rest[1] == 'A') &&
    700                           (rest[2] == 't' || rest[2] == 'T'))
    701                         dotw = TT_SAT;
    702                   else if (month == TT_UNKNOWN &&
    703                                    (rest[1] == 'e' || rest[1] == 'E') &&
    704                                    (rest[2] == 'p' || rest[2] == 'P'))
    705                         month = TT_SEP;
    706                   else if (dotw == TT_UNKNOWN &&
    707                                    (rest[1] == 'u' || rest[1] == 'U') &&
    708                                    (rest[2] == 'n' || rest[2] == 'N'))
    709                         dotw = TT_SUN;
    710                   break;
    711                 case 't': case 'T':
    712                   if (dotw == TT_UNKNOWN &&
    713                           (rest[1] == 'h' || rest[1] == 'H') &&
    714                           (rest[2] == 'u' || rest[2] == 'U'))
    715                         dotw = TT_THU;
    716                   else if (dotw == TT_UNKNOWN &&
    717                                    (rest[1] == 'u' || rest[1] == 'U') &&
    718                                    (rest[2] == 'e' || rest[2] == 'E'))
    719                         dotw = TT_TUE;
    720                   break;
    721                 case 'u': case 'U':
    722                   if (zone == TT_UNKNOWN &&
    723                           (rest[1] == 't' || rest[1] == 'T') &&
    724                           !(rest[2] >= 'A' && rest[2] <= 'Z') &&
    725                           !(rest[2] >= 'a' && rest[2] <= 'z'))
    726                         /* UT is the same as GMT but UTx is not. */
    727                         zone = TT_GMT;
    728                   break;
    729                 case 'w': case 'W':
    730                   if (dotw == TT_UNKNOWN &&
    731                           (rest[1] == 'e' || rest[1] == 'E') &&
    732                           (rest[2] == 'd' || rest[2] == 'D'))
    733                         dotw = TT_WED;
    734                   break;
    735 
    736                 case '+': case '-':
    737                   {
    738                         const char *end;
    739                         int sign;
    740                         if (zone_offset != -1)
    741                           {
    742                                 /* already got one... */
    743                                 rest++;
    744                                 break;
    745                           }
    746                         if (zone != TT_UNKNOWN && zone != TT_GMT)
    747                           {
    748                                 /* GMT+0300 is legal, but PST+0300 is not. */
    749                                 rest++;
    750                                 break;
    751                           }
    752 
    753                         sign = ((*rest == '+') ? 1 : -1);
    754                         rest++; /* move over sign */
    755                         end = rest;
    756                         while (*end >= '0' && *end <= '9')
    757                           end++;
    758                         if (rest == end) /* no digits here */
    759                           break;
    760 
    761                         if ((end - rest) == 4)
    762                           /* offset in HHMM */
    763                           zone_offset = (((((rest[0]-'0')*10) + (rest[1]-'0')) * 60) +
    764                                                          (((rest[2]-'0')*10) + (rest[3]-'0')));
    765                         else if ((end - rest) == 2)
    766                           /* offset in hours */
    767                           zone_offset = (((rest[0]-'0')*10) + (rest[1]-'0')) * 60;
    768                         else if ((end - rest) == 1)
    769                           /* offset in hours */
    770                           zone_offset = (rest[0]-'0') * 60;
    771                         else
    772                           /* 3 or >4 */
    773                           break;
    774 
    775                         zone_offset *= sign;
    776                         zone = TT_GMT;
    777                         break;
    778                   }
    779 
    780                 case '0': case '1': case '2': case '3': case '4':
    781                 case '5': case '6': case '7': case '8': case '9':
    782                   {
    783                         int tmp_hour = -1;
    784                         int tmp_min = -1;
    785                         int tmp_sec = -1;
    786                         int tmp_usec = -1;
    787                         const char *end = rest + 1;
    788                         while (*end >= '0' && *end <= '9')
    789                           end++;
    790 
    791                         /* end is now the first character after a range of digits. */
    792 
    793                         if (*end == ':')
    794                           {
    795                                 if (hour >= 0 && min >= 0) /* already got it */
    796                                   break;
    797 
    798                                 /* We have seen "[0-9]+:", so this is probably HH:MM[:SS] */
    799                                 if ((end - rest) > 2)
    800                                   /* it is [0-9][0-9][0-9]+: */
    801                                   break;
    802                                 else if ((end - rest) == 2)
    803                                   tmp_hour = ((rest[0]-'0')*10 +
    804                                                           (rest[1]-'0'));
    805                                 else
    806                                   tmp_hour = (rest[0]-'0');
    807 
    808                                 /* move over the colon, and parse minutes */
    809 
    810                                 rest = ++end;
    811                                 while (*end >= '0' && *end <= '9')
    812                                   end++;
    813 
    814                                 if (end == rest)
    815                                   /* no digits after first colon? */
    816                                   break;
    817                                 else if ((end - rest) > 2)
    818                                   /* it is [0-9][0-9][0-9]+: */
    819                                   break;
    820                                 else if ((end - rest) == 2)
    821                                   tmp_min = ((rest[0]-'0')*10 +
    822                                                          (rest[1]-'0'));
    823                                 else
    824                                   tmp_min = (rest[0]-'0');
    825 
    826                                 /* now go for seconds */
    827                                 rest = end;
    828                                 if (*rest == ':')
    829                                   rest++;
    830                                 end = rest;
    831                                 while (*end >= '0' && *end <= '9')
    832                                   end++;
    833 
    834                                 if (end == rest)
    835                                   /* no digits after second colon - that's ok. */
    836                                   ;
    837                                 else if ((end - rest) > 2)
    838                                   /* it is [0-9][0-9][0-9]+: */
    839                                   break;
    840                                 else if ((end - rest) == 2)
    841                                   tmp_sec = ((rest[0]-'0')*10 +
    842                                                          (rest[1]-'0'));
    843                                 else
    844                                   tmp_sec = (rest[0]-'0');
    845 
    846                                 /* fractional second */
    847                                 rest = end;
    848                                 if (*rest == '.')
    849                                   {
    850                                     rest++;
    851                                     end++;
    852                                     tmp_usec = 0;
    853                                     /* use up to 6 digits, skip over the rest */
    854                                     while (*end >= '0' && *end <= '9')
    855                                       {
    856                                         if (end - rest < 6)
    857                                           tmp_usec = tmp_usec * 10 + *end - '0';
    858                                         end++;
    859                                       }
    860                                     int ndigits = end - rest;
    861                                     while (ndigits++ < 6)
    862                                       tmp_usec *= 10;
    863                                     rest = end;
    864                                   }
    865 
    866                                 if (*rest == 'Z')
    867                                   {
    868                                     zone = TT_GMT;
    869                                     rest++;
    870                                   }
    871                                 else if (tmp_hour <= 12)
    872                                   {
    873                                     /* If we made it here, we've parsed hour and min,
    874                                        and possibly sec, so the current token is a time.
    875                                        Now skip over whitespace and see if there's an AM
    876                                        or PM directly following the time.
    877                                     */
    878                                         const char *s = end;
    879                                         while (*s && (*s == ' ' || *s == '\t'))
    880                                           s++;
    881                                         if ((s[0] == 'p' || s[0] == 'P') &&
    882                                                 (s[1] == 'm' || s[1] == 'M'))
    883                                           /* 10:05pm == 22:05, and 12:05pm == 12:05 */
    884                                           tmp_hour = (tmp_hour == 12 ? 12 : tmp_hour + 12);
    885                                         else if (tmp_hour == 12 &&
    886                                                          (s[0] == 'a' || s[0] == 'A') &&
    887                                                          (s[1] == 'm' || s[1] == 'M'))
    888                                           /* 12:05am == 00:05 */
    889                                           tmp_hour = 0;
    890                                   }
    891 
    892                                 hour = tmp_hour;
    893                                 min = tmp_min;
    894                                 sec = tmp_sec;
    895                                 usec = tmp_usec;
    896                                 rest = end;
    897                                 break;
    898                           }
    899                         else if ((*end == '/' || *end == '-') &&
    900                                          end[1] >= '0' && end[1] <= '9')
    901                           {
    902                                 /* Perhaps this is 6/16/95, 16/6/95, 6-16-95, or 16-6-95
    903                                    or even 95-06-05 or 1995-06-22.
    904                                  */
    905                                 int n1, n2, n3;
    906                                 const char *s;
    907 
    908                                 if (month != TT_UNKNOWN)
    909                                   /* if we saw a month name, this can't be. */
    910                                   break;
    911 
    912                                 s = rest;
    913 
    914                                 n1 = (*s++ - '0');                                /* first 1, 2 or 4 digits */
    915                                 if (*s >= '0' && *s <= '9')
    916                                   {
    917                                     n1 = n1*10 + (*s++ - '0');
    918 
    919                                     if (*s >= '0' && *s <= '9')            /* optional digits 3 and 4 */
    920                                       {
    921                                         n1 = n1*10 + (*s++ - '0');
    922                                         if (*s < '0' || *s > '9')
    923                                           break;
    924                                         n1 = n1*10 + (*s++ - '0');
    925                                       }
    926                                   }
    927 
    928                                 if (*s != '/' && *s != '-')                /* slash */
    929                                   break;
    930                                 s++;
    931 
    932                                 if (*s < '0' || *s > '9')                /* second 1 or 2 digits */
    933                                   break;
    934                                 n2 = (*s++ - '0');
    935                                 if (*s >= '0' && *s <= '9')
    936                                   n2 = n2*10 + (*s++ - '0');
    937 
    938                                 if (*s != '/' && *s != '-')                /* slash */
    939                                   break;
    940                                 s++;
    941 
    942                                 if (*s < '0' || *s > '9')                /* third 1, 2, 4, or 5 digits */
    943                                   break;
    944                                 n3 = (*s++ - '0');
    945                                 if (*s >= '0' && *s <= '9')
    946                                   n3 = n3*10 + (*s++ - '0');
    947 
    948                                 if (*s >= '0' && *s <= '9')            /* optional digits 3, 4, and 5 */
    949                                   {
    950                                         n3 = n3*10 + (*s++ - '0');
    951                                         if (*s < '0' || *s > '9')
    952                                           break;
    953                                         n3 = n3*10 + (*s++ - '0');
    954                                         if (*s >= '0' && *s <= '9')
    955                                           n3 = n3*10 + (*s++ - '0');
    956                                   }
    957 
    958                                 if (*s == 'T' && s[1] >= '0' && s[1] <= '9')
    959                                   /* followed by ISO 8601 T delimiter and number is ok */
    960                                   ;
    961                                 else if ((*s >= '0' && *s <= '9') ||
    962                                          (*s >= 'A' && *s <= 'Z') ||
    963                                          (*s >= 'a' && *s <= 'z'))
    964                                   /* but other alphanumerics are not ok */
    965                                   break;
    966 
    967                                 /* Ok, we parsed three multi-digit numbers, with / or -
    968                                    between them.  Now decide what the hell they are
    969                                    (DD/MM/YY or MM/DD/YY or [YY]YY/MM/DD.)
    970                                  */
    971 
    972                                 if (n1 > 31 || n1 == 0)  /* must be [YY]YY/MM/DD */
    973                                   {
    974                                         if (n2 > 12) break;
    975                                         if (n3 > 31) break;
    976                                         year = n1;
    977                                         if (year < 70)
    978                                             year += 2000;
    979                                         else if (year < 100)
    980                                             year += 1900;
    981                                         month = (TIME_TOKEN)(n2 + ((int)TT_JAN) - 1);
    982                                         date = n3;
    983                                         rest = s;
    984                                         break;
    985                                   }
    986 
    987                                 if (n1 > 12 && n2 > 12)  /* illegal */
    988                                   {
    989                                         rest = s;
    990                                         break;
    991                                   }
    992 
    993                                 if (n3 < 70)
    994                                     n3 += 2000;
    995                                 else if (n3 < 100)
    996                                     n3 += 1900;
    997 
    998                                 if (n1 > 12)  /* must be DD/MM/YY */
    999                                   {
   1000                                         date = n1;
   1001                                         month = (TIME_TOKEN)(n2 + ((int)TT_JAN) - 1);
   1002                                         year = n3;
   1003                                   }
   1004                                 else                  /* assume MM/DD/YY */
   1005                                   {
   1006                                         /* #### In the ambiguous case, should we consult the
   1007                                            locale to find out the local default? */
   1008                                         month = (TIME_TOKEN)(n1 + ((int)TT_JAN) - 1);
   1009                                         date = n2;
   1010                                         year = n3;
   1011                                   }
   1012                                 rest = s;
   1013                           }
   1014                         else if ((*end >= 'A' && *end <= 'Z') ||
   1015                                          (*end >= 'a' && *end <= 'z'))
   1016                           /* Digits followed by non-punctuation - what's that? */
   1017                           ;
   1018                         else if ((end - rest) == 5)                /* five digits is a year */
   1019                           year = (year < 0
   1020                                           ? ((rest[0]-'0')*10000L +
   1021                                                  (rest[1]-'0')*1000L +
   1022                                                  (rest[2]-'0')*100L +
   1023                                                  (rest[3]-'0')*10L +
   1024                                                  (rest[4]-'0'))
   1025                                           : year);
   1026                         else if ((end - rest) == 4)                /* four digits is a year */
   1027                           year = (year < 0
   1028                                           ? ((rest[0]-'0')*1000L +
   1029                                                  (rest[1]-'0')*100L +
   1030                                                  (rest[2]-'0')*10L +
   1031                                                  (rest[3]-'0'))
   1032                                           : year);
   1033                         else if ((end - rest) == 2)                /* two digits - date or year */
   1034                           {
   1035                                 int n = ((rest[0]-'0')*10 +
   1036                                                  (rest[1]-'0'));
   1037                                 /* If we don't have a date (day of the month) and we see a number
   1038                                      less than 32, then assume that is the date.
   1039 
   1040                                          Otherwise, if we have a date and not a year, assume this is the
   1041                                          year.  If it is less than 70, then assume it refers to the 21st
   1042                                          century.  If it is two digits (>= 70), assume it refers to this
   1043                                          century.  Otherwise, assume it refers to an unambiguous year.
   1044 
   1045                                          The world will surely end soon.
   1046                                    */
   1047                                 if (date < 0 && n < 32)
   1048                                   date = n;
   1049                                 else if (year < 0)
   1050                                   {
   1051                                         if (n < 70)
   1052                                           year = 2000 + n;
   1053                                         else if (n < 100)
   1054                                           year = 1900 + n;
   1055                                         else
   1056                                           year = n;
   1057                                   }
   1058                                 /* else what the hell is this. */
   1059                           }
   1060                         else if ((end - rest) == 1)                /* one digit - date */
   1061                           date = (date < 0 ? (rest[0]-'0') : date);
   1062                         /* else, three or more than five digits - what's that? */
   1063 
   1064                         break;
   1065                   }   /* case '0' .. '9' */
   1066                 }   /* switch */
   1067 
   1068           /* Skip to the end of this token, whether we parsed it or not.
   1069              Tokens are delimited by whitespace, or ,;-+/()[] but explicitly not .:
   1070              'T' is also treated as delimiter when followed by a digit (ISO 8601).
   1071            */
   1072           while (*rest &&
   1073                          *rest != ' ' && *rest != '\t' &&
   1074                          *rest != ',' && *rest != ';' &&
   1075                          *rest != '-' && *rest != '+' &&
   1076                          *rest != '/' &&
   1077                          *rest != '(' && *rest != ')' && *rest != '[' && *rest != ']' &&
   1078                          !(*rest == 'T' && rest[1] >= '0' && rest[1] <= '9')
   1079                 )
   1080                 rest++;
   1081           /* skip over uninteresting chars. */
   1082         SKIP_MORE:
   1083           while (*rest == ' ' || *rest == '\t' ||
   1084                  *rest == ',' || *rest == ';' || *rest == '/' ||
   1085                  *rest == '(' || *rest == ')' || *rest == '[' || *rest == ']')
   1086                 rest++;
   1087 
   1088           /* "-" is ignored at the beginning of a token if we have not yet
   1089                  parsed a year (e.g., the second "-" in "30-AUG-1966"), or if
   1090                  the character after the dash is not a digit. */
   1091           if (*rest == '-' && ((rest > string &&
   1092               isalpha((unsigned char)rest[-1]) && year < 0) ||
   1093               rest[1] < '0' || rest[1] > '9'))
   1094                 {
   1095                   rest++;
   1096                   goto SKIP_MORE;
   1097                 }
   1098 
   1099           /* Skip T that may precede ISO 8601 time. */
   1100           if (*rest == 'T' && rest[1] >= '0' && rest[1] <= '9')
   1101             rest++;
   1102         }   /* while */
   1103 
   1104   if (zone != TT_UNKNOWN && zone_offset == -1)
   1105         {
   1106           switch (zone)
   1107                 {
   1108                 case TT_PST: zone_offset = -8 * 60; break;
   1109                 case TT_PDT: zone_offset = -8 * 60; dst_offset = 1 * 60; break;
   1110                 case TT_MST: zone_offset = -7 * 60; break;
   1111                 case TT_MDT: zone_offset = -7 * 60; dst_offset = 1 * 60; break;
   1112                 case TT_CST: zone_offset = -6 * 60; break;
   1113                 case TT_CDT: zone_offset = -6 * 60; dst_offset = 1 * 60; break;
   1114                 case TT_EST: zone_offset = -5 * 60; break;
   1115                 case TT_EDT: zone_offset = -5 * 60; dst_offset = 1 * 60; break;
   1116                 case TT_AST: zone_offset = -4 * 60; break;
   1117                 case TT_NST: zone_offset = -3 * 60 - 30; break;
   1118                 case TT_GMT: zone_offset =  0 * 60; break;
   1119                 case TT_BST: zone_offset =  0 * 60; dst_offset = 1 * 60; break;
   1120                 case TT_MET: zone_offset =  1 * 60; break;
   1121                 case TT_EET: zone_offset =  2 * 60; break;
   1122                 case TT_JST: zone_offset =  9 * 60; break;
   1123                 default:
   1124                   PR_ASSERT (0);
   1125                   break;
   1126                 }
   1127         }
   1128 
   1129   /* If we didn't find a year, month, or day-of-the-month, we can't
   1130          possibly parse this, and in fact, mktime() will do something random
   1131          (I'm seeing it return "Tue Feb  5 06:28:16 2036", which is no doubt
   1132          a numerologically significant date... */
   1133   if (month == TT_UNKNOWN || date == -1 || year == -1 || year > PR_INT16_MAX)
   1134       return PR_FAILURE;
   1135 
   1136   memset(result, 0, sizeof(*result));
   1137   if (usec != -1)
   1138         result->tm_usec = usec;
   1139   if (sec != -1)
   1140         result->tm_sec = sec;
   1141   if (min != -1)
   1142         result->tm_min = min;
   1143   if (hour != -1)
   1144         result->tm_hour = hour;
   1145   if (date != -1)
   1146         result->tm_mday = date;
   1147   if (month != TT_UNKNOWN)
   1148         result->tm_month = (((int)month) - ((int)TT_JAN));
   1149   if (year != -1)
   1150         result->tm_year = static_cast<PRInt16>(year);
   1151   if (dotw != TT_UNKNOWN)
   1152         result->tm_wday = static_cast<PRInt8>(((int)dotw) - ((int)TT_SUN));
   1153   /*
   1154    * Mainly to compute wday and yday, but normalized time is also required
   1155    * by the check below that works around a Visual C++ 2005 mktime problem.
   1156    */
   1157   PR_NormalizeTime(result, PR_GMTParameters);
   1158   /* The remaining work is to set the gmt and dst offsets in tm_params. */
   1159 
   1160   if (zone == TT_UNKNOWN && default_to_gmt)
   1161         {
   1162           /* No zone was specified, so pretend the zone was GMT. */
   1163           zone = TT_GMT;
   1164           zone_offset = 0;
   1165         }
   1166 
   1167   if (zone_offset == -1)
   1168          {
   1169            /* no zone was specified, and we're to assume that everything
   1170              is local. */
   1171           struct tm localTime;
   1172           time_t secs;
   1173 
   1174           PR_ASSERT(result->tm_month > -1 &&
   1175                     result->tm_mday > 0 &&
   1176                     result->tm_hour > -1 &&
   1177                     result->tm_min > -1 &&
   1178                     result->tm_sec > -1);
   1179 
   1180             /*
   1181              * To obtain time_t from a tm structure representing the local
   1182              * time, we call mktime().  However, we need to see if we are
   1183              * on 1-Jan-1970 or before.  If we are, we can't call mktime()
   1184              * because mktime() will crash on win16. In that case, we
   1185              * calculate zone_offset based on the zone offset at
   1186              * 00:00:00, 2 Jan 1970 GMT, and subtract zone_offset from the
   1187              * date we are parsing to transform the date to GMT.  We also
   1188              * do so if mktime() returns (time_t) -1 (time out of range).
   1189            */
   1190 
   1191           /* month, day, hours, mins and secs are always non-negative
   1192              so we dont need to worry about them. */
   1193           if (result->tm_year >= 1970)
   1194                 {
   1195                   localTime.tm_sec = result->tm_sec;
   1196                   localTime.tm_min = result->tm_min;
   1197                   localTime.tm_hour = result->tm_hour;
   1198                   localTime.tm_mday = result->tm_mday;
   1199                   localTime.tm_mon = result->tm_month;
   1200                   localTime.tm_year = result->tm_year - 1900;
   1201                   /* Set this to -1 to tell mktime "I don't care".  If you set
   1202                      it to 0 or 1, you are making assertions about whether the
   1203                      date you are handing it is in daylight savings mode or not;
   1204                      and if you're wrong, it will "fix" it for you. */
   1205                   localTime.tm_isdst = -1;
   1206 
   1207 #if _MSC_VER == 1400  /* 1400 = Visual C++ 2005 (8.0) */
   1208                   /*
   1209                    * mktime will return (time_t) -1 if the input is a date
   1210                    * after 23:59:59, December 31, 3000, US Pacific Time (not
   1211                    * UTC as documented):
   1212                    * http://msdn.microsoft.com/en-us/library/d1y53h2a(VS.80).aspx
   1213                    * But if the year is 3001, mktime also invokes the invalid
   1214                    * parameter handler, causing the application to crash.  This
   1215                    * problem has been reported in
   1216                    * http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=266036.
   1217                    * We avoid this crash by not calling mktime if the date is
   1218                    * out of range.  To use a simple test that works in any time
   1219                    * zone, we consider year 3000 out of range as well.  (See
   1220                    * bug 480740.)
   1221                    */
   1222                   if (result->tm_year >= 3000) {
   1223                       /* Emulate what mktime would have done. */
   1224                       errno = EINVAL;
   1225                       secs = (time_t) -1;
   1226                   } else {
   1227                       secs = mktime(&localTime);
   1228                   }
   1229 #else
   1230                   secs = mktime(&localTime);
   1231 #endif
   1232                   if (secs != (time_t) -1)
   1233                     {
   1234                       *result_imploded = (PRInt64)secs * PR_USEC_PER_SEC;
   1235                       *result_imploded += result->tm_usec;
   1236                       return PR_SUCCESS;
   1237                     }
   1238                 }
   1239 
   1240                 /* So mktime() can't handle this case.  We assume the
   1241                    zone_offset for the date we are parsing is the same as
   1242                    the zone offset on 00:00:00 2 Jan 1970 GMT. */
   1243                 secs = 86400;
   1244                 localtime_r(&secs, &localTime);
   1245                 zone_offset = localTime.tm_min
   1246                               + 60 * localTime.tm_hour
   1247                               + 1440 * (localTime.tm_mday - 2);
   1248         }
   1249 
   1250   result->tm_params.tp_gmt_offset = zone_offset * 60;
   1251   result->tm_params.tp_dst_offset = dst_offset * 60;
   1252 
   1253   *result_imploded = PR_ImplodeTime(result);
   1254   return PR_SUCCESS;
   1255 }
   1256