prmjtime.c revision 6b15695578f07a3f72c4c9475c1a261a3021472a
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla Communicator client code, released
* March 31, 1998.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
/*
* PR time code.
*/
#include "jsstddef.h"
#ifdef SOLARIS
#define _REENTRANT 1
#endif
#include <string.h>
#include <time.h>
#include "jstypes.h"
#include "jsutil.h"
#include "jsprf.h"
#include "prmjtime.h"
#define PRMJ_DO_MILLISECONDS 1
#ifdef XP_OS2
#endif
#ifdef XP_WIN
#include <windef.h>
#include <winbase.h>
#endif
#ifdef XP_MAC
#include <OSUtils.h>
#include <TextUtils.h>
#include <Resources.h>
#include <Timer.h>
#include <UTCUtils.h>
#include <Power.h>
#include <CodeFragments.h>
#if !TARGET_CARBON
#include <Traps.h>
#endif
#endif
#ifdef _SVID_GETTOD /* Defined only on Solaris, see Solaris <sys/types.h> */
#endif
#endif /* XP_UNIX */
#ifdef XP_MAC
static uint64 dstLocalBaseMicroseconds;
static unsigned long gJanuaryFirst1970Seconds;
static void MacintoshInitializeTime(void)
{
unsigned long currentLocalTimeSeconds,
/*
* Figure out in local time what time the machine started up. This information can be added to
* upTime to figure out the current local time as well as GMT.
*/
/* Make sure that we normalize the macintosh base seconds to the unix base of January 1, 1970.
*/
/* Now convert the startup time into a wide so that we can figure out GMT and DST.
*/
}
{
/* just woke up from sleeping, so must recompute dstLocalBaseMicroseconds. */
if (message == kSleepWakeUp)
return 0;
}
/* Because serial port and SLIP conflict with ReadXPram calls,
* we cache the call here
*/
{
if (!didReadLocation)
{
/* install a sleep queue routine, so that when the machine wakes up, time can be recomputed. */
if (&SleepQInstall != (void*)kUnresolvedCFragSymbolAddress
#if !TARGET_CARBON
#endif
) {
}
}
}
}
#ifndef XP_MACOSX
/* CFM library init and terminate routines. We'll use the terminate routine
to clean up the sleep Q entry. On Mach-O, the sleep Q entry gets cleaned
up for us, so nothing to do there.
*/
extern pascal void __NSTerminate();
pascal void __JSTerminate(void);
{
return __NSInitialize(initBlock);
}
pascal void __JSTerminate()
{
/* clean up the sleepQ entry */
}
#endif /* XP_MACOSX */
#endif /* XP_MAC */
#define PRMJ_HOUR_SECONDS 3600L
/* function prototypes */
/*
* get the difference in seconds between this time zone and UTC (GMT)
*/
{
/* get the difference between this time zone and GMT */
#ifdef SUNOS4
#else
#endif
#endif
#if defined(XP_MAC)
/* difference has been set no need to recalculate */
if (zone != -1)
return zone;
/* Get the information about the local machine, including
* its GMT offset and its daylight savings time info.
* Convert each into wides that we can add to
* startupTimeMicroSeconds.
*/
/* Mask off top eight bits of gmtDelta, sign extend lower three. */
gmtOffsetSeconds >>= 8;
/* Backout OS adjustment for DST, to give consistent GMT offset. */
if (machineLocation.u.dlsDelta != 0)
return (zone = -gmtOffsetSeconds);
#endif
}
/* Constants for GMT offset from 1970 */
/* Convert from base time to extended time */
static JSInt64
{
#ifndef JS_HAVE_LONG_LONG
#else
#endif
return exttime;
}
PRMJ_Now(void)
{
#ifdef XP_OS2
struct timeb b;
#endif
#ifdef XP_WIN
#endif
#endif /* XP_UNIX */
#ifdef XP_MAC
#endif /* XP_MAC */
#ifdef XP_OS2
ftime(&b);
return s;
#endif
#ifdef XP_WIN
/* The windows epoch is around 1600. The unix epoch is around 1970.
win2un is the difference (in windows time units which are 10 times
more precise than the JS time unit) */
/* Win9x gets confused at midnight
So if the low part (precision <8mins) is 0 then we get the time
again. */
if (!time.dwLowDateTime) {
}
JSLL_SHL(s, s, 32);
return s;
#endif
#ifdef _SVID_GETTOD /* Defined only on Solaris, see Solaris <sys/types.h> */
gettimeofday(&tv);
#else
gettimeofday(&tv, 0);
#endif /* _SVID_GETTOD */
return s;
#endif /* XP_UNIX */
#ifdef XP_MAC
/* don't adjust for DST since it sets ctime and gmtime off on the MAC */
#endif /* XP_MAC */
}
/* Get the DST timezone offset for the time passed in */
{
#ifdef XP_MAC
/*
* Convert the local time passed in to Macintosh epoch seconds. Use UTC utilities to convert
* to UTC time, then compare difference with our GMT offset. If they are the same, then
*/
return 0;
else {
return dlsOffset;
}
#else
#ifndef HAVE_LOCALTIME_R
#endif
/* get the maximum of time_t value */
} else if(!JSLL_GE_ZERO(local_time)){
/*go ahead a day to make localtime work (does not work with 0) */
}
#ifndef HAVE_LOCALTIME_R
if(!ptm){
return JSLL_ZERO;
}
#else
#endif
if(diff < 0){
diff += PRMJ_DAY_SECONDS;
}
return(local_time);
#endif
}
/* Format a time value into a buffer. Same semantics as strftime() */
{
struct tm a;
/* Zero out the tm struct. Linux, SunOS 4 struct tm has extra members int
* tm_gmtoff, char *tm_zone; when tm_zone is garbage, strftime gets
* confused and dumps core. NSPR20 prtime.c attempts to fill these in by
* calling mktime on the partially filled struct, but this doesn't seem to
* work as well; the result string has "can't get timezone" for ECMA-valid
* years. Might still make sense to use this, but find the range of years
* for which valid tz information exists, and map (per ECMA hint) from the
* given year into that range.
* N.B. This hasn't been tested with anything that actually _uses_
* tm_gmtoff; zero might be the wrong thing to set it to if you really need
* to format a time. This fix is for jsdate.c, which only uses
* JS_FormatTime to get a string representing the time zone. */
/* Even with the above, SunOS 4 seems to detonate if tm_zone and tm_gmtoff
* are null. This doesn't quite work, though - the timezone is off by
* tzoff + dst. (And mktime seems to return -1 for the exact dst
* changeover time.)
*/
#if defined(SUNOS4)
if (mktime(&a) == -1) {
/* Seems to fail whenever the requested date is outside of the 32-bit
* UNIX epoch. We could proceed at this point (setting a.tm_zone to
* "") but then strftime returns a string with a 2-digit field of
* garbage for the year. So we return 0 and hope jsdate.c
* will fall back on toString.
*/
return 0;
}
#endif
#endif
}
/* table for number of days in a month */
static int mtab[] = {
/* jan, feb,mar,apr,may,jun */
31,28,31,30,31,30,
/* july,aug,sep,oct,nov,dec */
31,31,30,31,30,31
};
/*
* basic time calculation functionality for localtime and gmtime
* setups up prtm argument with correct values based upon input number
* of seconds.
*/
static void
{
/* convert tsecs back to year,month,day,hour,secs */
/* get the base time via UTC */
base = PRMJ_ToExtendedTime(0);
/* get the year */
/* subtract a year from tsecs */
days += 365;
/* is it a leap year ? */
days++;
}
year++;
}
/* let's find the month */
/* it's a Feb, check if this is a leap year */
yday++;
days++;
mday--;
}
month++;
}
/* now adjust tsecs */
mday++; /* day of month always start with 1 */
/* get the hours */
/* get minutes */
}