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