2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $TOG: tclUnixTime.c /main/3 1998/04/06 13:37:56 mgreess $ */
27 * Contains Unix specific versions of Tcl functions that
28 * obtain time values from the operating system.
30 * Copyright (c) 1995 Sun Microsystems, Inc.
32 * See the file "license.terms" for information on usage and redistribution
33 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
35 * SCCS: @(#) tclUnixTime.c 1.10 96/02/15 11:58:41
43 *-----------------------------------------------------------------------------
47 * This procedure returns the number of seconds from the epoch. On
48 * most Unix systems the epoch is Midnight Jan 1, 1970 GMT.
51 * Number of seconds from the epoch.
56 *-----------------------------------------------------------------------------
62 return time((time_t *) NULL);
66 *-----------------------------------------------------------------------------
70 * This procedure returns a value that represents the highest resolution
71 * clock available on the system. There are no garantees on what the
72 * resolution will be. In Tcl we will call this value a "click". The
73 * start time is also system dependant.
76 * Number of clicks from some start time.
81 *-----------------------------------------------------------------------------
96 now = (unsigned long) times(&dummy);
98 gettimeofday(&date, &tz);
99 now = date.tv_sec*1000000 + date.tv_usec;
106 *----------------------------------------------------------------------
110 * Determines the current timezone. The method varies wildly
111 * between different platform implementations, so its hidden in
120 *----------------------------------------------------------------------
124 TclGetTimeZone (currentTime)
125 unsigned long currentTime;
128 * Determine how a timezone is obtained from "struct tm". If there is no
129 * time zone in this struct (very lame) then use the timezone variable.
130 * This is done in a way to make the timezone variable the method of last
131 * resort, as some systems have it in addition to a field in "struct tm".
132 * The gettimeofday system call can also be used to determine the time
136 #if defined(HAVE_TM_TZADJ)
137 # define TCL_GOT_TIMEZONE
138 time_t curTime = (time_t) currentTime;
139 struct tm *timeDataPtr = localtime(&curTime);
142 timeZone = timeDataPtr->tm_tzadj / 60;
143 if (timeDataPtr->tm_isdst) {
150 #if defined(HAVE_TM_GMTOFF) && !defined (TCL_GOT_TIMEZONE)
151 # define TCL_GOT_TIMEZONE
152 time_t curTime = (time_t) currentTime;
153 struct tm *timeDataPtr = localtime(¤tTime);
156 timeZone = -(timeDataPtr->tm_gmtoff / 60);
157 if (timeDataPtr->tm_isdst) {
165 * Must prefer timezone variable over gettimeofday, as gettimeofday does
166 * not return timezone information on many systems that have moved this
167 * information outside of the kernel.
170 #if defined(HAVE_TIMEZONE_VAR) && !defined (TCL_GOT_TIMEZONE)
171 # define TCL_GOT_TIMEZONE
172 static int setTZ = 0;
181 * Note: this is not a typo in "timezone" below! See tzset
182 * documentation for details.
185 timeZone = timezone / 60;
190 #if defined(HAVE_GETTIMEOFDAY) && !defined (TCL_GOT_TIMEZONE)
191 # define TCL_GOT_TIMEZONE
196 gettimeofday(&tv, &tz);
197 timeZone = tz.tz_minuteswest;
205 #ifndef TCL_GOT_TIMEZONE
207 * Cause compile error, we don't know how to get timezone.
209 error: autoconf did not figure out how to determine the timezone.
215 *----------------------------------------------------------------------
219 * Gets the current system time in seconds and microseconds
220 * since the beginning of the epoch: 00:00 UCT, January 1, 1970.
223 * Returns the current time in timePtr.
228 *----------------------------------------------------------------------
233 Tcl_Time *timePtr; /* Location to store time information. */
238 (void) gettimeofday(&tv, &tz);
239 timePtr->sec = tv.tv_sec;
240 timePtr->usec = tv.tv_usec;