1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 /*
7  * This test program should eventually become a full-blown test for
8  * PR_ParseTimeString.  Right now it just verifies that PR_ParseTimeString
9  * doesn't crash on an out-of-range time string (bug 480740).
10  */
11 
12 #include "prtime.h"
13 
14 #include <time.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 
19 PRBool debug_mode = PR_TRUE;
20 
21 static char *dayOfWeek[] =
22 { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "???" };
23 static char *month[] =
24 {   "Jan", "Feb", "Mar", "Apr", "May", "Jun",
25     "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "???"
26 };
27 
PrintExplodedTime(const PRExplodedTime * et)28 static void PrintExplodedTime(const PRExplodedTime *et) {
29     PRInt32 totalOffset;
30     PRInt32 hourOffset, minOffset;
31     const char *sign;
32 
33     /* Print day of the week, month, day, hour, minute, and second */
34     if (debug_mode) printf("%s %s %ld %02ld:%02ld:%02ld ",
35                                dayOfWeek[et->tm_wday], month[et->tm_month], et->tm_mday,
36                                et->tm_hour, et->tm_min, et->tm_sec);
37 
38     /* Print time zone */
39     totalOffset = et->tm_params.tp_gmt_offset + et->tm_params.tp_dst_offset;
40     if (totalOffset == 0) {
41         if (debug_mode) {
42             printf("UTC ");
43         }
44     } else {
45         sign = "+";
46         if (totalOffset < 0) {
47             totalOffset = -totalOffset;
48             sign = "-";
49         }
50         hourOffset = totalOffset / 3600;
51         minOffset = (totalOffset % 3600) / 60;
52         if (debug_mode) {
53             printf("%s%02ld%02ld ", sign, hourOffset, minOffset);
54         }
55     }
56 
57     /* Print year */
58     if (debug_mode) {
59         printf("%hd", et->tm_year);
60     }
61 }
62 
main(int argc,char ** argv)63 int main(int argc, char **argv)
64 {
65     PRTime ct;
66     PRExplodedTime et;
67     PRStatus rv;
68     char *sp1 = "Sat, 1 Jan 3001 00:00:00";  /* no time zone */
69     char *sp2 = "Fri, 31 Dec 3000 23:59:60";  /* no time zone, not normalized */
70 
71 #if _MSC_VER >= 1400 && !defined(WINCE)
72     /* Run this test in the US Pacific Time timezone. */
73     _putenv_s("TZ", "PST8PDT");
74     _tzset();
75 #endif
76 
77     rv = PR_ParseTimeString(sp1, PR_FALSE, &ct);
78     printf("rv = %d\n", rv);
79     PR_ExplodeTime(ct, PR_GMTParameters, &et);
80     PrintExplodedTime(&et);
81     printf("\n");
82 
83     rv = PR_ParseTimeString(sp2, PR_FALSE, &ct);
84     printf("rv = %d\n", rv);
85     PR_ExplodeTime(ct, PR_GMTParameters, &et);
86     PrintExplodedTime(&et);
87     printf("\n");
88 
89     return 0;
90 }
91