~ubuntu-branches/ubuntu/saucy/nspr/saucy-updates

« back to all changes in this revision

Viewing changes to mozilla/nsprpub/pr/src/md/mac/mactime.c

  • Committer: Bazaar Package Importer
  • Author(s): Alexander Sack
  • Date: 2009-08-10 11:34:26 UTC
  • mfrom: (1.1.10 upstream)
  • Revision ID: james.westby@ubuntu.com-20090810113426-3uv4diflrkcbdimm
Tags: 4.8-0ubuntu1
* New upstream release: 4.8 (LP: #387812)
* adjust patches to changed upstreanm codebase
  - update debian/patches/99_configure.patch
* update shlibs symbols to include new API elements
  - update debian/libnspr4-0d.symbols

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
#include <OSUtils.h>
39
 
#include <Timer.h>
40
 
 
41
 
#include "primpl.h"
42
 
 
43
 
#include "mactime.h"
44
 
 
45
 
unsigned long gJanuaryFirst1970Seconds;
46
 
 
47
 
/*
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.
53
 
 *
54
 
 * Because serial port and SLIP conflict with ReadXPram calls,
55
 
 * we cache the call here.
56
 
 *
57
 
 * Caveat: this caching will give the wrong result if a session
58
 
 * extend across the DST changeover time.
59
 
 */
60
 
 
61
 
static void MyReadLocation(MachineLocation *loc)
62
 
{
63
 
    static MachineLocation storedLoc;
64
 
    static Boolean didReadLocation = false;
65
 
    
66
 
    if (!didReadLocation) {
67
 
        ReadLocation(&storedLoc);
68
 
        didReadLocation = true;
69
 
    }
70
 
    *loc = storedLoc;
71
 
}
72
 
 
73
 
static long GMTDelta(void)
74
 
{
75
 
    MachineLocation loc;
76
 
    long gmtDelta;
77
 
 
78
 
    MyReadLocation(&loc);
79
 
    gmtDelta = loc.u.gmtDelta & 0x00ffffff;
80
 
    if (gmtDelta & 0x00800000) {    /* test sign extend bit */
81
 
        gmtDelta |= 0xff000000;
82
 
    }
83
 
    return gmtDelta;
84
 
}
85
 
 
86
 
void MacintoshInitializeTime(void)
87
 
{
88
 
    /*
89
 
     * The NSPR epoch is midnight, Jan. 1, 1970 GMT.
90
 
     *
91
 
     * At midnight Jan. 1, 1970 GMT, the local time was
92
 
     *     midnight Jan. 1, 1970 + GMTDelta().
93
 
     *
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.)
97
 
     *
98
 
     * So the NSPR epoch is 2082844800 + GMTDelta() seconds since
99
 
     * the Mac epoch.  Whew! :-)
100
 
     */
101
 
    gJanuaryFirst1970Seconds = 2082844800 + GMTDelta();
102
 
}
103
 
 
104
 
/*
105
 
 *-----------------------------------------------------------------------
106
 
 *
107
 
 * PR_Now --
108
 
 *
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
112
 
 *     Implementation.
113
 
 *     Cf. time_t time(time_t *tp)
114
 
 *
115
 
 *-----------------------------------------------------------------------
116
 
 */
117
 
 
118
 
PRTime PR_Now(void)
119
 
{
120
 
    unsigned long currentTime;    /* unsigned 32-bit integer, ranging
121
 
                                     from midnight Jan. 1, 1904 to 
122
 
                                     6:28:15 AM Feb. 6, 2040 */
123
 
    PRTime retVal;
124
 
    int64  usecPerSec;
125
 
 
126
 
    /*
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.
130
 
     */
131
 
    GetDateTime(&currentTime);
132
 
 
133
 
    /*
134
 
     * Express the current time relative to the NSPR epoch,
135
 
     * midnight, Jan. 1, 1970 GMT.
136
 
     *
137
 
     * At midnight Jan. 1, 1970 GMT, the local time was
138
 
     *     midnight Jan. 1, 1970 + GMTDelta().
139
 
     *
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.)
143
 
     *
144
 
     * So the NSPR epoch is 2082844800 + GMTDelta() seconds since
145
 
     * the Mac epoch.  Whew! :-)
146
 
     */
147
 
    currentTime = currentTime - 2082844800 - GMTDelta();
148
 
 
149
 
    /* Convert seconds to microseconds */
150
 
    LL_I2L(usecPerSec, PR_USEC_PER_SEC);
151
 
    LL_I2L(retVal, currentTime);
152
 
    LL_MUL(retVal, retVal, usecPerSec);
153
 
 
154
 
    return retVal;
155
 
}
156
 
 
157
 
/*
158
 
 *-------------------------------------------------------------------------
159
 
 *
160
 
 * PR_LocalTimeParameters --
161
 
 * 
162
 
 *     returns the time parameters for the local time zone
163
 
 *
164
 
 *     This is the machine-dependent implementation for Mac.
165
 
 *
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.
170
 
 *
171
 
 *-------------------------------------------------------------------------
172
 
 */
173
 
 
174
 
PRTimeParameters PR_LocalTimeParameters(const PRExplodedTime *gmt)
175
 
{
176
 
#pragma unused (gmt)
177
 
 
178
 
    PRTimeParameters retVal;
179
 
    MachineLocation loc;
180
 
 
181
 
    MyReadLocation(&loc);
182
 
 
183
 
    /* 
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.
188
 
     */
189
 
 
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;
193
 
    }
194
 
 
195
 
    /*
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.
199
 
     */
200
 
 
201
 
    if (loc.u.dlsDelta) {
202
 
        retVal.tp_gmt_offset -= 3600;
203
 
        retVal.tp_dst_offset = 3600;
204
 
    } else {
205
 
        retVal.tp_dst_offset = 0;
206
 
    }
207
 
    return retVal;
208
 
}
209
 
 
210
 
PRIntervalTime _MD_GetInterval(void)
211
 
{
212
 
    PRIntervalTime retVal;
213
 
    PRUint64 upTime, microtomilli;      
214
 
 
215
 
    /*
216
 
     * Use the Microseconds procedure to obtain the number of
217
 
     * microseconds elapsed since system startup time.
218
 
     */
219
 
    Microseconds((UnsignedWide *)&upTime);
220
 
    LL_I2L(microtomilli, PR_USEC_PER_MSEC);
221
 
    LL_DIV(upTime, upTime, microtomilli);
222
 
    LL_L2I(retVal, upTime);
223
 
        
224
 
    return retVal;
225
 
}
226
 
 
227
 
struct tm *Maclocaltime(const time_t * t)
228
 
{
229
 
        DateTimeRec dtr;
230
 
        MachineLocation loc;
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};
235
 
 
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;
250
 
        return(&statictime);
251
 
}
252
 
 
253