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