Back to index

lightning-sunbird  0.9+nobinonly
mactime.c
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Netscape Portable Runtime (NSPR).
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include <OSUtils.h>
00039 #include <Timer.h>
00040 
00041 #include "primpl.h"
00042 
00043 #include "mactime.h"
00044 
00045 unsigned long gJanuaryFirst1970Seconds;
00046 
00047 /*
00048  * The geographic location and time zone information of a Mac
00049  * are stored in extended parameter RAM.  The ReadLocation
00050  * produdure uses the geographic location record, MachineLocation,
00051  * to read the geographic location and time zone information in
00052  * extended parameter RAM.
00053  *
00054  * Because serial port and SLIP conflict with ReadXPram calls,
00055  * we cache the call here.
00056  *
00057  * Caveat: this caching will give the wrong result if a session
00058  * extend across the DST changeover time.
00059  */
00060 
00061 static void MyReadLocation(MachineLocation *loc)
00062 {
00063     static MachineLocation storedLoc;
00064     static Boolean didReadLocation = false;
00065     
00066     if (!didReadLocation) {
00067         ReadLocation(&storedLoc);
00068         didReadLocation = true;
00069     }
00070     *loc = storedLoc;
00071 }
00072 
00073 static long GMTDelta(void)
00074 {
00075     MachineLocation loc;
00076     long gmtDelta;
00077 
00078     MyReadLocation(&loc);
00079     gmtDelta = loc.u.gmtDelta & 0x00ffffff;
00080     if (gmtDelta & 0x00800000) {    /* test sign extend bit */
00081         gmtDelta |= 0xff000000;
00082     }
00083     return gmtDelta;
00084 }
00085 
00086 void MacintoshInitializeTime(void)
00087 {
00088     /*
00089      * The NSPR epoch is midnight, Jan. 1, 1970 GMT.
00090      *
00091      * At midnight Jan. 1, 1970 GMT, the local time was
00092      *     midnight Jan. 1, 1970 + GMTDelta().
00093      *
00094      * Midnight Jan. 1, 1970 is 86400 * (365 * (1970 - 1904) + 17)
00095      *     = 2082844800 seconds since the Mac epoch.
00096      * (There were 17 leap years from 1904 to 1970.)
00097      *
00098      * So the NSPR epoch is 2082844800 + GMTDelta() seconds since
00099      * the Mac epoch.  Whew! :-)
00100      */
00101     gJanuaryFirst1970Seconds = 2082844800 + GMTDelta();
00102 }
00103 
00104 /*
00105  *-----------------------------------------------------------------------
00106  *
00107  * PR_Now --
00108  *
00109  *     Returns the current time in microseconds since the epoch.
00110  *     The epoch is midnight January 1, 1970 GMT.
00111  *     The implementation is machine dependent.  This is the Mac
00112  *     Implementation.
00113  *     Cf. time_t time(time_t *tp)
00114  *
00115  *-----------------------------------------------------------------------
00116  */
00117 
00118 PRTime PR_Now(void)
00119 {
00120     unsigned long currentTime;    /* unsigned 32-bit integer, ranging
00121                                      from midnight Jan. 1, 1904 to 
00122                                      6:28:15 AM Feb. 6, 2040 */
00123     PRTime retVal;
00124     int64  usecPerSec;
00125 
00126     /*
00127      * Get the current time expressed as the number of seconds
00128      * elapsed since the Mac epoch, midnight, Jan. 1, 1904 (local time).
00129      * On a Mac, current time accuracy is up to a second.
00130      */
00131     GetDateTime(&currentTime);
00132 
00133     /*
00134      * Express the current time relative to the NSPR epoch,
00135      * midnight, Jan. 1, 1970 GMT.
00136      *
00137      * At midnight Jan. 1, 1970 GMT, the local time was
00138      *     midnight Jan. 1, 1970 + GMTDelta().
00139      *
00140      * Midnight Jan. 1, 1970 is 86400 * (365 * (1970 - 1904) + 17)
00141      *     = 2082844800 seconds since the Mac epoch.
00142      * (There were 17 leap years from 1904 to 1970.)
00143      *
00144      * So the NSPR epoch is 2082844800 + GMTDelta() seconds since
00145      * the Mac epoch.  Whew! :-)
00146      */
00147     currentTime = currentTime - 2082844800 - GMTDelta();
00148 
00149     /* Convert seconds to microseconds */
00150     LL_I2L(usecPerSec, PR_USEC_PER_SEC);
00151     LL_I2L(retVal, currentTime);
00152     LL_MUL(retVal, retVal, usecPerSec);
00153 
00154     return retVal;
00155 }
00156 
00157 /*
00158  *-------------------------------------------------------------------------
00159  *
00160  * PR_LocalTimeParameters --
00161  * 
00162  *     returns the time parameters for the local time zone
00163  *
00164  *     This is the machine-dependent implementation for Mac.
00165  *
00166  *     Caveat: On a Mac, we only know the GMT and DST offsets for
00167  *     the current time, not for the time in question.
00168  *     Mac has no support for DST handling.
00169  *     DST changeover is all manually set by the user.
00170  *
00171  *-------------------------------------------------------------------------
00172  */
00173 
00174 PRTimeParameters PR_LocalTimeParameters(const PRExplodedTime *gmt)
00175 {
00176 #pragma unused (gmt)
00177 
00178     PRTimeParameters retVal;
00179     MachineLocation loc;
00180 
00181     MyReadLocation(&loc);
00182 
00183     /* 
00184      * On a Mac, the GMT value is in seconds east of GMT.  For example,
00185      * San Francisco is at -28,800 seconds (8 hours * 3600 seconds per hour)
00186      * east of GMT.  The gmtDelta field is a 3-byte value contained in a
00187      * long word, so you must take care to get it properly.
00188      */
00189 
00190     retVal.tp_gmt_offset = loc.u.gmtDelta & 0x00ffffff;
00191     if (retVal.tp_gmt_offset & 0x00800000) {    /* test sign extend bit */
00192        retVal.tp_gmt_offset |= 0xff000000;
00193     }
00194 
00195     /*
00196      * The daylight saving time value, dlsDelta, is a signed byte
00197      * value representing the offset for the hour field -- whether
00198      * to add 1 hour, subtract 1 hour, or make no change at all.
00199      */
00200 
00201     if (loc.u.dlsDelta) {
00202        retVal.tp_gmt_offset -= 3600;
00203        retVal.tp_dst_offset = 3600;
00204     } else {
00205        retVal.tp_dst_offset = 0;
00206     }
00207     return retVal;
00208 }
00209 
00210 PRIntervalTime _MD_GetInterval(void)
00211 {
00212     PRIntervalTime retVal;
00213     PRUint64 upTime, microtomilli; 
00214 
00215     /*
00216      * Use the Microseconds procedure to obtain the number of
00217      * microseconds elapsed since system startup time.
00218      */
00219     Microseconds((UnsignedWide *)&upTime);
00220     LL_I2L(microtomilli, PR_USEC_PER_MSEC);
00221     LL_DIV(upTime, upTime, microtomilli);
00222     LL_L2I(retVal, upTime);
00223        
00224     return retVal;
00225 }
00226 
00227 struct tm *Maclocaltime(const time_t * t)
00228 {
00229        DateTimeRec dtr;
00230        MachineLocation loc;
00231        time_t macLocal = *t + gJanuaryFirst1970Seconds; /* GMT Mac */
00232        static struct tm statictime;
00233        static const short monthday[12] =
00234               {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
00235 
00236        SecondsToDate(macLocal, &dtr);
00237        statictime.tm_sec = dtr.second;
00238        statictime.tm_min = dtr.minute;
00239        statictime.tm_hour = dtr.hour;
00240        statictime.tm_mday = dtr.day;
00241        statictime.tm_mon = dtr.month - 1;
00242        statictime.tm_year = dtr.year - 1900;
00243        statictime.tm_wday = dtr.dayOfWeek - 1;
00244        statictime.tm_yday = monthday[statictime.tm_mon]
00245               + statictime.tm_mday - 1;
00246        if (2 < statictime.tm_mon && !(statictime.tm_year & 3))
00247               ++statictime.tm_yday;
00248        MyReadLocation(&loc);
00249        statictime.tm_isdst = loc.u.dlsDelta;
00250        return(&statictime);
00251 }
00252 
00253