diff --git a/mozilla/nsprpub/pr/src/misc/prtime.c b/mozilla/nsprpub/pr/src/misc/prtime.c index d73b08cdeae..5f05d5ef68e 100644 --- a/mozilla/nsprpub/pr/src/misc/prtime.c +++ b/mozilla/nsprpub/pr/src/misc/prtime.c @@ -1698,29 +1698,37 @@ PR_FormatTime(char *buf, int buflen, const char *fmt, const PRExplodedTime *tm) { size_t rv; struct tm a; - a.tm_sec = tm->tm_sec; - a.tm_min = tm->tm_min; - a.tm_hour = tm->tm_hour; - a.tm_mday = tm->tm_mday; - a.tm_mon = tm->tm_month; - a.tm_wday = tm->tm_wday; - a.tm_year = tm->tm_year - 1900; - a.tm_yday = tm->tm_yday; - a.tm_isdst = tm->tm_params.tp_dst_offset ? 1 : 0; + struct tm *ap; -/* - * On some platforms, for example SunOS 4, struct tm has two additional - * fields: tm_zone and tm_gmtoff. - */ + if (tm) { + ap = &a; + a.tm_sec = tm->tm_sec; + a.tm_min = tm->tm_min; + a.tm_hour = tm->tm_hour; + a.tm_mday = tm->tm_mday; + a.tm_mon = tm->tm_month; + a.tm_wday = tm->tm_wday; + a.tm_year = tm->tm_year - 1900; + a.tm_yday = tm->tm_yday; + a.tm_isdst = tm->tm_params.tp_dst_offset ? 1 : 0; + + /* + * On some platforms, for example SunOS 4, struct tm has two + * additional fields: tm_zone and tm_gmtoff. + */ #if defined(SUNOS4) || (__GLIBC__ >= 2) || defined(XP_BEOS) \ || defined(NETBSD) || defined(OPENBSD) || defined(FREEBSD) \ || defined(DARWIN) || defined(SYMBIAN) - a.tm_zone = NULL; - a.tm_gmtoff = tm->tm_params.tp_gmt_offset + tm->tm_params.tp_dst_offset; + a.tm_zone = NULL; + a.tm_gmtoff = tm->tm_params.tp_gmt_offset + + tm->tm_params.tp_dst_offset; #endif + } else { + ap = NULL; + } - rv = strftime(buf, buflen, fmt, &a); + rv = strftime(buf, buflen, fmt, ap); if (!rv && buf && buflen > 0) { /* * When strftime fails, the contents of buf are indeterminate. diff --git a/mozilla/nsprpub/pr/tests/formattm.c b/mozilla/nsprpub/pr/tests/formattm.c index c3c758b5f28..8b04cd3701e 100644 --- a/mozilla/nsprpub/pr/tests/formattm.c +++ b/mozilla/nsprpub/pr/tests/formattm.c @@ -44,16 +44,39 @@ int main() { char buffer[256]; + char small_buffer[8]; PRTime now; PRExplodedTime tod; now = PR_Now(); PR_ExplodeTime(now, PR_LocalTimeParameters, &tod); - (void)PR_FormatTime(buffer, sizeof(buffer), - "%a %b %d %H:%M:%S %Z %Y", &tod); - printf("%s\n", buffer); + + if (PR_FormatTime(buffer, sizeof(buffer), + "%a %b %d %H:%M:%S %Z %Y", &tod) != 0) { + printf("%s\n", buffer); + } else { + fprintf(stderr, "PR_FormatTime(buffer) failed\n"); + return 1; + } + + small_buffer[0] = '?'; + if (PR_FormatTime(small_buffer, sizeof(small_buffer), + "%a %b %d %H:%M:%S %Z %Y", &tod) == 0) { + if (small_buffer[0] != '\0') { + fprintf(stderr, "PR_FormatTime(small_buffer) did not output " + "an empty string on failure\n"); + return 1; + } + printf("%s\n", small_buffer); + } else { + fprintf(stderr, "PR_FormatTime(small_buffer) succeeded " + "unexpectedly\n"); + return 1; + } + (void)PR_FormatTimeUSEnglish(buffer, sizeof(buffer), "%a %b %d %H:%M:%S %Z %Y", &tod); printf("%s\n", buffer); + return 0; }