3253
|
1 /* |
|
2 |
|
3 Copyright (C) 1999 John W. Eaton |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
3253
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
3290
|
28 #include <climits> |
3253
|
29 #include <cmath> |
|
30 |
3607
|
31 #ifdef HAVE_UNISTD_H |
|
32 #ifdef HAVE_SYS_TYPES_H |
|
33 #include <sys/types.h> |
|
34 #endif |
|
35 #include <unistd.h> |
|
36 #endif |
|
37 |
4101
|
38 #if defined (OCTAVE_USE_WINDOWS_API) |
4085
|
39 #include <windows.h> |
6113
|
40 #undef min |
|
41 #undef max |
4085
|
42 #endif |
|
43 |
3253
|
44 #include "lo-error.h" |
|
45 #include "lo-utils.h" |
|
46 #include "oct-time.h" |
|
47 |
|
48 octave_time::octave_time (const octave_base_tm& tm) |
|
49 { |
|
50 struct tm t; |
|
51 |
|
52 t.tm_sec = tm.sec (); |
|
53 t.tm_min = tm.min (); |
|
54 t.tm_hour = tm.hour (); |
|
55 t.tm_mday = tm.mday (); |
|
56 t.tm_mon = tm.mon (); |
|
57 t.tm_year = tm.year (); |
|
58 t.tm_wday = tm.wday (); |
|
59 t.tm_yday = tm.yday (); |
|
60 t.tm_isdst = tm.isdst (); |
|
61 |
3887
|
62 #if defined (HAVE_STRUCT_TM_TM_ZONE) |
3504
|
63 std::string s = tm.zone (); |
3344
|
64 char *ps = strsave (s.c_str ()); |
|
65 t.tm_zone = ps; |
3253
|
66 #endif |
|
67 |
|
68 ot_unix_time = mktime (&t); |
|
69 |
3887
|
70 #if defined (HAVE_STRUCT_TM_TM_ZONE) |
3344
|
71 delete [] ps; |
|
72 #endif |
|
73 |
3253
|
74 ot_usec = tm.usec (); |
|
75 } |
|
76 |
3504
|
77 std::string |
3255
|
78 octave_time::ctime (void) const |
|
79 { |
|
80 return octave_localtime (*this) . asctime (); |
|
81 } |
|
82 |
3253
|
83 void |
|
84 octave_time::stamp (void) |
|
85 { |
|
86 #if defined (HAVE_GETTIMEOFDAY) |
|
87 |
|
88 struct timeval tp; |
|
89 |
|
90 #if defined (GETTIMEOFDAY_NO_TZ) |
|
91 gettimeofday (&tp); |
|
92 #else |
|
93 gettimeofday (&tp, 0); |
|
94 #endif |
|
95 |
|
96 ot_unix_time = tp.tv_sec; |
4085
|
97 ot_usec = tp.tv_usec; |
3253
|
98 |
4101
|
99 #elif defined (OCTAVE_USE_WINDOWS_API) |
4085
|
100 |
|
101 // Loosely based on the code from Cygwin |
|
102 // Copyright 1996-2002 Red Hat, Inc. |
|
103 // Licenced under the GPL. |
|
104 |
|
105 const LONGLONG TIME_OFFSET = 0x19db1ded53e8000LL; |
|
106 |
|
107 static int init = 1; |
|
108 static LARGE_INTEGER base; |
|
109 static LARGE_INTEGER t0; |
|
110 static double dt; |
|
111 |
|
112 if (init) |
|
113 { |
|
114 LARGE_INTEGER ifreq; |
|
115 |
|
116 if (QueryPerformanceFrequency (&ifreq)) |
|
117 { |
|
118 // Get clock frequency |
|
119 dt = (double) 1000000.0 / (double) ifreq.QuadPart; |
|
120 |
|
121 // Get base time as microseconds from Jan 1. 1970 |
|
122 int priority = GetThreadPriority (GetCurrentThread ()); |
|
123 SetThreadPriority (GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL); |
|
124 if (QueryPerformanceCounter (&base)) |
|
125 { |
|
126 FILETIME f; |
|
127 |
|
128 GetSystemTimeAsFileTime (&f); |
|
129 |
|
130 t0.HighPart = f.dwHighDateTime; |
|
131 t0.LowPart = f.dwLowDateTime; |
|
132 t0.QuadPart -= TIME_OFFSET; |
|
133 t0.QuadPart /= 10; |
|
134 |
|
135 init = 0; |
|
136 } |
|
137 |
|
138 SetThreadPriority (GetCurrentThread (), priority); |
|
139 } |
|
140 |
|
141 if (! init) |
|
142 { |
|
143 ot_unix_time = time (0); |
|
144 ot_usec = 0; |
|
145 |
|
146 return; |
|
147 } |
|
148 } |
|
149 |
|
150 LARGE_INTEGER now; |
|
151 |
|
152 if (QueryPerformanceCounter (&now)) |
|
153 { |
|
154 now.QuadPart = (LONGLONG) (dt * (double)(now.QuadPart - base.QuadPart)); |
|
155 now.QuadPart += t0.QuadPart; |
|
156 |
|
157 ot_unix_time = now.QuadPart / 1000000LL; |
|
158 ot_usec = now.QuadPart % 1000000LL; |
|
159 } |
|
160 else |
|
161 { |
|
162 ot_unix_time = time (0); |
|
163 ot_usec = 0; |
|
164 } |
3253
|
165 |
|
166 #else |
|
167 |
|
168 ot_unix_time = time (0); |
|
169 |
|
170 #endif |
|
171 } |
|
172 |
3736
|
173 // From the mktime() manual page: |
|
174 // |
|
175 // The mktime() function converts a broken-down time structure, |
|
176 // expressed as local time, to calendar time representation. |
|
177 // |
|
178 // <snip> |
|
179 // |
|
180 // If structure members are outside their legal interval, they |
|
181 // will be normalized (so that, e.g., 40 October is changed into |
|
182 // 9 November). |
|
183 // |
|
184 // So, we no longer check limits here. |
|
185 |
|
186 #if 0 |
3253
|
187 #define DEFINE_SET_INT_FIELD_FCN(f, lo, hi) \ |
|
188 octave_base_tm& \ |
|
189 octave_base_tm::f (int v) \ |
|
190 { \ |
|
191 if (v < lo || v > hi) \ |
|
192 (*current_liboctave_error_handler) \ |
|
193 ("invalid value specified for " #f); \ |
|
194 \ |
|
195 tm_ ## f = v; \ |
|
196 \ |
|
197 return *this; \ |
|
198 } |
3736
|
199 #else |
|
200 #define DEFINE_SET_INT_FIELD_FCN(f, lo, hi) \ |
|
201 octave_base_tm& \ |
|
202 octave_base_tm::f (int v) \ |
|
203 { \ |
|
204 tm_ ## f = v; \ |
|
205 \ |
|
206 return *this; \ |
|
207 } |
|
208 #endif |
3253
|
209 |
|
210 DEFINE_SET_INT_FIELD_FCN (usec, 0, 1000000) |
|
211 DEFINE_SET_INT_FIELD_FCN (sec, 0, 61) |
|
212 DEFINE_SET_INT_FIELD_FCN (min, 0, 59) |
|
213 DEFINE_SET_INT_FIELD_FCN (hour, 0, 23) |
|
214 DEFINE_SET_INT_FIELD_FCN (mday, 1, 31) |
|
215 DEFINE_SET_INT_FIELD_FCN (mon, 0, 11) |
|
216 DEFINE_SET_INT_FIELD_FCN (year, INT_MIN, INT_MAX) |
|
217 DEFINE_SET_INT_FIELD_FCN (wday, 0, 6) |
|
218 DEFINE_SET_INT_FIELD_FCN (yday, 0, 365) |
|
219 DEFINE_SET_INT_FIELD_FCN (isdst, 0, 1) |
|
220 |
|
221 octave_base_tm& |
3504
|
222 octave_base_tm::zone (const std::string& s) |
3253
|
223 { |
|
224 tm_zone = s; |
|
225 return *this; |
|
226 } |
|
227 |
|
228 #if !defined STRFTIME_BUF_INITIAL_SIZE |
|
229 #define STRFTIME_BUF_INITIAL_SIZE 128 |
|
230 #endif |
|
231 |
3504
|
232 std::string |
|
233 octave_base_tm::strftime (const std::string& fmt) const |
3253
|
234 { |
3504
|
235 std::string retval; |
3253
|
236 |
3709
|
237 if (! fmt.empty ()) |
|
238 { |
|
239 struct tm t; |
3253
|
240 |
3709
|
241 t.tm_sec = tm_sec; |
|
242 t.tm_min = tm_min; |
|
243 t.tm_hour = tm_hour; |
|
244 t.tm_mday = tm_mday; |
|
245 t.tm_mon = tm_mon; |
|
246 t.tm_year = tm_year; |
|
247 t.tm_wday = tm_wday; |
|
248 t.tm_yday = tm_yday; |
|
249 t.tm_isdst = tm_isdst; |
3253
|
250 |
3887
|
251 #if defined (HAVE_STRUCT_TM_TM_ZONE) |
3709
|
252 char *ps = strsave (tm_zone.c_str ()); |
|
253 t.tm_zone = ps; |
3253
|
254 #endif |
|
255 |
3709
|
256 const char *fmt_str = fmt.c_str (); |
3253
|
257 |
3709
|
258 char *buf = 0; |
|
259 size_t bufsize = STRFTIME_BUF_INITIAL_SIZE; |
|
260 size_t chars_written = 0; |
3253
|
261 |
3709
|
262 while (chars_written == 0) |
|
263 { |
|
264 delete [] buf; |
|
265 buf = new char[bufsize]; |
|
266 buf[0] = '\0'; |
3253
|
267 |
3709
|
268 chars_written = ::strftime (buf, bufsize, fmt_str, &t); |
3253
|
269 |
3709
|
270 bufsize *= 2; |
|
271 } |
3253
|
272 |
3887
|
273 #if defined (HAVE_STRUCT_TM_TM_ZONE) |
3709
|
274 delete [] ps; |
3344
|
275 #endif |
|
276 |
3709
|
277 retval = buf; |
3253
|
278 |
3709
|
279 delete [] buf; |
|
280 } |
3253
|
281 |
|
282 return retval; |
|
283 } |
|
284 |
|
285 void |
|
286 octave_base_tm::init (void *p) |
|
287 { |
|
288 struct tm *t = static_cast<struct tm*> (p); |
|
289 |
|
290 tm_sec = t->tm_sec; |
|
291 tm_min = t->tm_min; |
|
292 tm_hour = t->tm_hour; |
|
293 tm_mday = t->tm_mday; |
|
294 tm_mon = t->tm_mon; |
|
295 tm_year = t->tm_year; |
|
296 tm_wday = t->tm_wday; |
|
297 tm_yday = t->tm_yday; |
|
298 tm_isdst = t->tm_isdst; |
|
299 |
3887
|
300 #if defined (HAVE_STRUCT_TM_TM_ZONE) |
3253
|
301 tm_zone = t->tm_zone; |
|
302 #elif defined (HAVE_TZNAME) |
|
303 if (t->tm_isdst == 0 || t->tm_isdst == 1) |
|
304 tm_zone = tzname[t->tm_isdst]; |
|
305 #endif |
|
306 } |
|
307 |
|
308 void |
|
309 octave_localtime::init (const octave_time& ot) |
|
310 { |
|
311 tm_usec = ot.usec (); |
|
312 |
3255
|
313 time_t t = ot; |
3253
|
314 |
|
315 octave_base_tm::init (localtime (&t)); |
|
316 } |
|
317 |
|
318 void |
|
319 octave_gmtime::init (const octave_time& ot) |
|
320 { |
|
321 tm_usec = ot.usec (); |
|
322 |
3255
|
323 time_t t = ot; |
3253
|
324 |
|
325 octave_base_tm::init (gmtime (&t)); |
|
326 } |
|
327 |
3465
|
328 void |
3504
|
329 octave_strptime::init (const std::string& str, const std::string& fmt) |
3465
|
330 { |
|
331 struct tm t; |
|
332 |
|
333 t.tm_sec = 0; |
|
334 t.tm_min = 0; |
|
335 t.tm_hour = 0; |
|
336 t.tm_mday = 0; |
|
337 t.tm_mon = 0; |
|
338 t.tm_year = 0; |
|
339 t.tm_wday = 0; |
|
340 t.tm_yday = 0; |
|
341 t.tm_isdst = 0; |
|
342 |
3887
|
343 #if defined (HAVE_STRUCT_TM_TM_ZONE) |
3465
|
344 char *ps = strsave (""); |
|
345 t.tm_zone = ps; |
|
346 #endif |
|
347 |
|
348 char *p = strsave (str.c_str ()); |
|
349 |
3706
|
350 char *q = oct_strptime (p, fmt.c_str (), &t); |
3465
|
351 |
5675
|
352 if (q) |
|
353 nchars = q - p + 1; |
|
354 else |
|
355 nchars = 0; |
3465
|
356 |
|
357 delete [] p; |
|
358 |
|
359 octave_base_tm::init (&t); |
|
360 |
3887
|
361 #if defined (HAVE_STRUCT_TM_TM_ZONE) |
3465
|
362 delete ps; |
|
363 #endif |
|
364 } |
|
365 |
3253
|
366 /* |
|
367 ;;; Local Variables: *** |
|
368 ;;; mode: C++ *** |
|
369 ;;; End: *** |
|
370 */ |