Wed, 31 Dec 2014 06:55:46 +0100
Added tag TORBROWSER_REPLICA for changeset 6474c204b198
michael@0 | 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | /* |
michael@0 | 7 | * file: timemac.c |
michael@0 | 8 | * description: test time and date routines on the Mac |
michael@0 | 9 | */ |
michael@0 | 10 | #include <stdio.h> |
michael@0 | 11 | #include "prinit.h" |
michael@0 | 12 | #include "prtime.h" |
michael@0 | 13 | |
michael@0 | 14 | |
michael@0 | 15 | static char *dayOfWeek[] = |
michael@0 | 16 | { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "???" }; |
michael@0 | 17 | static char *month[] = |
michael@0 | 18 | { "Jan", "Feb", "Mar", "Apr", "May", "Jun", |
michael@0 | 19 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "???" }; |
michael@0 | 20 | |
michael@0 | 21 | static void printExplodedTime(const PRExplodedTime *et) { |
michael@0 | 22 | PRInt32 totalOffset; |
michael@0 | 23 | PRInt32 hourOffset, minOffset; |
michael@0 | 24 | const char *sign; |
michael@0 | 25 | |
michael@0 | 26 | /* Print day of the week, month, day, hour, minute, and second */ |
michael@0 | 27 | printf( "%s %s %ld %02ld:%02ld:%02ld ", |
michael@0 | 28 | dayOfWeek[et->tm_wday], month[et->tm_month], et->tm_mday, |
michael@0 | 29 | et->tm_hour, et->tm_min, et->tm_sec); |
michael@0 | 30 | |
michael@0 | 31 | /* Print time zone */ |
michael@0 | 32 | totalOffset = et->tm_params.tp_gmt_offset + et->tm_params.tp_dst_offset; |
michael@0 | 33 | if (totalOffset == 0) { |
michael@0 | 34 | printf("UTC "); |
michael@0 | 35 | } else { |
michael@0 | 36 | sign = ""; |
michael@0 | 37 | if (totalOffset < 0) { |
michael@0 | 38 | totalOffset = -totalOffset; |
michael@0 | 39 | sign = "-"; |
michael@0 | 40 | } |
michael@0 | 41 | hourOffset = totalOffset / 3600; |
michael@0 | 42 | minOffset = (totalOffset % 3600) / 60; |
michael@0 | 43 | printf("%s%02ld%02ld ", sign, hourOffset, minOffset); |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | /* Print year */ |
michael@0 | 47 | printf("%d", et->tm_year); |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | int main(int argc, char** argv) |
michael@0 | 51 | { |
michael@0 | 52 | PR_STDIO_INIT(); |
michael@0 | 53 | PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0); |
michael@0 | 54 | |
michael@0 | 55 | |
michael@0 | 56 | /* |
michael@0 | 57 | ************************************************************* |
michael@0 | 58 | ** |
michael@0 | 59 | ** Testing PR_Now(), PR_ExplodeTime, and PR_ImplodeTime |
michael@0 | 60 | ** on the current time |
michael@0 | 61 | ** |
michael@0 | 62 | ************************************************************* |
michael@0 | 63 | */ |
michael@0 | 64 | |
michael@0 | 65 | { |
michael@0 | 66 | PRTime t1, t2; |
michael@0 | 67 | PRExplodedTime et; |
michael@0 | 68 | |
michael@0 | 69 | printf("*********************************************\n"); |
michael@0 | 70 | printf("** **\n"); |
michael@0 | 71 | printf("** Testing PR_Now(), PR_ExplodeTime, and **\n"); |
michael@0 | 72 | printf("** PR_ImplodeTime on the current time **\n"); |
michael@0 | 73 | printf("** **\n"); |
michael@0 | 74 | printf("*********************************************\n\n"); |
michael@0 | 75 | t1 = PR_Now(); |
michael@0 | 76 | |
michael@0 | 77 | /* First try converting to UTC */ |
michael@0 | 78 | |
michael@0 | 79 | PR_ExplodeTime(t1, PR_GMTParameters, &et); |
michael@0 | 80 | if (et.tm_params.tp_gmt_offset || et.tm_params.tp_dst_offset) { |
michael@0 | 81 | printf("ERROR: UTC has nonzero gmt or dst offset.\n"); |
michael@0 | 82 | return 1; |
michael@0 | 83 | } |
michael@0 | 84 | printf("Current UTC is "); |
michael@0 | 85 | printExplodedTime(&et); |
michael@0 | 86 | printf("\n"); |
michael@0 | 87 | |
michael@0 | 88 | t2 = PR_ImplodeTime(&et); |
michael@0 | 89 | if (LL_NE(t1, t2)) { |
michael@0 | 90 | printf("ERROR: Explode and implode are NOT inverse.\n"); |
michael@0 | 91 | return 1; |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | /* Next, try converting to local (US Pacific) time */ |
michael@0 | 95 | |
michael@0 | 96 | PR_ExplodeTime(t1, PR_LocalTimeParameters, &et); |
michael@0 | 97 | printf("Current local time is "); |
michael@0 | 98 | printExplodedTime(&et); |
michael@0 | 99 | printf("\n"); |
michael@0 | 100 | printf("GMT offset is %ld, DST offset is %ld\n", |
michael@0 | 101 | et.tm_params.tp_gmt_offset, et.tm_params.tp_dst_offset); |
michael@0 | 102 | t2 = PR_ImplodeTime(&et); |
michael@0 | 103 | if (LL_NE(t1, t2)) { |
michael@0 | 104 | printf("ERROR: Explode and implode are NOT inverse.\n"); |
michael@0 | 105 | return 1; |
michael@0 | 106 | } |
michael@0 | 107 | } |
michael@0 | 108 | |
michael@0 | 109 | printf("Please examine the results\n"); |
michael@0 | 110 | return 0; |
michael@0 | 111 | } |