Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtdocbook / tcl / tclUnixTime.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
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)
10  * any later version.
11  *
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
16  * details.
17  *
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
22  */
23 /* $TOG: tclUnixTime.c /main/3 1998/04/06 13:37:56 mgreess $ */
24 /* 
25  * tclUnixTime.c --
26  *
27  *      Contains Unix specific versions of Tcl functions that
28  *      obtain time values from the operating system.
29  *
30  * Copyright (c) 1995 Sun Microsystems, Inc.
31  *
32  * See the file "license.terms" for information on usage and redistribution
33  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
34  *
35  * SCCS: @(#) tclUnixTime.c 1.10 96/02/15 11:58:41
36  */
37
38 #include <sys/time.h>
39 #include "tclInt.h"
40 #include "tclPort.h"
41 \f
42 /*
43  *-----------------------------------------------------------------------------
44  *
45  * TclGetSeconds --
46  *
47  *      This procedure returns the number of seconds from the epoch.  On
48  *      most Unix systems the epoch is Midnight Jan 1, 1970 GMT.
49  *
50  * Results:
51  *      Number of seconds from the epoch.
52  *
53  * Side effects:
54  *      None.
55  *
56  *-----------------------------------------------------------------------------
57  */
58
59 unsigned long
60 TclGetSeconds()
61 {
62     return time((time_t *) NULL);
63 }
64 \f
65 /*
66  *-----------------------------------------------------------------------------
67  *
68  * TclGetClicks --
69  *
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.
74  *
75  * Results:
76  *      Number of clicks from some start time.
77  *
78  * Side effects:
79  *      None.
80  *
81  *-----------------------------------------------------------------------------
82  */
83
84 unsigned long
85 TclGetClicks()
86 {
87     unsigned long now;
88 #ifdef NO_GETTOD
89     struct tms dummy;
90 #else
91     struct timeval date;
92     struct timezone tz;
93 #endif
94
95 #ifdef NO_GETTOD
96     now = (unsigned long) times(&dummy);
97 #else
98     gettimeofday(&date, &tz);
99     now = date.tv_sec*1000000 + date.tv_usec;
100 #endif
101
102     return now;
103 }
104 \f
105 /*
106  *----------------------------------------------------------------------
107  *
108  * TclGetTimeZone --
109  *
110  *      Determines the current timezone.  The method varies wildly
111  *      between different platform implementations, so its hidden in
112  *      this function.
113  *
114  * Results:
115  *      Hours east of GMT.
116  *
117  * Side effects:
118  *      None.
119  *
120  *----------------------------------------------------------------------
121  */
122
123 int
124 TclGetTimeZone (currentTime)
125     unsigned long  currentTime;
126 {
127     /*
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
133      * zone.
134      */
135     
136 #if defined(HAVE_TM_TZADJ)
137 #   define TCL_GOT_TIMEZONE
138     time_t      curTime = (time_t) currentTime;
139     struct tm  *timeDataPtr = localtime(&curTime);
140     int         timeZone;
141
142     timeZone = timeDataPtr->tm_tzadj  / 60;
143     if (timeDataPtr->tm_isdst) {
144         timeZone += 60;
145     }
146     
147     return timeZone;
148 #endif
149
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(&currentTime);
154     int        timeZone;
155
156     timeZone = -(timeDataPtr->tm_gmtoff / 60);
157     if (timeDataPtr->tm_isdst) {
158         timeZone += 60;
159     }
160     
161     return timeZone;
162 #endif
163
164     /*
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.
168      */
169     
170 #if defined(HAVE_TIMEZONE_VAR) && !defined (TCL_GOT_TIMEZONE)
171 #   define TCL_GOT_TIMEZONE
172     static int setTZ = 0;
173     int        timeZone;
174
175     if (!setTZ) {
176         tzset();
177         setTZ = 1;
178     }
179
180     /*
181      * Note: this is not a typo in "timezone" below!  See tzset
182      * documentation for details.
183      */
184
185     timeZone = timezone / 60;
186
187     return timeZone;
188 #endif
189
190 #if defined(HAVE_GETTIMEOFDAY) && !defined (TCL_GOT_TIMEZONE)
191 #   define TCL_GOT_TIMEZONE
192     struct timeval  tv;
193     struct timezone tz;
194     int timeZone;
195
196     gettimeofday(&tv, &tz);
197     timeZone = tz.tz_minuteswest;
198     if (tz.tz_dsttime) {
199         timeZone += 60;
200     }
201     
202     return timeZone;
203 #endif
204
205 #ifndef TCL_GOT_TIMEZONE
206     /*
207      * Cause compile error, we don't know how to get timezone.
208      */
209     error: autoconf did not figure out how to determine the timezone. 
210 #endif
211
212 }
213 \f
214 /*
215  *----------------------------------------------------------------------
216  *
217  * TclGetTime --
218  *
219  *      Gets the current system time in seconds and microseconds
220  *      since the beginning of the epoch: 00:00 UCT, January 1, 1970.
221  *
222  * Results:
223  *      Returns the current time in timePtr.
224  *
225  * Side effects:
226  *      None.
227  *
228  *----------------------------------------------------------------------
229  */
230
231 void
232 TclGetTime(timePtr)
233     Tcl_Time *timePtr;          /* Location to store time information. */
234 {
235     struct timeval tv;
236     struct timezone tz;
237     
238     (void) gettimeofday(&tv, &tz);
239     timePtr->sec = tv.tv_sec;
240     timePtr->usec = tv.tv_usec;
241 }