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 
printExplodedTime(const PRExplodedTime * et)21 static void printExplodedTime(const PRExplodedTime *et) {
22     PRInt32 totalOffset;
23     PRInt32 hourOffset, minOffset;
24     const char *sign;
25 
26     /* Print day of the week, month, day, hour, minute, and second */
27     printf( "%s %s %ld %02ld:%02ld:%02ld ",
28 	    dayOfWeek[et->tm_wday], month[et->tm_month], et->tm_mday,
29 	    et->tm_hour, et->tm_min, 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 	printf("UTC ");
35     } else {
36         sign = "";
37         if (totalOffset < 0) {
38 	    totalOffset = -totalOffset;
39 	    sign = "-";
40         }
41         hourOffset = totalOffset / 3600;
42         minOffset = (totalOffset % 3600) / 60;
43         printf("%s%02ld%02ld ", sign, hourOffset, minOffset);
44     }
45 
46     /* Print year */
47     printf("%d", et->tm_year);
48 }
49 
main(int argc,char ** argv)50 int main(int argc, char** argv)
51 {
52     PR_STDIO_INIT();
53     PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
54 
55 
56    /*
57      *************************************************************
58      **
59      **  Testing PR_Now(), PR_ExplodeTime, and PR_ImplodeTime
60      **  on the current time
61      **
62      *************************************************************
63      */
64 
65     {
66 	PRTime t1, t2;
67 	PRExplodedTime et;
68 
69 	printf("*********************************************\n");
70 	printf("**                                         **\n");
71         printf("** Testing PR_Now(), PR_ExplodeTime, and   **\n");
72 	printf("** PR_ImplodeTime on the current time      **\n");
73 	printf("**                                         **\n");
74 	printf("*********************************************\n\n");
75         t1 = PR_Now();
76 
77         /* First try converting to UTC */
78 
79         PR_ExplodeTime(t1, PR_GMTParameters, &et);
80         if (et.tm_params.tp_gmt_offset || et.tm_params.tp_dst_offset) {
81 	    printf("ERROR: UTC has nonzero gmt or dst offset.\n");
82 	    return 1;
83         }
84         printf("Current UTC is ");
85 	printExplodedTime(&et);
86 	printf("\n");
87 
88         t2 = PR_ImplodeTime(&et);
89         if (LL_NE(t1, t2)) {
90 	    printf("ERROR: Explode and implode are NOT inverse.\n");
91 	    return 1;
92         }
93 
94         /* Next, try converting to local (US Pacific) time */
95 
96         PR_ExplodeTime(t1, PR_LocalTimeParameters, &et);
97         printf("Current local time is ");
98 	printExplodedTime(&et);
99 	printf("\n");
100 	printf("GMT offset is %ld, DST offset is %ld\n",
101 		et.tm_params.tp_gmt_offset, et.tm_params.tp_dst_offset);
102         t2 = PR_ImplodeTime(&et);
103         if (LL_NE(t1, t2)) {
104 	    printf("ERROR: Explode and implode are NOT inverse.\n");
105 	    return 1;
106 	}
107     }
108 
109     printf("Please examine the results\n");
110     return 0;
111 }
112