andre@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ andre@0: /* This Source Code Form is subject to the terms of the Mozilla Public andre@0: * License, v. 2.0. If a copy of the MPL was not distributed with this andre@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ andre@0: andre@0: /* andre@0: * prtime.c -- andre@0: * andre@0: * NSPR date and time functions andre@0: * andre@0: */ andre@0: andre@0: #include "prinit.h" andre@0: #include "prtime.h" andre@0: #include "prlock.h" andre@0: #include "prprf.h" andre@0: #include "prlog.h" andre@0: andre@0: #include andre@0: #include andre@0: #include /* for EINVAL */ andre@0: #include andre@0: andre@0: /* andre@0: * The COUNT_LEAPS macro counts the number of leap years passed by andre@0: * till the start of the given year Y. At the start of the year 4 andre@0: * A.D. the number of leap years passed by is 0, while at the start of andre@0: * the year 5 A.D. this count is 1. The number of years divisible by andre@0: * 100 but not divisible by 400 (the non-leap years) is deducted from andre@0: * the count to get the correct number of leap years. andre@0: * andre@0: * The COUNT_DAYS macro counts the number of days since 01/01/01 till the andre@0: * start of the given year Y. The number of days at the start of the year andre@0: * 1 is 0 while the number of days at the start of the year 2 is 365 andre@0: * (which is ((2)-1) * 365) and so on. The reference point is 01/01/01 andre@0: * midnight 00:00:00. andre@0: */ andre@0: andre@0: #define COUNT_LEAPS(Y) ( ((Y)-1)/4 - ((Y)-1)/100 + ((Y)-1)/400 ) andre@0: #define COUNT_DAYS(Y) ( ((Y)-1)*365 + COUNT_LEAPS(Y) ) andre@0: #define DAYS_BETWEEN_YEARS(A, B) (COUNT_DAYS(B) - COUNT_DAYS(A)) andre@0: andre@0: /* andre@0: * Static variables used by functions in this file andre@0: */ andre@0: andre@0: /* andre@0: * The following array contains the day of year for the last day of andre@0: * each month, where index 1 is January, and day 0 is January 1. andre@0: */ andre@0: andre@0: static const int lastDayOfMonth[2][13] = { andre@0: {-1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364}, andre@0: {-1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365} andre@0: }; andre@0: andre@0: /* andre@0: * The number of days in a month andre@0: */ andre@0: andre@0: static const PRInt8 nDays[2][12] = { andre@0: {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, andre@0: {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} andre@0: }; andre@0: andre@0: /* andre@0: * Declarations for internal functions defined later in this file. andre@0: */ andre@0: andre@0: static void ComputeGMT(PRTime time, PRExplodedTime *gmt); andre@0: static int IsLeapYear(PRInt16 year); andre@0: static void ApplySecOffset(PRExplodedTime *time, PRInt32 secOffset); andre@0: andre@0: /* andre@0: *------------------------------------------------------------------------ andre@0: * andre@0: * ComputeGMT -- andre@0: * andre@0: * Caveats: andre@0: * - we ignore leap seconds andre@0: * andre@0: *------------------------------------------------------------------------ andre@0: */ andre@0: andre@0: static void andre@0: ComputeGMT(PRTime time, PRExplodedTime *gmt) andre@0: { andre@0: PRInt32 tmp, rem; andre@0: PRInt32 numDays; andre@0: PRInt64 numDays64, rem64; andre@0: int isLeap; andre@0: PRInt64 sec; andre@0: PRInt64 usec; andre@0: PRInt64 usecPerSec; andre@0: PRInt64 secPerDay; andre@0: andre@0: /* andre@0: * We first do the usec, sec, min, hour thing so that we do not andre@0: * have to do LL arithmetic. andre@0: */ andre@0: andre@0: LL_I2L(usecPerSec, 1000000L); andre@0: LL_DIV(sec, time, usecPerSec); andre@0: LL_MOD(usec, time, usecPerSec); andre@0: LL_L2I(gmt->tm_usec, usec); andre@0: /* Correct for weird mod semantics so the remainder is always positive */ andre@0: if (gmt->tm_usec < 0) { andre@0: PRInt64 one; andre@0: andre@0: LL_I2L(one, 1L); andre@0: LL_SUB(sec, sec, one); andre@0: gmt->tm_usec += 1000000L; andre@0: } andre@0: andre@0: LL_I2L(secPerDay, 86400L); andre@0: LL_DIV(numDays64, sec, secPerDay); andre@0: LL_MOD(rem64, sec, secPerDay); andre@0: /* We are sure both of these numbers can fit into PRInt32 */ andre@0: LL_L2I(numDays, numDays64); andre@0: LL_L2I(rem, rem64); andre@0: if (rem < 0) { andre@0: numDays--; andre@0: rem += 86400L; andre@0: } andre@0: andre@0: /* Compute day of week. Epoch started on a Thursday. */ andre@0: andre@0: gmt->tm_wday = (numDays + 4) % 7; andre@0: if (gmt->tm_wday < 0) { andre@0: gmt->tm_wday += 7; andre@0: } andre@0: andre@0: /* Compute the time of day. */ andre@0: andre@0: gmt->tm_hour = rem / 3600; andre@0: rem %= 3600; andre@0: gmt->tm_min = rem / 60; andre@0: gmt->tm_sec = rem % 60; andre@0: andre@0: /* andre@0: * Compute the year by finding the 400 year period, then working andre@0: * down from there. andre@0: * andre@0: * Since numDays is originally the number of days since January 1, 1970, andre@0: * we must change it to be the number of days from January 1, 0001. andre@0: */ andre@0: andre@0: numDays += 719162; /* 719162 = days from year 1 up to 1970 */ andre@0: tmp = numDays / 146097; /* 146097 = days in 400 years */ andre@0: rem = numDays % 146097; andre@0: gmt->tm_year = tmp * 400 + 1; andre@0: andre@0: /* Compute the 100 year period. */ andre@0: andre@0: tmp = rem / 36524; /* 36524 = days in 100 years */ andre@0: rem %= 36524; andre@0: if (tmp == 4) { /* the 400th year is a leap year */ andre@0: tmp = 3; andre@0: rem = 36524; andre@0: } andre@0: gmt->tm_year += tmp * 100; andre@0: andre@0: /* Compute the 4 year period. */ andre@0: andre@0: tmp = rem / 1461; /* 1461 = days in 4 years */ andre@0: rem %= 1461; andre@0: gmt->tm_year += tmp * 4; andre@0: andre@0: /* Compute which year in the 4. */ andre@0: andre@0: tmp = rem / 365; andre@0: rem %= 365; andre@0: if (tmp == 4) { /* the 4th year is a leap year */ andre@0: tmp = 3; andre@0: rem = 365; andre@0: } andre@0: andre@0: gmt->tm_year += tmp; andre@0: gmt->tm_yday = rem; andre@0: isLeap = IsLeapYear(gmt->tm_year); andre@0: andre@0: /* Compute the month and day of month. */ andre@0: andre@0: for (tmp = 1; lastDayOfMonth[isLeap][tmp] < gmt->tm_yday; tmp++) { andre@0: } andre@0: gmt->tm_month = --tmp; andre@0: gmt->tm_mday = gmt->tm_yday - lastDayOfMonth[isLeap][tmp]; andre@0: andre@0: gmt->tm_params.tp_gmt_offset = 0; andre@0: gmt->tm_params.tp_dst_offset = 0; andre@0: } andre@0: andre@0: andre@0: /* andre@0: *------------------------------------------------------------------------ andre@0: * andre@0: * PR_ExplodeTime -- andre@0: * andre@0: * Cf. struct tm *gmtime(const time_t *tp) and andre@0: * struct tm *localtime(const time_t *tp) andre@0: * andre@0: *------------------------------------------------------------------------ andre@0: */ andre@0: andre@0: PR_IMPLEMENT(void) andre@0: PR_ExplodeTime( andre@0: PRTime usecs, andre@0: PRTimeParamFn params, andre@0: PRExplodedTime *exploded) andre@0: { andre@0: ComputeGMT(usecs, exploded); andre@0: exploded->tm_params = params(exploded); andre@0: ApplySecOffset(exploded, exploded->tm_params.tp_gmt_offset andre@0: + exploded->tm_params.tp_dst_offset); andre@0: } andre@0: andre@0: andre@0: /* andre@0: *------------------------------------------------------------------------ andre@0: * andre@0: * PR_ImplodeTime -- andre@0: * andre@0: * Cf. time_t mktime(struct tm *tp) andre@0: * Note that 1 year has < 2^25 seconds. So an PRInt32 is large enough. andre@0: * andre@0: *------------------------------------------------------------------------ andre@0: */ andre@0: PR_IMPLEMENT(PRTime) andre@0: PR_ImplodeTime(const PRExplodedTime *exploded) andre@0: { andre@0: PRExplodedTime copy; andre@0: PRTime retVal; andre@0: PRInt64 secPerDay, usecPerSec; andre@0: PRInt64 temp; andre@0: PRInt64 numSecs64; andre@0: PRInt32 numDays; andre@0: PRInt32 numSecs; andre@0: andre@0: /* Normalize first. Do this on our copy */ andre@0: copy = *exploded; andre@0: PR_NormalizeTime(©, PR_GMTParameters); andre@0: andre@0: numDays = DAYS_BETWEEN_YEARS(1970, copy.tm_year); andre@0: andre@0: numSecs = copy.tm_yday * 86400 + copy.tm_hour * 3600 andre@0: + copy.tm_min * 60 + copy.tm_sec; andre@0: andre@0: LL_I2L(temp, numDays); andre@0: LL_I2L(secPerDay, 86400); andre@0: LL_MUL(temp, temp, secPerDay); andre@0: LL_I2L(numSecs64, numSecs); andre@0: LL_ADD(numSecs64, numSecs64, temp); andre@0: andre@0: /* apply the GMT and DST offsets */ andre@0: LL_I2L(temp, copy.tm_params.tp_gmt_offset); andre@0: LL_SUB(numSecs64, numSecs64, temp); andre@0: LL_I2L(temp, copy.tm_params.tp_dst_offset); andre@0: LL_SUB(numSecs64, numSecs64, temp); andre@0: andre@0: LL_I2L(usecPerSec, 1000000L); andre@0: LL_MUL(temp, numSecs64, usecPerSec); andre@0: LL_I2L(retVal, copy.tm_usec); andre@0: LL_ADD(retVal, retVal, temp); andre@0: andre@0: return retVal; andre@0: } andre@0: andre@0: /* andre@0: *------------------------------------------------------------------------- andre@0: * andre@0: * IsLeapYear -- andre@0: * andre@0: * Returns 1 if the year is a leap year, 0 otherwise. andre@0: * andre@0: *------------------------------------------------------------------------- andre@0: */ andre@0: andre@0: static int IsLeapYear(PRInt16 year) andre@0: { andre@0: if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) andre@0: return 1; andre@0: else andre@0: return 0; andre@0: } andre@0: andre@0: /* andre@0: * 'secOffset' should be less than 86400 (i.e., a day). andre@0: * 'time' should point to a normalized PRExplodedTime. andre@0: */ andre@0: andre@0: static void andre@0: ApplySecOffset(PRExplodedTime *time, PRInt32 secOffset) andre@0: { andre@0: time->tm_sec += secOffset; andre@0: andre@0: /* Note that in this implementation we do not count leap seconds */ andre@0: if (time->tm_sec < 0 || time->tm_sec >= 60) { andre@0: time->tm_min += time->tm_sec / 60; andre@0: time->tm_sec %= 60; andre@0: if (time->tm_sec < 0) { andre@0: time->tm_sec += 60; andre@0: time->tm_min--; andre@0: } andre@0: } andre@0: andre@0: if (time->tm_min < 0 || time->tm_min >= 60) { andre@0: time->tm_hour += time->tm_min / 60; andre@0: time->tm_min %= 60; andre@0: if (time->tm_min < 0) { andre@0: time->tm_min += 60; andre@0: time->tm_hour--; andre@0: } andre@0: } andre@0: andre@0: if (time->tm_hour < 0) { andre@0: /* Decrement mday, yday, and wday */ andre@0: time->tm_hour += 24; andre@0: time->tm_mday--; andre@0: time->tm_yday--; andre@0: if (time->tm_mday < 1) { andre@0: time->tm_month--; andre@0: if (time->tm_month < 0) { andre@0: time->tm_month = 11; andre@0: time->tm_year--; andre@0: if (IsLeapYear(time->tm_year)) andre@0: time->tm_yday = 365; andre@0: else andre@0: time->tm_yday = 364; andre@0: } andre@0: time->tm_mday = nDays[IsLeapYear(time->tm_year)][time->tm_month]; andre@0: } andre@0: time->tm_wday--; andre@0: if (time->tm_wday < 0) andre@0: time->tm_wday = 6; andre@0: } else if (time->tm_hour > 23) { andre@0: /* Increment mday, yday, and wday */ andre@0: time->tm_hour -= 24; andre@0: time->tm_mday++; andre@0: time->tm_yday++; andre@0: if (time->tm_mday > andre@0: nDays[IsLeapYear(time->tm_year)][time->tm_month]) { andre@0: time->tm_mday = 1; andre@0: time->tm_month++; andre@0: if (time->tm_month > 11) { andre@0: time->tm_month = 0; andre@0: time->tm_year++; andre@0: time->tm_yday = 0; andre@0: } andre@0: } andre@0: time->tm_wday++; andre@0: if (time->tm_wday > 6) andre@0: time->tm_wday = 0; andre@0: } andre@0: } andre@0: andre@0: PR_IMPLEMENT(void) andre@0: PR_NormalizeTime(PRExplodedTime *time, PRTimeParamFn params) andre@0: { andre@0: int daysInMonth; andre@0: PRInt32 numDays; andre@0: andre@0: /* Get back to GMT */ andre@0: time->tm_sec -= time->tm_params.tp_gmt_offset andre@0: + time->tm_params.tp_dst_offset; andre@0: time->tm_params.tp_gmt_offset = 0; andre@0: time->tm_params.tp_dst_offset = 0; andre@0: andre@0: /* Now normalize GMT */ andre@0: andre@0: if (time->tm_usec < 0 || time->tm_usec >= 1000000) { andre@0: time->tm_sec += time->tm_usec / 1000000; andre@0: time->tm_usec %= 1000000; andre@0: if (time->tm_usec < 0) { andre@0: time->tm_usec += 1000000; andre@0: time->tm_sec--; andre@0: } andre@0: } andre@0: andre@0: /* Note that we do not count leap seconds in this implementation */ andre@0: if (time->tm_sec < 0 || time->tm_sec >= 60) { andre@0: time->tm_min += time->tm_sec / 60; andre@0: time->tm_sec %= 60; andre@0: if (time->tm_sec < 0) { andre@0: time->tm_sec += 60; andre@0: time->tm_min--; andre@0: } andre@0: } andre@0: andre@0: if (time->tm_min < 0 || time->tm_min >= 60) { andre@0: time->tm_hour += time->tm_min / 60; andre@0: time->tm_min %= 60; andre@0: if (time->tm_min < 0) { andre@0: time->tm_min += 60; andre@0: time->tm_hour--; andre@0: } andre@0: } andre@0: andre@0: if (time->tm_hour < 0 || time->tm_hour >= 24) { andre@0: time->tm_mday += time->tm_hour / 24; andre@0: time->tm_hour %= 24; andre@0: if (time->tm_hour < 0) { andre@0: time->tm_hour += 24; andre@0: time->tm_mday--; andre@0: } andre@0: } andre@0: andre@0: /* Normalize month and year before mday */ andre@0: if (time->tm_month < 0 || time->tm_month >= 12) { andre@0: time->tm_year += time->tm_month / 12; andre@0: time->tm_month %= 12; andre@0: if (time->tm_month < 0) { andre@0: time->tm_month += 12; andre@0: time->tm_year--; andre@0: } andre@0: } andre@0: andre@0: /* Now that month and year are in proper range, normalize mday */ andre@0: andre@0: if (time->tm_mday < 1) { andre@0: /* mday too small */ andre@0: do { andre@0: /* the previous month */ andre@0: time->tm_month--; andre@0: if (time->tm_month < 0) { andre@0: time->tm_month = 11; andre@0: time->tm_year--; andre@0: } andre@0: time->tm_mday += nDays[IsLeapYear(time->tm_year)][time->tm_month]; andre@0: } while (time->tm_mday < 1); andre@0: } else { andre@0: daysInMonth = nDays[IsLeapYear(time->tm_year)][time->tm_month]; andre@0: while (time->tm_mday > daysInMonth) { andre@0: /* mday too large */ andre@0: time->tm_mday -= daysInMonth; andre@0: time->tm_month++; andre@0: if (time->tm_month > 11) { andre@0: time->tm_month = 0; andre@0: time->tm_year++; andre@0: } andre@0: daysInMonth = nDays[IsLeapYear(time->tm_year)][time->tm_month]; andre@0: } andre@0: } andre@0: andre@0: /* Recompute yday and wday */ andre@0: time->tm_yday = time->tm_mday + andre@0: lastDayOfMonth[IsLeapYear(time->tm_year)][time->tm_month]; andre@0: andre@0: numDays = DAYS_BETWEEN_YEARS(1970, time->tm_year) + time->tm_yday; andre@0: time->tm_wday = (numDays + 4) % 7; andre@0: if (time->tm_wday < 0) { andre@0: time->tm_wday += 7; andre@0: } andre@0: andre@0: /* Recompute time parameters */ andre@0: andre@0: time->tm_params = params(time); andre@0: andre@0: ApplySecOffset(time, time->tm_params.tp_gmt_offset andre@0: + time->tm_params.tp_dst_offset); andre@0: } andre@0: andre@0: andre@0: /* andre@0: *------------------------------------------------------------------------- andre@0: * andre@0: * PR_LocalTimeParameters -- andre@0: * andre@0: * returns the time parameters for the local time zone andre@0: * andre@0: * The following uses localtime() from the standard C library. andre@0: * (time.h) This is our fallback implementation. Unix, PC, and BeOS andre@0: * use this version. A platform may have its own machine-dependent andre@0: * implementation of this function. andre@0: * andre@0: *------------------------------------------------------------------------- andre@0: */ andre@0: andre@0: #if defined(HAVE_INT_LOCALTIME_R) andre@0: andre@0: /* andre@0: * In this case we could define the macro as andre@0: * #define MT_safe_localtime(timer, result) \ andre@0: * (localtime_r(timer, result) == 0 ? result : NULL) andre@0: * I chose to compare the return value of localtime_r with -1 so andre@0: * that I can catch the cases where localtime_r returns a pointer andre@0: * to struct tm. The macro definition above would not be able to andre@0: * detect such mistakes because it is legal to compare a pointer andre@0: * with 0. andre@0: */ andre@0: andre@0: #define MT_safe_localtime(timer, result) \ andre@0: (localtime_r(timer, result) == -1 ? NULL: result) andre@0: andre@0: #elif defined(HAVE_POINTER_LOCALTIME_R) andre@0: andre@0: #define MT_safe_localtime localtime_r andre@0: andre@0: #else andre@0: andre@0: #define HAVE_LOCALTIME_MONITOR 1 /* We use 'monitor' to serialize our calls andre@0: * to localtime(). */ andre@0: static PRLock *monitor = NULL; andre@0: andre@0: static struct tm *MT_safe_localtime(const time_t *clock, struct tm *result) andre@0: { andre@0: struct tm *tmPtr; andre@0: int needLock = PR_Initialized(); /* We need to use a lock to protect andre@0: * against NSPR threads only when the andre@0: * NSPR thread system is activated. */ andre@0: andre@0: if (needLock) PR_Lock(monitor); andre@0: andre@0: /* andre@0: * Microsoft (all flavors) localtime() returns a NULL pointer if 'clock' andre@0: * represents a time before midnight January 1, 1970. In andre@0: * that case, we also return a NULL pointer and the struct tm andre@0: * object pointed to by 'result' is not modified. andre@0: * andre@0: * Watcom C/C++ 11.0 localtime() treats time_t as unsigned long andre@0: * hence, does not recognize negative values of clock as pre-1/1/70. andre@0: * We have to manually check (WIN16 only) for negative value of andre@0: * clock and return NULL. andre@0: * andre@0: * With negative values of clock, OS/2 returns the struct tm for andre@0: * clock plus ULONG_MAX. So we also have to check for the invalid andre@0: * structs returned for timezones west of Greenwich when clock == 0. andre@0: */ andre@0: andre@0: tmPtr = localtime(clock); andre@0: andre@0: #if defined(WIN16) || defined(XP_OS2) andre@0: if ( (PRInt32) *clock < 0 || andre@0: ( (PRInt32) *clock == 0 && tmPtr->tm_year != 70)) andre@0: result = NULL; andre@0: else andre@0: *result = *tmPtr; andre@0: #else andre@0: if (tmPtr) { andre@0: *result = *tmPtr; andre@0: } else { andre@0: result = NULL; andre@0: } andre@0: #endif /* WIN16 */ andre@0: andre@0: if (needLock) PR_Unlock(monitor); andre@0: andre@0: return result; andre@0: } andre@0: andre@0: #endif /* definition of MT_safe_localtime() */ andre@0: andre@0: void _PR_InitTime(void) andre@0: { andre@0: #ifdef HAVE_LOCALTIME_MONITOR andre@0: monitor = PR_NewLock(); andre@0: #endif andre@0: #ifdef WINCE andre@0: _MD_InitTime(); andre@0: #endif andre@0: } andre@0: andre@0: void _PR_CleanupTime(void) andre@0: { andre@0: #ifdef HAVE_LOCALTIME_MONITOR andre@0: if (monitor) { andre@0: PR_DestroyLock(monitor); andre@0: monitor = NULL; andre@0: } andre@0: #endif andre@0: #ifdef WINCE andre@0: _MD_CleanupTime(); andre@0: #endif andre@0: } andre@0: andre@0: #if defined(XP_UNIX) || defined(XP_PC) || defined(XP_BEOS) andre@0: andre@0: PR_IMPLEMENT(PRTimeParameters) andre@0: PR_LocalTimeParameters(const PRExplodedTime *gmt) andre@0: { andre@0: andre@0: PRTimeParameters retVal; andre@0: struct tm localTime; andre@0: time_t secs; andre@0: PRTime secs64; andre@0: PRInt64 usecPerSec; andre@0: PRInt64 usecPerSec_1; andre@0: PRInt64 maxInt32; andre@0: PRInt64 minInt32; andre@0: PRInt32 dayOffset; andre@0: PRInt32 offset2Jan1970; andre@0: PRInt32 offsetNew; andre@0: int isdst2Jan1970; andre@0: andre@0: /* andre@0: * Calculate the GMT offset. First, figure out what is andre@0: * 00:00:00 Jan. 2, 1970 GMT (which is exactly a day, or 86400 andre@0: * seconds, since the epoch) in local time. Then we calculate andre@0: * the difference between local time and GMT in seconds: andre@0: * gmt_offset = local_time - GMT andre@0: * andre@0: * Caveat: the validity of this calculation depends on two andre@0: * assumptions: andre@0: * 1. Daylight saving time was not in effect on Jan. 2, 1970. andre@0: * 2. The time zone of the geographic location has not changed andre@0: * since Jan. 2, 1970. andre@0: */ andre@0: andre@0: secs = 86400L; andre@0: (void) MT_safe_localtime(&secs, &localTime); andre@0: andre@0: /* GMT is 00:00:00, 2nd of Jan. */ andre@0: andre@0: offset2Jan1970 = (PRInt32)localTime.tm_sec andre@0: + 60L * (PRInt32)localTime.tm_min andre@0: + 3600L * (PRInt32)localTime.tm_hour andre@0: + 86400L * (PRInt32)((PRInt32)localTime.tm_mday - 2L); andre@0: andre@0: isdst2Jan1970 = localTime.tm_isdst; andre@0: andre@0: /* andre@0: * Now compute DST offset. We calculate the overall offset andre@0: * of local time from GMT, similar to above. The overall andre@0: * offset has two components: gmt offset and dst offset. andre@0: * We subtract gmt offset from the overall offset to get andre@0: * the dst offset. andre@0: * overall_offset = local_time - GMT andre@0: * overall_offset = gmt_offset + dst_offset andre@0: * ==> dst_offset = local_time - GMT - gmt_offset andre@0: */ andre@0: andre@0: secs64 = PR_ImplodeTime(gmt); /* This is still in microseconds */ andre@0: LL_I2L(usecPerSec, PR_USEC_PER_SEC); andre@0: LL_I2L(usecPerSec_1, PR_USEC_PER_SEC - 1); andre@0: /* Convert to seconds, truncating down (3.1 -> 3 and -3.1 -> -4) */ andre@0: if (LL_GE_ZERO(secs64)) { andre@0: LL_DIV(secs64, secs64, usecPerSec); andre@0: } else { andre@0: LL_NEG(secs64, secs64); andre@0: LL_ADD(secs64, secs64, usecPerSec_1); andre@0: LL_DIV(secs64, secs64, usecPerSec); andre@0: LL_NEG(secs64, secs64); andre@0: } andre@0: LL_I2L(maxInt32, PR_INT32_MAX); andre@0: LL_I2L(minInt32, PR_INT32_MIN); andre@0: if (LL_CMP(secs64, >, maxInt32) || LL_CMP(secs64, <, minInt32)) { andre@0: /* secs64 is too large or too small for time_t (32-bit integer) */ andre@0: retVal.tp_gmt_offset = offset2Jan1970; andre@0: retVal.tp_dst_offset = 0; andre@0: return retVal; andre@0: } andre@0: LL_L2I(secs, secs64); andre@0: andre@0: /* andre@0: * On Windows, localtime() (and our MT_safe_localtime() too) andre@0: * returns a NULL pointer for time before midnight January 1, andre@0: * 1970 GMT. In that case, we just use the GMT offset for andre@0: * Jan 2, 1970 and assume that DST was not in effect. andre@0: */ andre@0: andre@0: if (MT_safe_localtime(&secs, &localTime) == NULL) { andre@0: retVal.tp_gmt_offset = offset2Jan1970; andre@0: retVal.tp_dst_offset = 0; andre@0: return retVal; andre@0: } andre@0: andre@0: /* andre@0: * dayOffset is the offset between local time and GMT in andre@0: * the day component, which can only be -1, 0, or 1. We andre@0: * use the day of the week to compute dayOffset. andre@0: */ andre@0: andre@0: dayOffset = (PRInt32) localTime.tm_wday - gmt->tm_wday; andre@0: andre@0: /* andre@0: * Need to adjust for wrapping around of day of the week from andre@0: * 6 back to 0. andre@0: */ andre@0: andre@0: if (dayOffset == -6) { andre@0: /* Local time is Sunday (0) and GMT is Saturday (6) */ andre@0: dayOffset = 1; andre@0: } else if (dayOffset == 6) { andre@0: /* Local time is Saturday (6) and GMT is Sunday (0) */ andre@0: dayOffset = -1; andre@0: } andre@0: andre@0: offsetNew = (PRInt32)localTime.tm_sec - gmt->tm_sec andre@0: + 60L * ((PRInt32)localTime.tm_min - gmt->tm_min) andre@0: + 3600L * ((PRInt32)localTime.tm_hour - gmt->tm_hour) andre@0: + 86400L * (PRInt32)dayOffset; andre@0: andre@0: if (localTime.tm_isdst <= 0) { andre@0: /* DST is not in effect */ andre@0: retVal.tp_gmt_offset = offsetNew; andre@0: retVal.tp_dst_offset = 0; andre@0: } else { andre@0: /* DST is in effect */ andre@0: if (isdst2Jan1970 <=0) { andre@0: /* andre@0: * DST was not in effect back in 2 Jan. 1970. andre@0: * Use the offset back then as the GMT offset, andre@0: * assuming the time zone has not changed since then. andre@0: */ andre@0: retVal.tp_gmt_offset = offset2Jan1970; andre@0: retVal.tp_dst_offset = offsetNew - offset2Jan1970; andre@0: } else { andre@0: /* andre@0: * DST was also in effect back in 2 Jan. 1970. andre@0: * Then our clever trick (or rather, ugly hack) fails. andre@0: * We will just assume DST offset is an hour. andre@0: */ andre@0: retVal.tp_gmt_offset = offsetNew - 3600; andre@0: retVal.tp_dst_offset = 3600; andre@0: } andre@0: } andre@0: andre@0: return retVal; andre@0: } andre@0: andre@0: #endif /* defined(XP_UNIX) || defined(XP_PC) || defined(XP_BEOS) */ andre@0: andre@0: /* andre@0: *------------------------------------------------------------------------ andre@0: * andre@0: * PR_USPacificTimeParameters -- andre@0: * andre@0: * The time parameters function for the US Pacific Time Zone. andre@0: * andre@0: *------------------------------------------------------------------------ andre@0: */ andre@0: andre@0: /* andre@0: * Returns the mday of the first sunday of the month, where andre@0: * mday and wday are for a given day in the month. andre@0: * mdays start with 1 (e.g. 1..31). andre@0: * wdays start with 0 and are in the range 0..6. 0 = Sunday. andre@0: */ andre@0: #define firstSunday(mday, wday) (((mday - wday + 7 - 1) % 7) + 1) andre@0: andre@0: /* andre@0: * Returns the mday for the N'th Sunday of the month, where andre@0: * mday and wday are for a given day in the month. andre@0: * mdays start with 1 (e.g. 1..31). andre@0: * wdays start with 0 and are in the range 0..6. 0 = Sunday. andre@0: * N has the following values: 0 = first, 1 = second (etc), -1 = last. andre@0: * ndays is the number of days in that month, the same value as the andre@0: * mday of the last day of the month. andre@0: */ andre@0: static PRInt32 andre@0: NthSunday(PRInt32 mday, PRInt32 wday, PRInt32 N, PRInt32 ndays) andre@0: { andre@0: PRInt32 firstSun = firstSunday(mday, wday); andre@0: andre@0: if (N < 0) andre@0: N = (ndays - firstSun) / 7; andre@0: return firstSun + (7 * N); andre@0: } andre@0: andre@0: typedef struct DSTParams { andre@0: PRInt8 dst_start_month; /* 0 = January */ andre@0: PRInt8 dst_start_Nth_Sunday; /* N as defined above */ andre@0: PRInt8 dst_start_month_ndays; /* ndays as defined above */ andre@0: PRInt8 dst_end_month; /* 0 = January */ andre@0: PRInt8 dst_end_Nth_Sunday; /* N as defined above */ andre@0: PRInt8 dst_end_month_ndays; /* ndays as defined above */ andre@0: } DSTParams; andre@0: andre@0: static const DSTParams dstParams[2] = { andre@0: /* year < 2007: First April Sunday - Last October Sunday */ andre@0: { 3, 0, 30, 9, -1, 31 }, andre@0: /* year >= 2007: Second March Sunday - First November Sunday */ andre@0: { 2, 1, 31, 10, 0, 30 } andre@0: }; andre@0: andre@0: PR_IMPLEMENT(PRTimeParameters) andre@0: PR_USPacificTimeParameters(const PRExplodedTime *gmt) andre@0: { andre@0: const DSTParams *dst; andre@0: PRTimeParameters retVal; andre@0: PRExplodedTime st; andre@0: andre@0: /* andre@0: * Based on geographic location and GMT, figure out offset of andre@0: * standard time from GMT. In this example implementation, we andre@0: * assume the local time zone is US Pacific Time. andre@0: */ andre@0: andre@0: retVal.tp_gmt_offset = -8L * 3600L; andre@0: andre@0: /* andre@0: * Make a copy of GMT. Note that the tm_params field of this copy andre@0: * is ignored. andre@0: */ andre@0: andre@0: st.tm_usec = gmt->tm_usec; andre@0: st.tm_sec = gmt->tm_sec; andre@0: st.tm_min = gmt->tm_min; andre@0: st.tm_hour = gmt->tm_hour; andre@0: st.tm_mday = gmt->tm_mday; andre@0: st.tm_month = gmt->tm_month; andre@0: st.tm_year = gmt->tm_year; andre@0: st.tm_wday = gmt->tm_wday; andre@0: st.tm_yday = gmt->tm_yday; andre@0: andre@0: /* Apply the offset to GMT to obtain the local standard time */ andre@0: ApplySecOffset(&st, retVal.tp_gmt_offset); andre@0: andre@0: if (st.tm_year < 2007) { /* first April Sunday - Last October Sunday */ andre@0: dst = &dstParams[0]; andre@0: } else { /* Second March Sunday - First November Sunday */ andre@0: dst = &dstParams[1]; andre@0: } andre@0: andre@0: /* andre@0: * Apply the rules on standard time or GMT to obtain daylight saving andre@0: * time offset. In this implementation, we use the US DST rule. andre@0: */ andre@0: if (st.tm_month < dst->dst_start_month) { andre@0: retVal.tp_dst_offset = 0L; andre@0: } else if (st.tm_month == dst->dst_start_month) { andre@0: int NthSun = NthSunday(st.tm_mday, st.tm_wday, andre@0: dst->dst_start_Nth_Sunday, andre@0: dst->dst_start_month_ndays); andre@0: if (st.tm_mday < NthSun) { /* Before starting Sunday */ andre@0: retVal.tp_dst_offset = 0L; andre@0: } else if (st.tm_mday == NthSun) { /* Starting Sunday */ andre@0: /* 01:59:59 PST -> 03:00:00 PDT */ andre@0: if (st.tm_hour < 2) { andre@0: retVal.tp_dst_offset = 0L; andre@0: } else { andre@0: retVal.tp_dst_offset = 3600L; andre@0: } andre@0: } else { /* After starting Sunday */ andre@0: retVal.tp_dst_offset = 3600L; andre@0: } andre@0: } else if (st.tm_month < dst->dst_end_month) { andre@0: retVal.tp_dst_offset = 3600L; andre@0: } else if (st.tm_month == dst->dst_end_month) { andre@0: int NthSun = NthSunday(st.tm_mday, st.tm_wday, andre@0: dst->dst_end_Nth_Sunday, andre@0: dst->dst_end_month_ndays); andre@0: if (st.tm_mday < NthSun) { /* Before ending Sunday */ andre@0: retVal.tp_dst_offset = 3600L; andre@0: } else if (st.tm_mday == NthSun) { /* Ending Sunday */ andre@0: /* 01:59:59 PDT -> 01:00:00 PST */ andre@0: if (st.tm_hour < 1) { andre@0: retVal.tp_dst_offset = 3600L; andre@0: } else { andre@0: retVal.tp_dst_offset = 0L; andre@0: } andre@0: } else { /* After ending Sunday */ andre@0: retVal.tp_dst_offset = 0L; andre@0: } andre@0: } else { andre@0: retVal.tp_dst_offset = 0L; andre@0: } andre@0: return retVal; andre@0: } andre@0: andre@0: /* andre@0: *------------------------------------------------------------------------ andre@0: * andre@0: * PR_GMTParameters -- andre@0: * andre@0: * Returns the PRTimeParameters for Greenwich Mean Time. andre@0: * Trivially, both the tp_gmt_offset and tp_dst_offset fields are 0. andre@0: * andre@0: *------------------------------------------------------------------------ andre@0: */ andre@0: andre@0: PR_IMPLEMENT(PRTimeParameters) andre@0: PR_GMTParameters(const PRExplodedTime *gmt) andre@0: { andre@0: PRTimeParameters retVal = { 0, 0 }; andre@0: return retVal; andre@0: } andre@0: andre@0: /* andre@0: * The following code implements PR_ParseTimeString(). It is based on andre@0: * ns/lib/xp/xp_time.c, revision 1.25, by Jamie Zawinski . andre@0: */ andre@0: andre@0: /* andre@0: * We only recognize the abbreviations of a small subset of time zones andre@0: * in North America, Europe, and Japan. andre@0: * andre@0: * PST/PDT: Pacific Standard/Daylight Time andre@0: * MST/MDT: Mountain Standard/Daylight Time andre@0: * CST/CDT: Central Standard/Daylight Time andre@0: * EST/EDT: Eastern Standard/Daylight Time andre@0: * AST: Atlantic Standard Time andre@0: * NST: Newfoundland Standard Time andre@0: * GMT: Greenwich Mean Time andre@0: * BST: British Summer Time andre@0: * MET: Middle Europe Time andre@0: * EET: Eastern Europe Time andre@0: * JST: Japan Standard Time andre@0: */ andre@0: andre@0: typedef enum andre@0: { andre@0: TT_UNKNOWN, andre@0: andre@0: TT_SUN, TT_MON, TT_TUE, TT_WED, TT_THU, TT_FRI, TT_SAT, andre@0: andre@0: TT_JAN, TT_FEB, TT_MAR, TT_APR, TT_MAY, TT_JUN, andre@0: TT_JUL, TT_AUG, TT_SEP, TT_OCT, TT_NOV, TT_DEC, andre@0: andre@0: TT_PST, TT_PDT, TT_MST, TT_MDT, TT_CST, TT_CDT, TT_EST, TT_EDT, andre@0: TT_AST, TT_NST, TT_GMT, TT_BST, TT_MET, TT_EET, TT_JST andre@0: } TIME_TOKEN; andre@0: andre@0: /* andre@0: * This parses a time/date string into a PRTime andre@0: * (microseconds after "1-Jan-1970 00:00:00 GMT"). andre@0: * It returns PR_SUCCESS on success, and PR_FAILURE andre@0: * if the time/date string can't be parsed. andre@0: * andre@0: * Many formats are handled, including: andre@0: * andre@0: * 14 Apr 89 03:20:12 andre@0: * 14 Apr 89 03:20 GMT andre@0: * Fri, 17 Mar 89 4:01:33 andre@0: * Fri, 17 Mar 89 4:01 GMT andre@0: * Mon Jan 16 16:12 PDT 1989 andre@0: * Mon Jan 16 16:12 +0130 1989 andre@0: * 6 May 1992 16:41-JST (Wednesday) andre@0: * 22-AUG-1993 10:59:12.82 andre@0: * 22-AUG-1993 10:59pm andre@0: * 22-AUG-1993 12:59am andre@0: * 22-AUG-1993 12:59 PM andre@0: * Friday, August 04, 1995 3:54 PM andre@0: * 06/21/95 04:24:34 PM andre@0: * 20/06/95 21:07 andre@0: * 95-06-08 19:32:48 EDT andre@0: * andre@0: * If the input string doesn't contain a description of the timezone, andre@0: * we consult the `default_to_gmt' to decide whether the string should andre@0: * be interpreted relative to the local time zone (PR_FALSE) or GMT (PR_TRUE). andre@0: * The correct value for this argument depends on what standard specified andre@0: * the time string which you are parsing. andre@0: */ andre@0: andre@0: PR_IMPLEMENT(PRStatus) andre@0: PR_ParseTimeStringToExplodedTime( andre@0: const char *string, andre@0: PRBool default_to_gmt, andre@0: PRExplodedTime *result) andre@0: { andre@0: TIME_TOKEN dotw = TT_UNKNOWN; andre@0: TIME_TOKEN month = TT_UNKNOWN; andre@0: TIME_TOKEN zone = TT_UNKNOWN; andre@0: int zone_offset = -1; andre@0: int dst_offset = 0; andre@0: int date = -1; andre@0: PRInt32 year = -1; andre@0: int hour = -1; andre@0: int min = -1; andre@0: int sec = -1; andre@0: andre@0: const char *rest = string; andre@0: andre@0: int iterations = 0; andre@0: andre@0: PR_ASSERT(string && result); andre@0: if (!string || !result) return PR_FAILURE; andre@0: andre@0: while (*rest) andre@0: { andre@0: andre@0: if (iterations++ > 1000) andre@0: { andre@0: return PR_FAILURE; andre@0: } andre@0: andre@0: switch (*rest) andre@0: { andre@0: case 'a': case 'A': andre@0: if (month == TT_UNKNOWN && andre@0: (rest[1] == 'p' || rest[1] == 'P') && andre@0: (rest[2] == 'r' || rest[2] == 'R')) andre@0: month = TT_APR; andre@0: else if (zone == TT_UNKNOWN && andre@0: (rest[1] == 's' || rest[1] == 'S') && andre@0: (rest[2] == 't' || rest[2] == 'T')) andre@0: zone = TT_AST; andre@0: else if (month == TT_UNKNOWN && andre@0: (rest[1] == 'u' || rest[1] == 'U') && andre@0: (rest[2] == 'g' || rest[2] == 'G')) andre@0: month = TT_AUG; andre@0: break; andre@0: case 'b': case 'B': andre@0: if (zone == TT_UNKNOWN && andre@0: (rest[1] == 's' || rest[1] == 'S') && andre@0: (rest[2] == 't' || rest[2] == 'T')) andre@0: zone = TT_BST; andre@0: break; andre@0: case 'c': case 'C': andre@0: if (zone == TT_UNKNOWN && andre@0: (rest[1] == 'd' || rest[1] == 'D') && andre@0: (rest[2] == 't' || rest[2] == 'T')) andre@0: zone = TT_CDT; andre@0: else if (zone == TT_UNKNOWN && andre@0: (rest[1] == 's' || rest[1] == 'S') && andre@0: (rest[2] == 't' || rest[2] == 'T')) andre@0: zone = TT_CST; andre@0: break; andre@0: case 'd': case 'D': andre@0: if (month == TT_UNKNOWN && andre@0: (rest[1] == 'e' || rest[1] == 'E') && andre@0: (rest[2] == 'c' || rest[2] == 'C')) andre@0: month = TT_DEC; andre@0: break; andre@0: case 'e': case 'E': andre@0: if (zone == TT_UNKNOWN && andre@0: (rest[1] == 'd' || rest[1] == 'D') && andre@0: (rest[2] == 't' || rest[2] == 'T')) andre@0: zone = TT_EDT; andre@0: else if (zone == TT_UNKNOWN && andre@0: (rest[1] == 'e' || rest[1] == 'E') && andre@0: (rest[2] == 't' || rest[2] == 'T')) andre@0: zone = TT_EET; andre@0: else if (zone == TT_UNKNOWN && andre@0: (rest[1] == 's' || rest[1] == 'S') && andre@0: (rest[2] == 't' || rest[2] == 'T')) andre@0: zone = TT_EST; andre@0: break; andre@0: case 'f': case 'F': andre@0: if (month == TT_UNKNOWN && andre@0: (rest[1] == 'e' || rest[1] == 'E') && andre@0: (rest[2] == 'b' || rest[2] == 'B')) andre@0: month = TT_FEB; andre@0: else if (dotw == TT_UNKNOWN && andre@0: (rest[1] == 'r' || rest[1] == 'R') && andre@0: (rest[2] == 'i' || rest[2] == 'I')) andre@0: dotw = TT_FRI; andre@0: break; andre@0: case 'g': case 'G': andre@0: if (zone == TT_UNKNOWN && andre@0: (rest[1] == 'm' || rest[1] == 'M') && andre@0: (rest[2] == 't' || rest[2] == 'T')) andre@0: zone = TT_GMT; andre@0: break; andre@0: case 'j': case 'J': andre@0: if (month == TT_UNKNOWN && andre@0: (rest[1] == 'a' || rest[1] == 'A') && andre@0: (rest[2] == 'n' || rest[2] == 'N')) andre@0: month = TT_JAN; andre@0: else if (zone == TT_UNKNOWN && andre@0: (rest[1] == 's' || rest[1] == 'S') && andre@0: (rest[2] == 't' || rest[2] == 'T')) andre@0: zone = TT_JST; andre@0: else if (month == TT_UNKNOWN && andre@0: (rest[1] == 'u' || rest[1] == 'U') && andre@0: (rest[2] == 'l' || rest[2] == 'L')) andre@0: month = TT_JUL; andre@0: else if (month == TT_UNKNOWN && andre@0: (rest[1] == 'u' || rest[1] == 'U') && andre@0: (rest[2] == 'n' || rest[2] == 'N')) andre@0: month = TT_JUN; andre@0: break; andre@0: case 'm': case 'M': andre@0: if (month == TT_UNKNOWN && andre@0: (rest[1] == 'a' || rest[1] == 'A') && andre@0: (rest[2] == 'r' || rest[2] == 'R')) andre@0: month = TT_MAR; andre@0: else if (month == TT_UNKNOWN && andre@0: (rest[1] == 'a' || rest[1] == 'A') && andre@0: (rest[2] == 'y' || rest[2] == 'Y')) andre@0: month = TT_MAY; andre@0: else if (zone == TT_UNKNOWN && andre@0: (rest[1] == 'd' || rest[1] == 'D') && andre@0: (rest[2] == 't' || rest[2] == 'T')) andre@0: zone = TT_MDT; andre@0: else if (zone == TT_UNKNOWN && andre@0: (rest[1] == 'e' || rest[1] == 'E') && andre@0: (rest[2] == 't' || rest[2] == 'T')) andre@0: zone = TT_MET; andre@0: else if (dotw == TT_UNKNOWN && andre@0: (rest[1] == 'o' || rest[1] == 'O') && andre@0: (rest[2] == 'n' || rest[2] == 'N')) andre@0: dotw = TT_MON; andre@0: else if (zone == TT_UNKNOWN && andre@0: (rest[1] == 's' || rest[1] == 'S') && andre@0: (rest[2] == 't' || rest[2] == 'T')) andre@0: zone = TT_MST; andre@0: break; andre@0: case 'n': case 'N': andre@0: if (month == TT_UNKNOWN && andre@0: (rest[1] == 'o' || rest[1] == 'O') && andre@0: (rest[2] == 'v' || rest[2] == 'V')) andre@0: month = TT_NOV; andre@0: else if (zone == TT_UNKNOWN && andre@0: (rest[1] == 's' || rest[1] == 'S') && andre@0: (rest[2] == 't' || rest[2] == 'T')) andre@0: zone = TT_NST; andre@0: break; andre@0: case 'o': case 'O': andre@0: if (month == TT_UNKNOWN && andre@0: (rest[1] == 'c' || rest[1] == 'C') && andre@0: (rest[2] == 't' || rest[2] == 'T')) andre@0: month = TT_OCT; andre@0: break; andre@0: case 'p': case 'P': andre@0: if (zone == TT_UNKNOWN && andre@0: (rest[1] == 'd' || rest[1] == 'D') && andre@0: (rest[2] == 't' || rest[2] == 'T')) andre@0: zone = TT_PDT; andre@0: else if (zone == TT_UNKNOWN && andre@0: (rest[1] == 's' || rest[1] == 'S') && andre@0: (rest[2] == 't' || rest[2] == 'T')) andre@0: zone = TT_PST; andre@0: break; andre@0: case 's': case 'S': andre@0: if (dotw == TT_UNKNOWN && andre@0: (rest[1] == 'a' || rest[1] == 'A') && andre@0: (rest[2] == 't' || rest[2] == 'T')) andre@0: dotw = TT_SAT; andre@0: else if (month == TT_UNKNOWN && andre@0: (rest[1] == 'e' || rest[1] == 'E') && andre@0: (rest[2] == 'p' || rest[2] == 'P')) andre@0: month = TT_SEP; andre@0: else if (dotw == TT_UNKNOWN && andre@0: (rest[1] == 'u' || rest[1] == 'U') && andre@0: (rest[2] == 'n' || rest[2] == 'N')) andre@0: dotw = TT_SUN; andre@0: break; andre@0: case 't': case 'T': andre@0: if (dotw == TT_UNKNOWN && andre@0: (rest[1] == 'h' || rest[1] == 'H') && andre@0: (rest[2] == 'u' || rest[2] == 'U')) andre@0: dotw = TT_THU; andre@0: else if (dotw == TT_UNKNOWN && andre@0: (rest[1] == 'u' || rest[1] == 'U') && andre@0: (rest[2] == 'e' || rest[2] == 'E')) andre@0: dotw = TT_TUE; andre@0: break; andre@0: case 'u': case 'U': andre@0: if (zone == TT_UNKNOWN && andre@0: (rest[1] == 't' || rest[1] == 'T') && andre@0: !(rest[2] >= 'A' && rest[2] <= 'Z') && andre@0: !(rest[2] >= 'a' && rest[2] <= 'z')) andre@0: /* UT is the same as GMT but UTx is not. */ andre@0: zone = TT_GMT; andre@0: break; andre@0: case 'w': case 'W': andre@0: if (dotw == TT_UNKNOWN && andre@0: (rest[1] == 'e' || rest[1] == 'E') && andre@0: (rest[2] == 'd' || rest[2] == 'D')) andre@0: dotw = TT_WED; andre@0: break; andre@0: andre@0: case '+': case '-': andre@0: { andre@0: const char *end; andre@0: int sign; andre@0: if (zone_offset != -1) andre@0: { andre@0: /* already got one... */ andre@0: rest++; andre@0: break; andre@0: } andre@0: if (zone != TT_UNKNOWN && zone != TT_GMT) andre@0: { andre@0: /* GMT+0300 is legal, but PST+0300 is not. */ andre@0: rest++; andre@0: break; andre@0: } andre@0: andre@0: sign = ((*rest == '+') ? 1 : -1); andre@0: rest++; /* move over sign */ andre@0: end = rest; andre@0: while (*end >= '0' && *end <= '9') andre@0: end++; andre@0: if (rest == end) /* no digits here */ andre@0: break; andre@0: andre@0: if ((end - rest) == 4) andre@0: /* offset in HHMM */ andre@0: zone_offset = (((((rest[0]-'0')*10) + (rest[1]-'0')) * 60) + andre@0: (((rest[2]-'0')*10) + (rest[3]-'0'))); andre@0: else if ((end - rest) == 2) andre@0: /* offset in hours */ andre@0: zone_offset = (((rest[0]-'0')*10) + (rest[1]-'0')) * 60; andre@0: else if ((end - rest) == 1) andre@0: /* offset in hours */ andre@0: zone_offset = (rest[0]-'0') * 60; andre@0: else andre@0: /* 3 or >4 */ andre@0: break; andre@0: andre@0: zone_offset *= sign; andre@0: zone = TT_GMT; andre@0: break; andre@0: } andre@0: andre@0: case '0': case '1': case '2': case '3': case '4': andre@0: case '5': case '6': case '7': case '8': case '9': andre@0: { andre@0: int tmp_hour = -1; andre@0: int tmp_min = -1; andre@0: int tmp_sec = -1; andre@0: const char *end = rest + 1; andre@0: while (*end >= '0' && *end <= '9') andre@0: end++; andre@0: andre@0: /* end is now the first character after a range of digits. */ andre@0: andre@0: if (*end == ':') andre@0: { andre@0: if (hour >= 0 && min >= 0) /* already got it */ andre@0: break; andre@0: andre@0: /* We have seen "[0-9]+:", so this is probably HH:MM[:SS] */ andre@0: if ((end - rest) > 2) andre@0: /* it is [0-9][0-9][0-9]+: */ andre@0: break; andre@0: else if ((end - rest) == 2) andre@0: tmp_hour = ((rest[0]-'0')*10 + andre@0: (rest[1]-'0')); andre@0: else andre@0: tmp_hour = (rest[0]-'0'); andre@0: andre@0: /* move over the colon, and parse minutes */ andre@0: andre@0: rest = ++end; andre@0: while (*end >= '0' && *end <= '9') andre@0: end++; andre@0: andre@0: if (end == rest) andre@0: /* no digits after first colon? */ andre@0: break; andre@0: else if ((end - rest) > 2) andre@0: /* it is [0-9][0-9][0-9]+: */ andre@0: break; andre@0: else if ((end - rest) == 2) andre@0: tmp_min = ((rest[0]-'0')*10 + andre@0: (rest[1]-'0')); andre@0: else andre@0: tmp_min = (rest[0]-'0'); andre@0: andre@0: /* now go for seconds */ andre@0: rest = end; andre@0: if (*rest == ':') andre@0: rest++; andre@0: end = rest; andre@0: while (*end >= '0' && *end <= '9') andre@0: end++; andre@0: andre@0: if (end == rest) andre@0: /* no digits after second colon - that's ok. */ andre@0: ; andre@0: else if ((end - rest) > 2) andre@0: /* it is [0-9][0-9][0-9]+: */ andre@0: break; andre@0: else if ((end - rest) == 2) andre@0: tmp_sec = ((rest[0]-'0')*10 + andre@0: (rest[1]-'0')); andre@0: else andre@0: tmp_sec = (rest[0]-'0'); andre@0: andre@0: /* If we made it here, we've parsed hour and min, andre@0: and possibly sec, so it worked as a unit. */ andre@0: andre@0: /* skip over whitespace and see if there's an AM or PM andre@0: directly following the time. andre@0: */ andre@0: if (tmp_hour <= 12) andre@0: { andre@0: const char *s = end; andre@0: while (*s && (*s == ' ' || *s == '\t')) andre@0: s++; andre@0: if ((s[0] == 'p' || s[0] == 'P') && andre@0: (s[1] == 'm' || s[1] == 'M')) andre@0: /* 10:05pm == 22:05, and 12:05pm == 12:05 */ andre@0: tmp_hour = (tmp_hour == 12 ? 12 : tmp_hour + 12); andre@0: else if (tmp_hour == 12 && andre@0: (s[0] == 'a' || s[0] == 'A') && andre@0: (s[1] == 'm' || s[1] == 'M')) andre@0: /* 12:05am == 00:05 */ andre@0: tmp_hour = 0; andre@0: } andre@0: andre@0: hour = tmp_hour; andre@0: min = tmp_min; andre@0: sec = tmp_sec; andre@0: rest = end; andre@0: break; andre@0: } andre@0: else if ((*end == '/' || *end == '-') && andre@0: end[1] >= '0' && end[1] <= '9') andre@0: { andre@0: /* Perhaps this is 6/16/95, 16/6/95, 6-16-95, or 16-6-95 andre@0: or even 95-06-05... andre@0: #### But it doesn't handle 1995-06-22. andre@0: */ andre@0: int n1, n2, n3; andre@0: const char *s; andre@0: andre@0: if (month != TT_UNKNOWN) andre@0: /* if we saw a month name, this can't be. */ andre@0: break; andre@0: andre@0: s = rest; andre@0: andre@0: n1 = (*s++ - '0'); /* first 1 or 2 digits */ andre@0: if (*s >= '0' && *s <= '9') andre@0: n1 = n1*10 + (*s++ - '0'); andre@0: andre@0: if (*s != '/' && *s != '-') /* slash */ andre@0: break; andre@0: s++; andre@0: andre@0: if (*s < '0' || *s > '9') /* second 1 or 2 digits */ andre@0: break; andre@0: n2 = (*s++ - '0'); andre@0: if (*s >= '0' && *s <= '9') andre@0: n2 = n2*10 + (*s++ - '0'); andre@0: andre@0: if (*s != '/' && *s != '-') /* slash */ andre@0: break; andre@0: s++; andre@0: andre@0: if (*s < '0' || *s > '9') /* third 1, 2, 4, or 5 digits */ andre@0: break; andre@0: n3 = (*s++ - '0'); andre@0: if (*s >= '0' && *s <= '9') andre@0: n3 = n3*10 + (*s++ - '0'); andre@0: andre@0: if (*s >= '0' && *s <= '9') /* optional digits 3, 4, and 5 */ andre@0: { andre@0: n3 = n3*10 + (*s++ - '0'); andre@0: if (*s < '0' || *s > '9') andre@0: break; andre@0: n3 = n3*10 + (*s++ - '0'); andre@0: if (*s >= '0' && *s <= '9') andre@0: n3 = n3*10 + (*s++ - '0'); andre@0: } andre@0: andre@0: if ((*s >= '0' && *s <= '9') || /* followed by non-alphanum */ andre@0: (*s >= 'A' && *s <= 'Z') || andre@0: (*s >= 'a' && *s <= 'z')) andre@0: break; andre@0: andre@0: /* Ok, we parsed three 1-2 digit numbers, with / or - andre@0: between them. Now decide what the hell they are andre@0: (DD/MM/YY or MM/DD/YY or YY/MM/DD.) andre@0: */ andre@0: andre@0: if (n1 > 31 || n1 == 0) /* must be YY/MM/DD */ andre@0: { andre@0: if (n2 > 12) break; andre@0: if (n3 > 31) break; andre@0: year = n1; andre@0: if (year < 70) andre@0: year += 2000; andre@0: else if (year < 100) andre@0: year += 1900; andre@0: month = (TIME_TOKEN)(n2 + ((int)TT_JAN) - 1); andre@0: date = n3; andre@0: rest = s; andre@0: break; andre@0: } andre@0: andre@0: if (n1 > 12 && n2 > 12) /* illegal */ andre@0: { andre@0: rest = s; andre@0: break; andre@0: } andre@0: andre@0: if (n3 < 70) andre@0: n3 += 2000; andre@0: else if (n3 < 100) andre@0: n3 += 1900; andre@0: andre@0: if (n1 > 12) /* must be DD/MM/YY */ andre@0: { andre@0: date = n1; andre@0: month = (TIME_TOKEN)(n2 + ((int)TT_JAN) - 1); andre@0: year = n3; andre@0: } andre@0: else /* assume MM/DD/YY */ andre@0: { andre@0: /* #### In the ambiguous case, should we consult the andre@0: locale to find out the local default? */ andre@0: month = (TIME_TOKEN)(n1 + ((int)TT_JAN) - 1); andre@0: date = n2; andre@0: year = n3; andre@0: } andre@0: rest = s; andre@0: } andre@0: else if ((*end >= 'A' && *end <= 'Z') || andre@0: (*end >= 'a' && *end <= 'z')) andre@0: /* Digits followed by non-punctuation - what's that? */ andre@0: ; andre@0: else if ((end - rest) == 5) /* five digits is a year */ andre@0: year = (year < 0 andre@0: ? ((rest[0]-'0')*10000L + andre@0: (rest[1]-'0')*1000L + andre@0: (rest[2]-'0')*100L + andre@0: (rest[3]-'0')*10L + andre@0: (rest[4]-'0')) andre@0: : year); andre@0: else if ((end - rest) == 4) /* four digits is a year */ andre@0: year = (year < 0 andre@0: ? ((rest[0]-'0')*1000L + andre@0: (rest[1]-'0')*100L + andre@0: (rest[2]-'0')*10L + andre@0: (rest[3]-'0')) andre@0: : year); andre@0: else if ((end - rest) == 2) /* two digits - date or year */ andre@0: { andre@0: int n = ((rest[0]-'0')*10 + andre@0: (rest[1]-'0')); andre@0: /* If we don't have a date (day of the month) and we see a number andre@0: less than 32, then assume that is the date. andre@0: andre@0: Otherwise, if we have a date and not a year, assume this is the andre@0: year. If it is less than 70, then assume it refers to the 21st andre@0: century. If it is two digits (>= 70), assume it refers to this andre@0: century. Otherwise, assume it refers to an unambiguous year. andre@0: andre@0: The world will surely end soon. andre@0: */ andre@0: if (date < 0 && n < 32) andre@0: date = n; andre@0: else if (year < 0) andre@0: { andre@0: if (n < 70) andre@0: year = 2000 + n; andre@0: else if (n < 100) andre@0: year = 1900 + n; andre@0: else andre@0: year = n; andre@0: } andre@0: /* else what the hell is this. */ andre@0: } andre@0: else if ((end - rest) == 1) /* one digit - date */ andre@0: date = (date < 0 ? (rest[0]-'0') : date); andre@0: /* else, three or more than five digits - what's that? */ andre@0: andre@0: break; andre@0: } andre@0: } andre@0: andre@0: /* Skip to the end of this token, whether we parsed it or not. andre@0: Tokens are delimited by whitespace, or ,;-/ andre@0: But explicitly not :+-. andre@0: */ andre@0: while (*rest && andre@0: *rest != ' ' && *rest != '\t' && andre@0: *rest != ',' && *rest != ';' && andre@0: *rest != '-' && *rest != '+' && andre@0: *rest != '/' && andre@0: *rest != '(' && *rest != ')' && *rest != '[' && *rest != ']') andre@0: rest++; andre@0: /* skip over uninteresting chars. */ andre@0: SKIP_MORE: andre@0: while (*rest && andre@0: (*rest == ' ' || *rest == '\t' || andre@0: *rest == ',' || *rest == ';' || *rest == '/' || andre@0: *rest == '(' || *rest == ')' || *rest == '[' || *rest == ']')) andre@0: rest++; andre@0: andre@0: /* "-" is ignored at the beginning of a token if we have not yet andre@0: parsed a year (e.g., the second "-" in "30-AUG-1966"), or if andre@0: the character after the dash is not a digit. */ andre@0: if (*rest == '-' && ((rest > string && andre@0: isalpha((unsigned char)rest[-1]) && year < 0) || andre@0: rest[1] < '0' || rest[1] > '9')) andre@0: { andre@0: rest++; andre@0: goto SKIP_MORE; andre@0: } andre@0: andre@0: } andre@0: andre@0: if (zone != TT_UNKNOWN && zone_offset == -1) andre@0: { andre@0: switch (zone) andre@0: { andre@0: case TT_PST: zone_offset = -8 * 60; break; andre@0: case TT_PDT: zone_offset = -8 * 60; dst_offset = 1 * 60; break; andre@0: case TT_MST: zone_offset = -7 * 60; break; andre@0: case TT_MDT: zone_offset = -7 * 60; dst_offset = 1 * 60; break; andre@0: case TT_CST: zone_offset = -6 * 60; break; andre@0: case TT_CDT: zone_offset = -6 * 60; dst_offset = 1 * 60; break; andre@0: case TT_EST: zone_offset = -5 * 60; break; andre@0: case TT_EDT: zone_offset = -5 * 60; dst_offset = 1 * 60; break; andre@0: case TT_AST: zone_offset = -4 * 60; break; andre@0: case TT_NST: zone_offset = -3 * 60 - 30; break; andre@0: case TT_GMT: zone_offset = 0 * 60; break; andre@0: case TT_BST: zone_offset = 0 * 60; dst_offset = 1 * 60; break; andre@0: case TT_MET: zone_offset = 1 * 60; break; andre@0: case TT_EET: zone_offset = 2 * 60; break; andre@0: case TT_JST: zone_offset = 9 * 60; break; andre@0: default: andre@0: PR_ASSERT (0); andre@0: break; andre@0: } andre@0: } andre@0: andre@0: /* If we didn't find a year, month, or day-of-the-month, we can't andre@0: possibly parse this, and in fact, mktime() will do something random andre@0: (I'm seeing it return "Tue Feb 5 06:28:16 2036", which is no doubt andre@0: a numerologically significant date... */ andre@0: if (month == TT_UNKNOWN || date == -1 || year == -1 || year > PR_INT16_MAX) andre@0: return PR_FAILURE; andre@0: andre@0: memset(result, 0, sizeof(*result)); andre@0: if (sec != -1) andre@0: result->tm_sec = sec; andre@0: if (min != -1) andre@0: result->tm_min = min; andre@0: if (hour != -1) andre@0: result->tm_hour = hour; andre@0: if (date != -1) andre@0: result->tm_mday = date; andre@0: if (month != TT_UNKNOWN) andre@0: result->tm_month = (((int)month) - ((int)TT_JAN)); andre@0: if (year != -1) andre@0: result->tm_year = year; andre@0: if (dotw != TT_UNKNOWN) andre@0: result->tm_wday = (((int)dotw) - ((int)TT_SUN)); andre@0: /* andre@0: * Mainly to compute wday and yday, but normalized time is also required andre@0: * by the check below that works around a Visual C++ 2005 mktime problem. andre@0: */ andre@0: PR_NormalizeTime(result, PR_GMTParameters); andre@0: /* The remaining work is to set the gmt and dst offsets in tm_params. */ andre@0: andre@0: if (zone == TT_UNKNOWN && default_to_gmt) andre@0: { andre@0: /* No zone was specified, so pretend the zone was GMT. */ andre@0: zone = TT_GMT; andre@0: zone_offset = 0; andre@0: } andre@0: andre@0: if (zone_offset == -1) andre@0: { andre@0: /* no zone was specified, and we're to assume that everything andre@0: is local. */ andre@0: struct tm localTime; andre@0: time_t secs; andre@0: andre@0: PR_ASSERT(result->tm_month > -1 && andre@0: result->tm_mday > 0 && andre@0: result->tm_hour > -1 && andre@0: result->tm_min > -1 && andre@0: result->tm_sec > -1); andre@0: andre@0: /* andre@0: * To obtain time_t from a tm structure representing the local andre@0: * time, we call mktime(). However, we need to see if we are andre@0: * on 1-Jan-1970 or before. If we are, we can't call mktime() andre@0: * because mktime() will crash on win16. In that case, we andre@0: * calculate zone_offset based on the zone offset at andre@0: * 00:00:00, 2 Jan 1970 GMT, and subtract zone_offset from the andre@0: * date we are parsing to transform the date to GMT. We also andre@0: * do so if mktime() returns (time_t) -1 (time out of range). andre@0: */ andre@0: andre@0: /* month, day, hours, mins and secs are always non-negative andre@0: so we dont need to worry about them. */ andre@0: if(result->tm_year >= 1970) andre@0: { andre@0: PRInt64 usec_per_sec; andre@0: andre@0: localTime.tm_sec = result->tm_sec; andre@0: localTime.tm_min = result->tm_min; andre@0: localTime.tm_hour = result->tm_hour; andre@0: localTime.tm_mday = result->tm_mday; andre@0: localTime.tm_mon = result->tm_month; andre@0: localTime.tm_year = result->tm_year - 1900; andre@0: /* Set this to -1 to tell mktime "I don't care". If you set andre@0: it to 0 or 1, you are making assertions about whether the andre@0: date you are handing it is in daylight savings mode or not; andre@0: and if you're wrong, it will "fix" it for you. */ andre@0: localTime.tm_isdst = -1; andre@0: andre@0: #if _MSC_VER == 1400 /* 1400 = Visual C++ 2005 (8.0) */ andre@0: /* andre@0: * mktime will return (time_t) -1 if the input is a date andre@0: * after 23:59:59, December 31, 3000, US Pacific Time (not andre@0: * UTC as documented): andre@0: * http://msdn.microsoft.com/en-us/library/d1y53h2a(VS.80).aspx andre@0: * But if the year is 3001, mktime also invokes the invalid andre@0: * parameter handler, causing the application to crash. This andre@0: * problem has been reported in andre@0: * http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=266036. andre@0: * We avoid this crash by not calling mktime if the date is andre@0: * out of range. To use a simple test that works in any time andre@0: * zone, we consider year 3000 out of range as well. (See andre@0: * bug 480740.) andre@0: */ andre@0: if (result->tm_year >= 3000) { andre@0: /* Emulate what mktime would have done. */ andre@0: errno = EINVAL; andre@0: secs = (time_t) -1; andre@0: } else { andre@0: secs = mktime(&localTime); andre@0: } andre@0: #else andre@0: secs = mktime(&localTime); andre@0: #endif andre@0: if (secs != (time_t) -1) andre@0: { andre@0: PRTime usecs64; andre@0: LL_I2L(usecs64, secs); andre@0: LL_I2L(usec_per_sec, PR_USEC_PER_SEC); andre@0: LL_MUL(usecs64, usecs64, usec_per_sec); andre@0: PR_ExplodeTime(usecs64, PR_LocalTimeParameters, result); andre@0: return PR_SUCCESS; andre@0: } andre@0: } andre@0: andre@0: /* So mktime() can't handle this case. We assume the andre@0: zone_offset for the date we are parsing is the same as andre@0: the zone offset on 00:00:00 2 Jan 1970 GMT. */ andre@0: secs = 86400; andre@0: (void) MT_safe_localtime(&secs, &localTime); andre@0: zone_offset = localTime.tm_min andre@0: + 60 * localTime.tm_hour andre@0: + 1440 * (localTime.tm_mday - 2); andre@0: } andre@0: andre@0: result->tm_params.tp_gmt_offset = zone_offset * 60; andre@0: result->tm_params.tp_dst_offset = dst_offset * 60; andre@0: andre@0: return PR_SUCCESS; andre@0: } andre@0: andre@0: PR_IMPLEMENT(PRStatus) andre@0: PR_ParseTimeString( andre@0: const char *string, andre@0: PRBool default_to_gmt, andre@0: PRTime *result) andre@0: { andre@0: PRExplodedTime tm; andre@0: PRStatus rv; andre@0: andre@0: rv = PR_ParseTimeStringToExplodedTime(string, andre@0: default_to_gmt, andre@0: &tm); andre@0: if (rv != PR_SUCCESS) andre@0: return rv; andre@0: andre@0: *result = PR_ImplodeTime(&tm); andre@0: andre@0: return PR_SUCCESS; andre@0: } andre@0: andre@0: /* andre@0: ******************************************************************* andre@0: ******************************************************************* andre@0: ** andre@0: ** OLD COMPATIBILITY FUNCTIONS andre@0: ** andre@0: ******************************************************************* andre@0: ******************************************************************* andre@0: */ andre@0: andre@0: andre@0: /* andre@0: *----------------------------------------------------------------------- andre@0: * andre@0: * PR_FormatTime -- andre@0: * andre@0: * Format a time value into a buffer. Same semantics as strftime(). andre@0: * andre@0: *----------------------------------------------------------------------- andre@0: */ andre@0: andre@0: PR_IMPLEMENT(PRUint32) andre@0: PR_FormatTime(char *buf, int buflen, const char *fmt, const PRExplodedTime *tm) andre@0: { andre@0: size_t rv; andre@0: struct tm a; andre@0: struct tm *ap; andre@0: andre@0: if (tm) { andre@0: ap = &a; andre@0: a.tm_sec = tm->tm_sec; andre@0: a.tm_min = tm->tm_min; andre@0: a.tm_hour = tm->tm_hour; andre@0: a.tm_mday = tm->tm_mday; andre@0: a.tm_mon = tm->tm_month; andre@0: a.tm_wday = tm->tm_wday; andre@0: a.tm_year = tm->tm_year - 1900; andre@0: a.tm_yday = tm->tm_yday; andre@0: a.tm_isdst = tm->tm_params.tp_dst_offset ? 1 : 0; andre@0: andre@0: /* andre@0: * On some platforms, for example SunOS 4, struct tm has two andre@0: * additional fields: tm_zone and tm_gmtoff. andre@0: */ andre@0: andre@0: #if (__GLIBC__ >= 2) || defined(XP_BEOS) \ andre@0: || defined(NETBSD) || defined(OPENBSD) || defined(FREEBSD) \ andre@0: || defined(DARWIN) || defined(SYMBIAN) || defined(ANDROID) andre@0: a.tm_zone = NULL; andre@0: a.tm_gmtoff = tm->tm_params.tp_gmt_offset + andre@0: tm->tm_params.tp_dst_offset; andre@0: #endif andre@0: } else { andre@0: ap = NULL; andre@0: } andre@0: andre@0: rv = strftime(buf, buflen, fmt, ap); andre@0: if (!rv && buf && buflen > 0) { andre@0: /* andre@0: * When strftime fails, the contents of buf are indeterminate. andre@0: * Some callers don't check the return value from this function, andre@0: * so store an empty string in buf in case they try to print it. andre@0: */ andre@0: buf[0] = '\0'; andre@0: } andre@0: return rv; andre@0: } andre@0: andre@0: andre@0: /* andre@0: * The following string arrays and macros are used by PR_FormatTimeUSEnglish(). andre@0: */ andre@0: andre@0: static const char* abbrevDays[] = andre@0: { andre@0: "Sun","Mon","Tue","Wed","Thu","Fri","Sat" andre@0: }; andre@0: andre@0: static const char* days[] = andre@0: { andre@0: "Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday" andre@0: }; andre@0: andre@0: static const char* abbrevMonths[] = andre@0: { andre@0: "Jan", "Feb", "Mar", "Apr", "May", "Jun", andre@0: "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" andre@0: }; andre@0: andre@0: static const char* months[] = andre@0: { andre@0: "January", "February", "March", "April", "May", "June", andre@0: "July", "August", "September", "October", "November", "December" andre@0: }; andre@0: andre@0: andre@0: /* andre@0: * Add a single character to the given buffer, incrementing the buffer pointer andre@0: * and decrementing the buffer size. Return 0 on error. andre@0: */ andre@0: #define ADDCHAR( buf, bufSize, ch ) \ andre@0: do \ andre@0: { \ andre@0: if( bufSize < 1 ) \ andre@0: { \ andre@0: *(--buf) = '\0'; \ andre@0: return 0; \ andre@0: } \ andre@0: *buf++ = ch; \ andre@0: bufSize--; \ andre@0: } \ andre@0: while(0) andre@0: andre@0: andre@0: /* andre@0: * Add a string to the given buffer, incrementing the buffer pointer andre@0: * and decrementing the buffer size appropriately. Return 0 on error. andre@0: */ andre@0: #define ADDSTR( buf, bufSize, str ) \ andre@0: do \ andre@0: { \ andre@0: PRUint32 strSize = strlen( str ); \ andre@0: if( strSize > bufSize ) \ andre@0: { \ andre@0: if( bufSize==0 ) \ andre@0: *(--buf) = '\0'; \ andre@0: else \ andre@0: *buf = '\0'; \ andre@0: return 0; \ andre@0: } \ andre@0: memcpy(buf, str, strSize); \ andre@0: buf += strSize; \ andre@0: bufSize -= strSize; \ andre@0: } \ andre@0: while(0) andre@0: andre@0: /* Needed by PR_FormatTimeUSEnglish() */ andre@0: static unsigned int pr_WeekOfYear(const PRExplodedTime* time, andre@0: unsigned int firstDayOfWeek); andre@0: andre@0: andre@0: /*********************************************************************************** andre@0: * andre@0: * Description: andre@0: * This is a dumbed down version of strftime that will format the date in US andre@0: * English regardless of the setting of the global locale. This functionality is andre@0: * needed to write things like MIME headers which must always be in US English. andre@0: * andre@0: **********************************************************************************/ andre@0: andre@0: PR_IMPLEMENT(PRUint32) andre@0: PR_FormatTimeUSEnglish( char* buf, PRUint32 bufSize, andre@0: const char* format, const PRExplodedTime* time ) andre@0: { andre@0: char* bufPtr = buf; andre@0: const char* fmtPtr; andre@0: char tmpBuf[ 40 ]; andre@0: const int tmpBufSize = sizeof( tmpBuf ); andre@0: andre@0: andre@0: for( fmtPtr=format; *fmtPtr != '\0'; fmtPtr++ ) andre@0: { andre@0: if( *fmtPtr != '%' ) andre@0: { andre@0: ADDCHAR( bufPtr, bufSize, *fmtPtr ); andre@0: } andre@0: else andre@0: { andre@0: switch( *(++fmtPtr) ) andre@0: { andre@0: case '%': andre@0: /* escaped '%' character */ andre@0: ADDCHAR( bufPtr, bufSize, '%' ); andre@0: break; andre@0: andre@0: case 'a': andre@0: /* abbreviated weekday name */ andre@0: ADDSTR( bufPtr, bufSize, abbrevDays[ time->tm_wday ] ); andre@0: break; andre@0: andre@0: case 'A': andre@0: /* full weekday name */ andre@0: ADDSTR( bufPtr, bufSize, days[ time->tm_wday ] ); andre@0: break; andre@0: andre@0: case 'b': andre@0: /* abbreviated month name */ andre@0: ADDSTR( bufPtr, bufSize, abbrevMonths[ time->tm_month ] ); andre@0: break; andre@0: andre@0: case 'B': andre@0: /* full month name */ andre@0: ADDSTR(bufPtr, bufSize, months[ time->tm_month ] ); andre@0: break; andre@0: andre@0: case 'c': andre@0: /* Date and time. */ andre@0: PR_FormatTimeUSEnglish( tmpBuf, tmpBufSize, "%a %b %d %H:%M:%S %Y", time ); andre@0: ADDSTR( bufPtr, bufSize, tmpBuf ); andre@0: break; andre@0: andre@0: case 'd': andre@0: /* day of month ( 01 - 31 ) */ andre@0: PR_snprintf(tmpBuf,tmpBufSize,"%.2ld",time->tm_mday ); andre@0: ADDSTR( bufPtr, bufSize, tmpBuf ); andre@0: break; andre@0: andre@0: case 'H': andre@0: /* hour ( 00 - 23 ) */ andre@0: PR_snprintf(tmpBuf,tmpBufSize,"%.2ld",time->tm_hour ); andre@0: ADDSTR( bufPtr, bufSize, tmpBuf ); andre@0: break; andre@0: andre@0: case 'I': andre@0: /* hour ( 01 - 12 ) */ andre@0: PR_snprintf(tmpBuf,tmpBufSize,"%.2ld", andre@0: (time->tm_hour%12) ? time->tm_hour%12 : (PRInt32) 12 ); andre@0: ADDSTR( bufPtr, bufSize, tmpBuf ); andre@0: break; andre@0: andre@0: case 'j': andre@0: /* day number of year ( 001 - 366 ) */ andre@0: PR_snprintf(tmpBuf,tmpBufSize,"%.3d",time->tm_yday + 1); andre@0: ADDSTR( bufPtr, bufSize, tmpBuf ); andre@0: break; andre@0: andre@0: case 'm': andre@0: /* month number ( 01 - 12 ) */ andre@0: PR_snprintf(tmpBuf,tmpBufSize,"%.2ld",time->tm_month+1); andre@0: ADDSTR( bufPtr, bufSize, tmpBuf ); andre@0: break; andre@0: andre@0: case 'M': andre@0: /* minute ( 00 - 59 ) */ andre@0: PR_snprintf(tmpBuf,tmpBufSize,"%.2ld",time->tm_min ); andre@0: ADDSTR( bufPtr, bufSize, tmpBuf ); andre@0: break; andre@0: andre@0: case 'p': andre@0: /* locale's equivalent of either AM or PM */ andre@0: ADDSTR( bufPtr, bufSize, (time->tm_hour<12)?"AM":"PM" ); andre@0: break; andre@0: andre@0: case 'S': andre@0: /* seconds ( 00 - 61 ), allows for leap seconds */ andre@0: PR_snprintf(tmpBuf,tmpBufSize,"%.2ld",time->tm_sec ); andre@0: ADDSTR( bufPtr, bufSize, tmpBuf ); andre@0: break; andre@0: andre@0: case 'U': andre@0: /* week number of year ( 00 - 53 ), Sunday is the first day of week 1 */ andre@0: PR_snprintf(tmpBuf,tmpBufSize,"%.2d", pr_WeekOfYear( time, 0 ) ); andre@0: ADDSTR( bufPtr, bufSize, tmpBuf ); andre@0: break; andre@0: andre@0: case 'w': andre@0: /* weekday number ( 0 - 6 ), Sunday = 0 */ andre@0: PR_snprintf(tmpBuf,tmpBufSize,"%d",time->tm_wday ); andre@0: ADDSTR( bufPtr, bufSize, tmpBuf ); andre@0: break; andre@0: andre@0: case 'W': andre@0: /* Week number of year ( 00 - 53 ), Monday is the first day of week 1 */ andre@0: PR_snprintf(tmpBuf,tmpBufSize,"%.2d", pr_WeekOfYear( time, 1 ) ); andre@0: ADDSTR( bufPtr, bufSize, tmpBuf ); andre@0: break; andre@0: andre@0: case 'x': andre@0: /* Date representation */ andre@0: PR_FormatTimeUSEnglish( tmpBuf, tmpBufSize, "%m/%d/%y", time ); andre@0: ADDSTR( bufPtr, bufSize, tmpBuf ); andre@0: break; andre@0: andre@0: case 'X': andre@0: /* Time representation. */ andre@0: PR_FormatTimeUSEnglish( tmpBuf, tmpBufSize, "%H:%M:%S", time ); andre@0: ADDSTR( bufPtr, bufSize, tmpBuf ); andre@0: break; andre@0: andre@0: case 'y': andre@0: /* year within century ( 00 - 99 ) */ andre@0: PR_snprintf(tmpBuf,tmpBufSize,"%.2d",time->tm_year % 100 ); andre@0: ADDSTR( bufPtr, bufSize, tmpBuf ); andre@0: break; andre@0: andre@0: case 'Y': andre@0: /* year as ccyy ( for example 1986 ) */ andre@0: PR_snprintf(tmpBuf,tmpBufSize,"%.4d",time->tm_year ); andre@0: ADDSTR( bufPtr, bufSize, tmpBuf ); andre@0: break; andre@0: andre@0: case 'Z': andre@0: /* Time zone name or no characters if no time zone exists. andre@0: * Since time zone name is supposed to be independant of locale, we andre@0: * defer to PR_FormatTime() for this option. andre@0: */ andre@0: PR_FormatTime( tmpBuf, tmpBufSize, "%Z", time ); andre@0: ADDSTR( bufPtr, bufSize, tmpBuf ); andre@0: break; andre@0: andre@0: default: andre@0: /* Unknown format. Simply copy format into output buffer. */ andre@0: ADDCHAR( bufPtr, bufSize, '%' ); andre@0: ADDCHAR( bufPtr, bufSize, *fmtPtr ); andre@0: break; andre@0: andre@0: } andre@0: } andre@0: } andre@0: andre@0: ADDCHAR( bufPtr, bufSize, '\0' ); andre@0: return (PRUint32)(bufPtr - buf - 1); andre@0: } andre@0: andre@0: andre@0: andre@0: /*********************************************************************************** andre@0: * andre@0: * Description: andre@0: * Returns the week number of the year (0-53) for the given time. firstDayOfWeek andre@0: * is the day on which the week is considered to start (0=Sun, 1=Mon, ...). andre@0: * Week 1 starts the first time firstDayOfWeek occurs in the year. In other words, andre@0: * a partial week at the start of the year is considered week 0. andre@0: * andre@0: **********************************************************************************/ andre@0: andre@0: static unsigned int andre@0: pr_WeekOfYear(const PRExplodedTime* time, unsigned int firstDayOfWeek) andre@0: { andre@0: int dayOfWeek; andre@0: int dayOfYear; andre@0: andre@0: /* Get the day of the year for the given time then adjust it to represent the andre@0: * first day of the week containing the given time. andre@0: */ andre@0: dayOfWeek = time->tm_wday - firstDayOfWeek; andre@0: if (dayOfWeek < 0) andre@0: dayOfWeek += 7; andre@0: andre@0: dayOfYear = time->tm_yday - dayOfWeek; andre@0: andre@0: andre@0: if( dayOfYear <= 0 ) andre@0: { andre@0: /* If dayOfYear is <= 0, it is in the first partial week of the year. */ andre@0: return 0; andre@0: } andre@0: else andre@0: { andre@0: /* Count the number of full weeks ( dayOfYear / 7 ) then add a week if there andre@0: * are any days left over ( dayOfYear % 7 ). Because we are only counting to andre@0: * the first day of the week containing the given time, rather than to the andre@0: * actual day representing the given time, any days in week 0 will be "absorbed" andre@0: * as extra days in the given week. andre@0: */ andre@0: return (dayOfYear / 7) + ( (dayOfYear % 7) == 0 ? 0 : 1 ); andre@0: } andre@0: } andre@0: