Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/lib/libtksh/tcl/tclUnixTime.c
1810 views
1
/*
2
* tclUnixTime.c --
3
*
4
* Contains Unix specific versions of Tcl functions that
5
* obtain time values from the operating system.
6
*
7
* Copyright (c) 1995 Sun Microsystems, Inc.
8
*
9
* See the file "license.terms" for information on usage and redistribution
10
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
11
*
12
* SCCS: @(#) tclUnixTime.c 1.11 96/07/23 16:17:21
13
*/
14
15
#if _PACKAGE_ast
16
17
#include <ast.h>
18
#include <tm.h>
19
#include <tv.h>
20
21
#include "tcl.h"
22
23
/*
24
* see function comments below for details
25
*/
26
27
unsigned long
28
TclpGetSeconds()
29
{
30
return time(NiL);
31
}
32
33
unsigned long
34
TclpGetClicks()
35
{
36
Tv_t tv;
37
38
tvgettime(&tv);
39
return 1000000 * tv.tv_sec + tv.tv_nsec / 1000;
40
}
41
42
int
43
TclpGetTimeZone(now)
44
unsigned long now;
45
{
46
tmset(tm_info.zone);
47
return tm_info.zone->west;
48
}
49
50
void
51
TclpGetTime(tp)
52
Tcl_Time* tp;
53
{
54
Tv_t tv;
55
56
tvgettime(&tv);
57
tp->sec = tv.tv_sec;
58
tp->usec = tv.tv_nsec / 1000;
59
}
60
61
#else /* _PACKAGE_ast */
62
63
#include "tclInt.h"
64
#include "tclPort.h"
65
66
/*
67
*-----------------------------------------------------------------------------
68
*
69
* TclpGetSeconds --
70
*
71
* This procedure returns the number of seconds from the epoch. On
72
* most Unix systems the epoch is Midnight Jan 1, 1970 GMT.
73
*
74
* Results:
75
* Number of seconds from the epoch.
76
*
77
* Side effects:
78
* None.
79
*
80
*-----------------------------------------------------------------------------
81
*/
82
83
unsigned long
84
TclpGetSeconds()
85
{
86
return time((time_t *) NULL);
87
}
88
89
/*
90
*-----------------------------------------------------------------------------
91
*
92
* TclpGetClicks --
93
*
94
* This procedure returns a value that represents the highest resolution
95
* clock available on the system. There are no garantees on what the
96
* resolution will be. In Tcl we will call this value a "click". The
97
* start time is also system dependant.
98
*
99
* Results:
100
* Number of clicks from some start time.
101
*
102
* Side effects:
103
* None.
104
*
105
*-----------------------------------------------------------------------------
106
*/
107
108
unsigned long
109
TclpGetClicks()
110
{
111
unsigned long now;
112
#ifdef NO_GETTOD
113
struct tms dummy;
114
#else
115
struct timeval date;
116
struct timezone tz;
117
#endif
118
119
#ifdef NO_GETTOD
120
now = (unsigned long) times(&dummy);
121
#else
122
gettimeofday(&date, &tz);
123
now = date.tv_sec*1000000 + date.tv_usec;
124
#endif
125
126
return now;
127
}
128
129
/*
130
*----------------------------------------------------------------------
131
*
132
* TclpGetTimeZone --
133
*
134
* Determines the current timezone. The method varies wildly
135
* between different platform implementations, so its hidden in
136
* this function.
137
*
138
* Results:
139
* Hours east of GMT.
140
*
141
* Side effects:
142
* None.
143
*
144
*----------------------------------------------------------------------
145
*/
146
147
int
148
TclpGetTimeZone (currentTime)
149
unsigned long currentTime;
150
{
151
/*
152
* Determine how a timezone is obtained from "struct tm". If there is no
153
* time zone in this struct (very lame) then use the timezone variable.
154
* This is done in a way to make the timezone variable the method of last
155
* resort, as some systems have it in addition to a field in "struct tm".
156
* The gettimeofday system call can also be used to determine the time
157
* zone.
158
*/
159
160
#if defined(HAVE_TM_TZADJ)
161
# define TCL_GOT_TIMEZONE
162
time_t curTime = (time_t) currentTime;
163
struct tm *timeDataPtr = localtime(&curTime);
164
int timeZone;
165
166
timeZone = timeDataPtr->tm_tzadj / 60;
167
if (timeDataPtr->tm_isdst) {
168
timeZone += 60;
169
}
170
171
return timeZone;
172
#endif
173
174
#if defined(HAVE_TM_GMTOFF) && !defined (TCL_GOT_TIMEZONE)
175
# define TCL_GOT_TIMEZONE
176
time_t curTime = (time_t) currentTime;
177
struct tm *timeDataPtr = localtime(&currentTime);
178
int timeZone;
179
180
timeZone = -(timeDataPtr->tm_gmtoff / 60);
181
if (timeDataPtr->tm_isdst) {
182
timeZone += 60;
183
}
184
185
return timeZone;
186
#endif
187
188
/*
189
* Must prefer timezone variable over gettimeofday, as gettimeofday does
190
* not return timezone information on many systems that have moved this
191
* information outside of the kernel.
192
*/
193
194
#if defined(HAVE_TIMEZONE_VAR) && !defined (TCL_GOT_TIMEZONE)
195
# define TCL_GOT_TIMEZONE
196
static int setTZ = 0;
197
int timeZone;
198
199
if (!setTZ) {
200
tzset();
201
setTZ = 1;
202
}
203
204
/*
205
* Note: this is not a typo in "timezone" below! See tzset
206
* documentation for details.
207
*/
208
209
timeZone = timezone / 60;
210
211
return timeZone;
212
#endif
213
214
#if defined(HAVE_GETTIMEOFDAY) && !defined (TCL_GOT_TIMEZONE)
215
# define TCL_GOT_TIMEZONE
216
struct timeval tv;
217
struct timezone tz;
218
int timeZone;
219
220
gettimeofday(&tv, &tz);
221
timeZone = tz.tz_minuteswest;
222
if (tz.tz_dsttime) {
223
timeZone += 60;
224
}
225
226
return timeZone;
227
#endif
228
229
#ifndef TCL_GOT_TIMEZONE
230
/*
231
* Cause compile error, we don't know how to get timezone.
232
*/
233
error: autoconf did not figure out how to determine the timezone.
234
#endif
235
236
}
237
238
/*
239
*----------------------------------------------------------------------
240
*
241
* TclpGetTime --
242
*
243
* Gets the current system time in seconds and microseconds
244
* since the beginning of the epoch: 00:00 UCT, January 1, 1970.
245
*
246
* Results:
247
* Returns the current time in timePtr.
248
*
249
* Side effects:
250
* None.
251
*
252
*----------------------------------------------------------------------
253
*/
254
255
void
256
TclpGetTime(timePtr)
257
Tcl_Time *timePtr; /* Location to store time information. */
258
{
259
struct timeval tv;
260
struct timezone tz;
261
262
(void) gettimeofday(&tv, &tz);
263
timePtr->sec = tv.tv_sec;
264
timePtr->usec = tv.tv_usec;
265
}
266
267
#endif /* _PACKAGE_ast */
268
269