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