From 4747d02fa99bced9e99215dbf08b0e1d048945f4 Mon Sep 17 00:00:00 2001 From: mccabe Date: Thu, 30 Apr 1998 03:51:26 +0000 Subject: [PATCH] Changed pr[mj]time.c to just return 0 for failure in FormatTime, and changed jsdate.c to detect failure and default to using toString in place of toLocaleString whenever FormatTime fails. git-svn-id: svn://10.0.0.236/trunk@895 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/js/ref/jsdate.c | 81 +++++++++++++++++++++------------------ mozilla/js/ref/prtime.c | 17 ++++---- mozilla/js/src/jsdate.c | 81 +++++++++++++++++++++------------------ mozilla/js/src/prmjtime.c | 17 ++++---- 4 files changed, 106 insertions(+), 90 deletions(-) diff --git a/mozilla/js/ref/jsdate.c b/mozilla/js/ref/jsdate.c index fffc65b262f..4e0ea1b7576 100644 --- a/mozilla/js/ref/jsdate.c +++ b/mozilla/js/ref/jsdate.c @@ -1408,7 +1408,7 @@ new_explode(jsdouble time, PRTime *split, JSBool findEquivalent) * to some equivalent year in the range 0 to 2800. Borrowed from * A. D. Olsen. */ - int cycles; + jsint cycles; #define CYCLE_YEARS 2800L cycles = (year >= 0) ? year / CYCLE_YEARS : -1 - (-1 - year) / CYCLE_YEARS; @@ -1436,43 +1436,6 @@ new_explode(jsdouble time, PRTime *split, JSBool findEquivalent) split->tm_isdst = (DaylightSavingTA(time) != 0); } -static JSBool -date_toLocaleString(JSContext *cx, JSObject *obj, uintN argc, - jsval *argv, jsval *rval) -{ - char buf[100]; - JSString *str; - PRTime split; - jsdouble *date = date_getProlog(cx, obj, argv); - if (!date) - return JS_FALSE; - - if (!JSDOUBLE_IS_FINITE(*date)) { - PR_snprintf(buf, sizeof buf, js_NaN_date_str); - } else { - jsdouble local = LocalTime(*date); - new_explode(local, &split, JS_FALSE); - - /* let PRTime format it. Use '%#c' for windows, because '%c' is - * backward-compatible and non-y2k with msvc; '%#c' requests that a - * full year be used in the result string. - */ - PR_FormatTime(buf, sizeof buf, -#ifdef _WIN32 - "%#c", -#else - "%c", -#endif - &split); - } - - str = JS_NewStringCopyZ(cx, buf); - if (!str) - return JS_FALSE; - *rval = STRING_TO_JSVAL(str); - return JS_TRUE; -} - /* helper function */ static JSBool date_format(JSContext *cx, jsdouble date, jsval *rval) @@ -1536,6 +1499,48 @@ date_format(JSContext *cx, jsdouble date, jsval *rval) return JS_TRUE; } +static JSBool +date_toLocaleString(JSContext *cx, JSObject *obj, uintN argc, + jsval *argv, jsval *rval) +{ + char buf[100]; + JSString *str; + PRTime split; + jsdouble *date = date_getProlog(cx, obj, argv); + if (!date) + return JS_FALSE; + + if (!JSDOUBLE_IS_FINITE(*date)) { + PR_snprintf(buf, sizeof buf, js_NaN_date_str); + } else { + intN result_len; + jsdouble local = LocalTime(*date); + new_explode(local, &split, JS_FALSE); + + /* let PRTime format it. Use '%#c' for windows, because '%c' is + * backward-compatible and non-y2k with msvc; '%#c' requests that a + * full year be used in the result string. + */ + result_len = PR_FormatTime(buf, sizeof buf, +#ifdef _WIN32 + "%#c", +#else + "%c", +#endif + &split); + + /* If it failed, default to toString. */ + if (result_len == 0) + return date_format(cx, *date, rval); + } + + str = JS_NewStringCopyZ(cx, buf); + if (!str) + return JS_FALSE; + *rval = STRING_TO_JSVAL(str); + return JS_TRUE; +} + #if JS_HAS_TOSOURCE #include "prdtoa.h" diff --git a/mozilla/js/ref/prtime.c b/mozilla/js/ref/prtime.c index 1b095f3f8d0..cf6f92f8844 100644 --- a/mozilla/js/ref/prtime.c +++ b/mozilla/js/ref/prtime.c @@ -232,10 +232,6 @@ PR_LocalGMTDifference() #define G2037GMTMICROHI 0x00e45fab /* micro secs to 2037 high */ #define G2037GMTMICROLOW 0x7a238000 /* micro secs to 2037 low */ -/* Convert from extended time to base time (time since Jan 1 1970) it - * truncates dates if time is before 1970 and after 2037. - */ - /* Convert from base time to extended time */ static int64 PR_ToExtendedTime(int32 time) @@ -364,10 +360,10 @@ PR_DSTOffset(int64 time) int32 diff; int64 maxtimet; struct tm tm; + PRTime prtm; #if defined( XP_PC ) || defined( FREEBSD ) || defined ( HPUX9 ) struct tm *ptm; #endif - PRTime prtm; LL_UI2L(us2s, PR_USEC_PER_SEC); LL_DIV(time, time, us2s); @@ -446,12 +442,19 @@ PR_FormatTime(char *buf, int buflen, char *fmt, PRTime *prtm) * Still not sure if MKLINUX is necessary; this is borrowed from the NSPR20 * prtime.c. I'm leaving it out - My Linux does the right thing without it * (and the wrong thing with it) even though it has the tm_gmtoff, tm_zone - * fields. + * fields. Linux seems to be happy so long as the tm struct is zeroed out. + * The #ifdef in nspr is: + * #if defined(SUNOS4) || defined(MKLINUX) || defined (__GLIBC >= 2) */ #if defined(SUNOS4) if (mktime(&a) == -1) { - PR_snprintf(buf, buflen, "can't get timezone"); + /* Seems to fail whenever the requested date is outside of the 32-bit + * UNIX epoch. We could proceed at this point (setting a.tm_zone to + * "") but then strftime returns a string with a 2-digit field of + * garbage for the year. So we return 0 and hope jsdate.c + * will fall back on toString. + */ return 0; } #endif diff --git a/mozilla/js/src/jsdate.c b/mozilla/js/src/jsdate.c index 415ab36a845..f513d90ad8d 100644 --- a/mozilla/js/src/jsdate.c +++ b/mozilla/js/src/jsdate.c @@ -1408,7 +1408,7 @@ new_explode(jsdouble time, PRMJTime *split, JSBool findEquivalent) * to some equivalent year in the range 0 to 2800. Borrowed from * A. D. Olsen. */ - int cycles; + jsint cycles; #define CYCLE_YEARS 2800L cycles = (year >= 0) ? year / CYCLE_YEARS : -1 - (-1 - year) / CYCLE_YEARS; @@ -1436,43 +1436,6 @@ new_explode(jsdouble time, PRMJTime *split, JSBool findEquivalent) split->tm_isdst = (DaylightSavingTA(time) != 0); } -static JSBool -date_toLocaleString(JSContext *cx, JSObject *obj, uintN argc, - jsval *argv, jsval *rval) -{ - char buf[100]; - JSString *str; - PRMJTime split; - jsdouble *date = date_getProlog(cx, obj, argv); - if (!date) - return JS_FALSE; - - if (!JSDOUBLE_IS_FINITE(*date)) { - PR_snprintf(buf, sizeof buf, js_NaN_date_str); - } else { - jsdouble local = LocalTime(*date); - new_explode(local, &split, JS_FALSE); - - /* let PRMJTime format it. Use '%#c' for windows, because '%c' is - * backward-compatible and non-y2k with msvc; '%#c' requests that a - * full year be used in the result string. - */ - PRMJ_FormatTime(buf, sizeof buf, -#ifdef _WIN32 - "%#c", -#else - "%c", -#endif - &split); - } - - str = JS_NewStringCopyZ(cx, buf); - if (!str) - return JS_FALSE; - *rval = STRING_TO_JSVAL(str); - return JS_TRUE; -} - /* helper function */ static JSBool date_format(JSContext *cx, jsdouble date, jsval *rval) @@ -1536,6 +1499,48 @@ date_format(JSContext *cx, jsdouble date, jsval *rval) return JS_TRUE; } +static JSBool +date_toLocaleString(JSContext *cx, JSObject *obj, uintN argc, + jsval *argv, jsval *rval) +{ + char buf[100]; + JSString *str; + PRMJTime split; + jsdouble *date = date_getProlog(cx, obj, argv); + if (!date) + return JS_FALSE; + + if (!JSDOUBLE_IS_FINITE(*date)) { + PR_snprintf(buf, sizeof buf, js_NaN_date_str); + } else { + intN result_len; + jsdouble local = LocalTime(*date); + new_explode(local, &split, JS_FALSE); + + /* let PRMJTime format it. Use '%#c' for windows, because '%c' is + * backward-compatible and non-y2k with msvc; '%#c' requests that a + * full year be used in the result string. + */ + result_len = PRMJ_FormatTime(buf, sizeof buf, +#ifdef _WIN32 + "%#c", +#else + "%c", +#endif + &split); + + /* If it failed, default to toString. */ + if (result_len == 0) + return date_format(cx, *date, rval); + } + + str = JS_NewStringCopyZ(cx, buf); + if (!str) + return JS_FALSE; + *rval = STRING_TO_JSVAL(str); + return JS_TRUE; +} + #if JS_HAS_TOSOURCE #include "prdtoa.h" diff --git a/mozilla/js/src/prmjtime.c b/mozilla/js/src/prmjtime.c index 84dc2eaeab0..3ecc2a72445 100644 --- a/mozilla/js/src/prmjtime.c +++ b/mozilla/js/src/prmjtime.c @@ -158,10 +158,6 @@ PRMJ_LocalGMTDifference() #define G2037GMTMICROHI 0x00e45fab /* micro secs to 2037 high */ #define G2037GMTMICROLOW 0x7a238000 /* micro secs to 2037 low */ -/* Convert from extended time to base time (time since Jan 1 1970) it - * truncates dates if time is before 1970 and after 2037. - */ - /* Convert from base time to extended time */ PR_IMPLEMENT(PRInt64) PRMJ_ToExtendedTime(PRInt32 time) @@ -287,10 +283,10 @@ PRMJ_DSTOffset(PRInt64 time) PRInt32 diff; PRInt64 maxtimet; struct tm tm; + PRTime prtm; #if defined( XP_PC ) || defined( FREEBSD ) || defined ( HPUX9 ) struct tm *ptm; #endif - PRMJTime prtm; LL_UI2L(us2s, PRMJ_USEC_PER_SEC); @@ -372,12 +368,19 @@ PRMJ_FormatTime(char *buf, int buflen, char *fmt, PRMJTime *prtm) * Still not sure if MKLINUX is necessary; this is borrowed from the NSPR20 * prtime.c. I'm leaving it out - My Linux does the right thing without it * (and the wrong thing with it) even though it has the tm_gmtoff, tm_zone - * fields. + * fields. Linux seems to be happy so long as the tm struct is zeroed out. + * The #ifdef in nspr is: + * #if defined(SUNOS4) || defined(MKLINUX) || defined (__GLIBC >= 2) */ #if defined(SUNOS4) if (mktime(&a) == -1) { - PR_snprintf(buf, buflen, "can't get timezone"); + /* Seems to fail whenever the requested date is outside of the 32-bit + * UNIX epoch. We could proceed at this point (setting a.tm_zone to + * "") but then strftime returns a string with a 2-digit field of + * garbage for the year. So we return 0 and hope jsdate.c + * will fall back on toString. + */ return 0; } #endif