parsetm.c (4090B)
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 #include "prtime.h" 7 8 #include <time.h> 9 #include <stdio.h> 10 #include <stdlib.h> 11 #include <string.h> 12 13 PRBool debug_mode = PR_TRUE; 14 15 static char* dayOfWeek[] = {"Sun", "Mon", "Tue", "Wed", 16 "Thu", "Fri", "Sat", "???"}; 17 static char* month[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", 18 "Aug", "Sep", "Oct", "Nov", "Dec", "???"}; 19 20 static void PrintExplodedTime(const PRExplodedTime* et) { 21 PRInt32 totalOffset; 22 PRInt32 hourOffset, minOffset; 23 const char* sign; 24 25 /* Print day of the week, month, day, hour, minute, and second */ 26 if (debug_mode) 27 printf("%s %s %ld %02ld:%02ld:%02ld ", dayOfWeek[et->tm_wday], 28 month[et->tm_month], et->tm_mday, et->tm_hour, et->tm_min, 29 et->tm_sec); 30 31 /* Print time zone */ 32 totalOffset = et->tm_params.tp_gmt_offset + et->tm_params.tp_dst_offset; 33 if (totalOffset == 0) { 34 if (debug_mode) { 35 printf("UTC "); 36 } 37 } else { 38 sign = "+"; 39 if (totalOffset < 0) { 40 totalOffset = -totalOffset; 41 sign = "-"; 42 } 43 hourOffset = totalOffset / 3600; 44 minOffset = (totalOffset % 3600) / 60; 45 if (debug_mode) { 46 printf("%s%02ld%02ld ", sign, hourOffset, minOffset); 47 } 48 } 49 50 /* Print year */ 51 if (debug_mode) { 52 printf("%hd", et->tm_year); 53 } 54 } 55 56 static PRStatus KnownAnswerTest(void) { 57 static const struct { 58 const char* string; 59 PRBool default_to_gmt; 60 PRTime expected; 61 } tests[] = { 62 {"Mon, 15 Oct 2007 19:45:00 GMT", PR_FALSE, PR_INT64(1192477500000000)}, 63 {"15 Oct 07 19:45 GMT", PR_FALSE, PR_INT64(1192477500000000)}, 64 {"Mon Oct 15 12:45 PDT 2007", PR_FALSE, PR_INT64(1192477500000000)}, 65 {"16 Oct 2007 4:45-JST (Tuesday)", PR_FALSE, PR_INT64(1192477500000000)}, 66 // Not normalized. 67 {"Mon Oct 15 12:44:60 PDT 2007", PR_FALSE, PR_INT64(1192477500000000)}, 68 // Not normalized. 69 {"Sun Oct 14 36:45 PDT 2007", PR_FALSE, PR_INT64(1192477500000000)}, 70 {"Mon, 15 Oct 2007 19:45:23 GMT", PR_FALSE, PR_INT64(1192477523000000)}, 71 }; 72 73 PRBool failed = PR_FALSE; 74 75 for (int i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) { 76 PRTime result = 0; 77 if (PR_ParseTimeString(tests[i].string, tests[i].default_to_gmt, &result) != 78 PR_SUCCESS) { 79 printf("PR_ParseTimeString(\"%s\", %s, &result) failed\n", 80 tests[i].string, tests[i].default_to_gmt ? "PR_TRUE" : "PR_FALSE"); 81 failed = PR_TRUE; 82 continue; 83 } 84 if (result != tests[i].expected) { 85 printf( 86 "PR_ParseTimeString(\"%s\", %s, &result) returns %lld, expected " 87 "%lld\n", 88 tests[i].string, tests[i].default_to_gmt ? "PR_TRUE" : "PR_FALSE", 89 (long long)result, (long long)tests[i].expected); 90 failed = PR_TRUE; 91 } 92 } 93 94 return failed ? PR_FAILURE : PR_SUCCESS; 95 } 96 97 int main(int argc, char** argv) { 98 /* 99 * 1. Verify that PR_ParseTimeString doesn't crash on an out-of-range time 100 * string (bug 480740). 101 */ 102 PRTime ct; 103 PRExplodedTime et; 104 PRStatus rv; 105 char* sp1 = "Sat, 1 Jan 3001 00:00:00"; /* no time zone */ 106 char* sp2 = "Fri, 31 Dec 3000 23:59:60"; /* no time zone, not normalized */ 107 108 #if _MSC_VER >= 1400 && !defined(WINCE) 109 /* Run this test in the US Pacific Time timezone. */ 110 _putenv_s("TZ", "PST8PDT"); 111 _tzset(); 112 #endif 113 114 rv = PR_ParseTimeString(sp1, PR_FALSE, &ct); 115 printf("rv = %d\n", rv); 116 PR_ExplodeTime(ct, PR_GMTParameters, &et); 117 PrintExplodedTime(&et); 118 printf("\n"); 119 120 rv = PR_ParseTimeString(sp2, PR_FALSE, &ct); 121 printf("rv = %d\n", rv); 122 PR_ExplodeTime(ct, PR_GMTParameters, &et); 123 PrintExplodedTime(&et); 124 printf("\n"); 125 126 /* 2. Run a full-blown test for PR_ParseTimeString. */ 127 if (KnownAnswerTest() != PR_SUCCESS) { 128 printf("FAIL\n"); 129 return 1; 130 } 131 132 printf("PASS\n"); 133 return 0; 134 }