4 * Contains Unix specific versions of Tcl functions that
5 * obtain time values from the operating system.
7 * Copyright (c) 1995 Sun Microsystems, Inc.
9 * See the file "license.terms" for information on usage and redistribution
10 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
17 #define TM_YEAR_BASE 1900
18 #define IsLeapYear(x) ((x % 4 == 0) && (x % 100 != 0 || x % 400 == 0))
21 *-----------------------------------------------------------------------------
25 * This procedure returns the number of seconds from the epoch. On
26 * most Unix systems the epoch is Midnight Jan 1, 1970 GMT.
29 * Number of seconds from the epoch.
34 *-----------------------------------------------------------------------------
40 return time((time_t *) NULL);
44 *-----------------------------------------------------------------------------
48 * This procedure returns a value that represents the highest resolution
49 * clock available on the system. There are no garantees on what the
50 * resolution will be. In Tcl we will call this value a "click". The
51 * start time is also system dependant.
54 * Number of clicks from some start time.
59 *-----------------------------------------------------------------------------
74 now = (unsigned long) times(&dummy);
76 gettimeofday(&date, &tz);
77 now = date.tv_sec*1000000 + date.tv_usec;
84 *----------------------------------------------------------------------
88 * Determines the current timezone. The method varies wildly
89 * between different platform implementations, so its hidden in
93 * The return value is the local time zone, measured in
94 * minutes away from GMT (-ve for east, +ve for west).
99 *----------------------------------------------------------------------
103 TclpGetTimeZone (currentTime)
104 unsigned long currentTime;
107 * Determine how a timezone is obtained from "struct tm". If there is no
108 * time zone in this struct (very lame) then use the timezone variable.
109 * This is done in a way to make the timezone variable the method of last
110 * resort, as some systems have it in addition to a field in "struct tm".
111 * The gettimeofday system call can also be used to determine the time
115 #if defined(HAVE_TM_TZADJ)
116 # define TCL_GOT_TIMEZONE
117 time_t curTime = (time_t) currentTime;
118 struct tm *timeDataPtr = localtime(&curTime);
121 timeZone = timeDataPtr->tm_tzadj / 60;
122 if (timeDataPtr->tm_isdst) {
129 #if defined(HAVE_TM_GMTOFF) && !defined (TCL_GOT_TIMEZONE)
130 # define TCL_GOT_TIMEZONE
131 time_t curTime = (time_t) currentTime;
132 struct tm *timeDataPtr = localtime(&curTime);
135 timeZone = -(timeDataPtr->tm_gmtoff / 60);
136 if (timeDataPtr->tm_isdst) {
143 #if defined(USE_DELTA_FOR_TZ)
144 #define TCL_GOT_TIMEZONE 1
146 * This hack replaces using global var timezone or gettimeofday
147 * in situations where they are buggy such as on AIX when libbsd.a
154 tt = 849268800L; /* 1996-11-29 12:00:00 GMT */
155 stm = localtime(&tt); /* eg 1996-11-29 6:00:00 CST6CDT */
156 /* The calculation below assumes a max of +12 or -12 hours from GMT */
157 timeZone = (12 - stm->tm_hour)*60 + (0 - stm->tm_min);
158 return timeZone; /* eg +360 for CST6CDT */
162 * Must prefer timezone variable over gettimeofday, as gettimeofday does
163 * not return timezone information on many systems that have moved this
164 * information outside of the kernel.
167 #if defined(HAVE_TIMEZONE_VAR) && !defined (TCL_GOT_TIMEZONE)
168 # define TCL_GOT_TIMEZONE
169 static int setTZ = 0;
171 static Tcl_Mutex tzMutex;
175 Tcl_MutexLock(&tzMutex);
180 Tcl_MutexUnlock(&tzMutex);
183 * Note: this is not a typo in "timezone" below! See tzset
184 * documentation for details.
187 timeZone = timezone / 60;
192 #if !defined(NO_GETTOD) && !defined (TCL_GOT_TIMEZONE)
193 # define TCL_GOT_TIMEZONE
198 gettimeofday(&tv, &tz);
199 timeZone = tz.tz_minuteswest;
207 #ifndef TCL_GOT_TIMEZONE
209 * Cause compile error, we don't know how to get timezone.
211 error: autoconf did not figure out how to determine the timezone.
217 *----------------------------------------------------------------------
221 * Gets the current system time in seconds and microseconds
222 * since the beginning of the epoch: 00:00 UCT, January 1, 1970.
225 * Returns the current time in timePtr.
230 *----------------------------------------------------------------------
235 Tcl_Time *timePtr; /* Location to store time information. */
240 (void) gettimeofday(&tv, &tz);
241 timePtr->sec = tv.tv_sec;
242 timePtr->usec = tv.tv_usec;
246 *----------------------------------------------------------------------
250 * This function converts between seconds and struct tm. If
251 * useGMT is true, then the returned date will be in Greenwich
252 * Mean Time (GMT). Otherwise, it will be in the local time zone.
255 * Returns a static tm structure.
260 *----------------------------------------------------------------------
264 TclpGetDate(time, useGMT)
268 CONST time_t *tp = (CONST time_t *)time;
273 return localtime(tp);
278 *----------------------------------------------------------------------
282 * On Unix, we can safely call the native strftime implementation.
285 * The normal strftime result.
290 *----------------------------------------------------------------------
294 TclpStrftime(s, maxsize, format, t)
300 if (format[0] == '%' && format[1] == 'Q') {
301 /* Format as a stardate */
302 sprintf(s, "Stardate %2d%03d.%01d",
303 (((t->tm_year + TM_YEAR_BASE) + 377) - 2323),
304 (((t->tm_yday + 1) * 1000) /
305 (365 + IsLeapYear((t->tm_year + TM_YEAR_BASE)))),
306 (((t->tm_hour * 60) + t->tm_min)/144));
309 return strftime(s, maxsize, format, t);