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