VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/nsprpub/pr/tests/timemac.c@ 1

Last change on this file since 1 was 1, checked in by vboxsync, 55 years ago

import

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.9 KB
Line 
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2/* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 *
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
14 *
15 * The Original Code is the Netscape Portable Runtime (NSPR).
16 *
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998-2000
20 * the Initial Developer. All Rights Reserved.
21 *
22 * Contributor(s):
23 *
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
35 *
36 * ***** END LICENSE BLOCK ***** */
37
38/*
39 * file: timemac.c
40 * description: test time and date routines on the Mac
41 */
42#include <stdio.h>
43#include "prinit.h"
44#include "prtime.h"
45
46#ifdef XP_MAC
47#include "prlog.h"
48#define printf PR_LogPrint
49extern void SetupMacPrintfLog(char *logFile);
50#endif
51
52
53static char *dayOfWeek[] =
54 { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "???" };
55static char *month[] =
56 { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
57 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "???" };
58
59static void printExplodedTime(const PRExplodedTime *et) {
60 PRInt32 totalOffset;
61 PRInt32 hourOffset, minOffset;
62 const char *sign;
63
64 /* Print day of the week, month, day, hour, minute, and second */
65 printf( "%s %s %ld %02ld:%02ld:%02ld ",
66 dayOfWeek[et->tm_wday], month[et->tm_month], et->tm_mday,
67 et->tm_hour, et->tm_min, et->tm_sec);
68
69 /* Print time zone */
70 totalOffset = et->tm_params.tp_gmt_offset + et->tm_params.tp_dst_offset;
71 if (totalOffset == 0) {
72 printf("UTC ");
73 } else {
74 sign = "";
75 if (totalOffset < 0) {
76 totalOffset = -totalOffset;
77 sign = "-";
78 }
79 hourOffset = totalOffset / 3600;
80 minOffset = (totalOffset % 3600) / 60;
81 printf("%s%02ld%02ld ", sign, hourOffset, minOffset);
82 }
83
84 /* Print year */
85 printf("%d", et->tm_year);
86}
87
88int main(int argc, char** argv)
89{
90 PR_STDIO_INIT();
91 PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
92
93
94#ifdef XP_MAC
95 SetupMacPrintfLog("timemac.log");
96#endif
97
98 /*
99 *************************************************************
100 **
101 ** Testing PR_Now(), PR_ExplodeTime, and PR_ImplodeTime
102 ** on the current time
103 **
104 *************************************************************
105 */
106
107 {
108 PRTime t1, t2;
109 PRExplodedTime et;
110
111 printf("*********************************************\n");
112 printf("** **\n");
113 printf("** Testing PR_Now(), PR_ExplodeTime, and **\n");
114 printf("** PR_ImplodeTime on the current time **\n");
115 printf("** **\n");
116 printf("*********************************************\n\n");
117 t1 = PR_Now();
118
119 /* First try converting to UTC */
120
121 PR_ExplodeTime(t1, PR_GMTParameters, &et);
122 if (et.tm_params.tp_gmt_offset || et.tm_params.tp_dst_offset) {
123 printf("ERROR: UTC has nonzero gmt or dst offset.\n");
124 return 1;
125 }
126 printf("Current UTC is ");
127 printExplodedTime(&et);
128 printf("\n");
129
130 t2 = PR_ImplodeTime(&et);
131 if (LL_NE(t1, t2)) {
132 printf("ERROR: Explode and implode are NOT inverse.\n");
133 return 1;
134 }
135
136 /* Next, try converting to local (US Pacific) time */
137
138 PR_ExplodeTime(t1, PR_LocalTimeParameters, &et);
139 printf("Current local time is ");
140 printExplodedTime(&et);
141 printf("\n");
142 printf("GMT offset is %ld, DST offset is %ld\n",
143 et.tm_params.tp_gmt_offset, et.tm_params.tp_dst_offset);
144 t2 = PR_ImplodeTime(&et);
145 if (LL_NE(t1, t2)) {
146 printf("ERROR: Explode and implode are NOT inverse.\n");
147 return 1;
148 }
149 }
150
151 printf("Please examine the results\n");
152 return 0;
153}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette