1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/nsprpub/pr/tests/parsetm.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,84 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +/* 1.10 + * This test program should eventually become a full-blown test for 1.11 + * PR_ParseTimeString. Right now it just verifies that PR_ParseTimeString 1.12 + * doesn't crash on an out-of-range time string (bug 480740). 1.13 + */ 1.14 + 1.15 +#include "prtime.h" 1.16 + 1.17 +#include <time.h> 1.18 +#include <stdio.h> 1.19 +#include <stdlib.h> 1.20 +#include <string.h> 1.21 + 1.22 +PRBool debug_mode = PR_TRUE; 1.23 + 1.24 +static char *dayOfWeek[] = 1.25 + { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "???" }; 1.26 +static char *month[] = 1.27 + { "Jan", "Feb", "Mar", "Apr", "May", "Jun", 1.28 + "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "???" }; 1.29 + 1.30 +static void PrintExplodedTime(const PRExplodedTime *et) { 1.31 + PRInt32 totalOffset; 1.32 + PRInt32 hourOffset, minOffset; 1.33 + const char *sign; 1.34 + 1.35 + /* Print day of the week, month, day, hour, minute, and second */ 1.36 + if (debug_mode) printf("%s %s %ld %02ld:%02ld:%02ld ", 1.37 + dayOfWeek[et->tm_wday], month[et->tm_month], et->tm_mday, 1.38 + et->tm_hour, et->tm_min, et->tm_sec); 1.39 + 1.40 + /* Print time zone */ 1.41 + totalOffset = et->tm_params.tp_gmt_offset + et->tm_params.tp_dst_offset; 1.42 + if (totalOffset == 0) { 1.43 + if (debug_mode) printf("UTC "); 1.44 + } else { 1.45 + sign = "+"; 1.46 + if (totalOffset < 0) { 1.47 + totalOffset = -totalOffset; 1.48 + sign = "-"; 1.49 + } 1.50 + hourOffset = totalOffset / 3600; 1.51 + minOffset = (totalOffset % 3600) / 60; 1.52 + if (debug_mode) 1.53 + printf("%s%02ld%02ld ", sign, hourOffset, minOffset); 1.54 + } 1.55 + 1.56 + /* Print year */ 1.57 + if (debug_mode) printf("%hd", et->tm_year); 1.58 +} 1.59 + 1.60 +int main(int argc, char **argv) 1.61 +{ 1.62 + PRTime ct; 1.63 + PRExplodedTime et; 1.64 + PRStatus rv; 1.65 + char *sp1 = "Sat, 1 Jan 3001 00:00:00"; /* no time zone */ 1.66 + char *sp2 = "Fri, 31 Dec 3000 23:59:60"; /* no time zone, not normalized */ 1.67 + 1.68 +#if _MSC_VER >= 1400 && !defined(WINCE) 1.69 + /* Run this test in the US Pacific Time timezone. */ 1.70 + _putenv_s("TZ", "PST8PDT"); 1.71 + _tzset(); 1.72 +#endif 1.73 + 1.74 + rv = PR_ParseTimeString(sp1, PR_FALSE, &ct); 1.75 + printf("rv = %d\n", rv); 1.76 + PR_ExplodeTime(ct, PR_GMTParameters, &et); 1.77 + PrintExplodedTime(&et); 1.78 + printf("\n"); 1.79 + 1.80 + rv = PR_ParseTimeString(sp2, PR_FALSE, &ct); 1.81 + printf("rv = %d\n", rv); 1.82 + PR_ExplodeTime(ct, PR_GMTParameters, &et); 1.83 + PrintExplodedTime(&et); 1.84 + printf("\n"); 1.85 + 1.86 + return 0; 1.87 +}