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
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/
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
15
* The Original Code is the Netscape Portable Runtime (NSPR).
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.
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.
36
* ***** END LICENSE BLOCK ***** */
45
unsigned long gJanuaryFirst1970Seconds;
48
* The geographic location and time zone information of a Mac
49
* are stored in extended parameter RAM. The ReadLocation
50
* produdure uses the geographic location record, MachineLocation,
51
* to read the geographic location and time zone information in
52
* extended parameter RAM.
54
* Because serial port and SLIP conflict with ReadXPram calls,
55
* we cache the call here.
57
* Caveat: this caching will give the wrong result if a session
58
* extend across the DST changeover time.
61
static void MyReadLocation(MachineLocation *loc)
63
static MachineLocation storedLoc;
64
static Boolean didReadLocation = false;
66
if (!didReadLocation) {
67
ReadLocation(&storedLoc);
68
didReadLocation = true;
73
static long GMTDelta(void)
79
gmtDelta = loc.u.gmtDelta & 0x00ffffff;
80
if (gmtDelta & 0x00800000) { /* test sign extend bit */
81
gmtDelta |= 0xff000000;
86
void MacintoshInitializeTime(void)
89
* The NSPR epoch is midnight, Jan. 1, 1970 GMT.
91
* At midnight Jan. 1, 1970 GMT, the local time was
92
* midnight Jan. 1, 1970 + GMTDelta().
94
* Midnight Jan. 1, 1970 is 86400 * (365 * (1970 - 1904) + 17)
95
* = 2082844800 seconds since the Mac epoch.
96
* (There were 17 leap years from 1904 to 1970.)
98
* So the NSPR epoch is 2082844800 + GMTDelta() seconds since
99
* the Mac epoch. Whew! :-)
101
gJanuaryFirst1970Seconds = 2082844800 + GMTDelta();
105
*-----------------------------------------------------------------------
109
* Returns the current time in microseconds since the epoch.
110
* The epoch is midnight January 1, 1970 GMT.
111
* The implementation is machine dependent. This is the Mac
113
* Cf. time_t time(time_t *tp)
115
*-----------------------------------------------------------------------
120
unsigned long currentTime; /* unsigned 32-bit integer, ranging
121
from midnight Jan. 1, 1904 to
122
6:28:15 AM Feb. 6, 2040 */
127
* Get the current time expressed as the number of seconds
128
* elapsed since the Mac epoch, midnight, Jan. 1, 1904 (local time).
129
* On a Mac, current time accuracy is up to a second.
131
GetDateTime(¤tTime);
134
* Express the current time relative to the NSPR epoch,
135
* midnight, Jan. 1, 1970 GMT.
137
* At midnight Jan. 1, 1970 GMT, the local time was
138
* midnight Jan. 1, 1970 + GMTDelta().
140
* Midnight Jan. 1, 1970 is 86400 * (365 * (1970 - 1904) + 17)
141
* = 2082844800 seconds since the Mac epoch.
142
* (There were 17 leap years from 1904 to 1970.)
144
* So the NSPR epoch is 2082844800 + GMTDelta() seconds since
145
* the Mac epoch. Whew! :-)
147
currentTime = currentTime - 2082844800 - GMTDelta();
149
/* Convert seconds to microseconds */
150
LL_I2L(usecPerSec, PR_USEC_PER_SEC);
151
LL_I2L(retVal, currentTime);
152
LL_MUL(retVal, retVal, usecPerSec);
158
*-------------------------------------------------------------------------
160
* PR_LocalTimeParameters --
162
* returns the time parameters for the local time zone
164
* This is the machine-dependent implementation for Mac.
166
* Caveat: On a Mac, we only know the GMT and DST offsets for
167
* the current time, not for the time in question.
168
* Mac has no support for DST handling.
169
* DST changeover is all manually set by the user.
171
*-------------------------------------------------------------------------
174
PRTimeParameters PR_LocalTimeParameters(const PRExplodedTime *gmt)
178
PRTimeParameters retVal;
181
MyReadLocation(&loc);
184
* On a Mac, the GMT value is in seconds east of GMT. For example,
185
* San Francisco is at -28,800 seconds (8 hours * 3600 seconds per hour)
186
* east of GMT. The gmtDelta field is a 3-byte value contained in a
187
* long word, so you must take care to get it properly.
190
retVal.tp_gmt_offset = loc.u.gmtDelta & 0x00ffffff;
191
if (retVal.tp_gmt_offset & 0x00800000) { /* test sign extend bit */
192
retVal.tp_gmt_offset |= 0xff000000;
196
* The daylight saving time value, dlsDelta, is a signed byte
197
* value representing the offset for the hour field -- whether
198
* to add 1 hour, subtract 1 hour, or make no change at all.
201
if (loc.u.dlsDelta) {
202
retVal.tp_gmt_offset -= 3600;
203
retVal.tp_dst_offset = 3600;
205
retVal.tp_dst_offset = 0;
210
PRIntervalTime _MD_GetInterval(void)
212
PRIntervalTime retVal;
213
PRUint64 upTime, microtomilli;
216
* Use the Microseconds procedure to obtain the number of
217
* microseconds elapsed since system startup time.
219
Microseconds((UnsignedWide *)&upTime);
220
LL_I2L(microtomilli, PR_USEC_PER_MSEC);
221
LL_DIV(upTime, upTime, microtomilli);
222
LL_L2I(retVal, upTime);
227
struct tm *Maclocaltime(const time_t * t)
231
time_t macLocal = *t + gJanuaryFirst1970Seconds; /* GMT Mac */
232
static struct tm statictime;
233
static const short monthday[12] =
234
{0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
236
SecondsToDate(macLocal, &dtr);
237
statictime.tm_sec = dtr.second;
238
statictime.tm_min = dtr.minute;
239
statictime.tm_hour = dtr.hour;
240
statictime.tm_mday = dtr.day;
241
statictime.tm_mon = dtr.month - 1;
242
statictime.tm_year = dtr.year - 1900;
243
statictime.tm_wday = dtr.dayOfWeek - 1;
244
statictime.tm_yday = monthday[statictime.tm_mon]
245
+ statictime.tm_mday - 1;
246
if (2 < statictime.tm_mon && !(statictime.tm_year & 3))
247
++statictime.tm_yday;
248
MyReadLocation(&loc);
249
statictime.tm_isdst = loc.u.dlsDelta;