Mercurial > hg > octave-lyh
annotate liboctave/oct-time.cc @ 10123:8590f3c51868
Fix a few MSVC-related problems and partially re-enable MSVC compilation.
author | Michael Goffioul <michael.goffioul@gmail.com> |
---|---|
date | Sun, 17 Jan 2010 20:32:38 +0000 |
parents | 0b0bf1fd1ed7 |
children | 4c0cdbe0acca |
rev | line source |
---|---|
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) |
10123
8590f3c51868
Fix a few MSVC-related problems and partially re-enable MSVC compilation.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
10072
diff
changeset
|
38 #define WIN32_LEAN_AND_MEAN |
4085 | 39 #include <windows.h> |
6113 | 40 #undef min |
41 #undef max | |
4085 | 42 #endif |
43 | |
10123
8590f3c51868
Fix a few MSVC-related problems and partially re-enable MSVC compilation.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
10072
diff
changeset
|
44 #include <sys/time.h> |
9946 | 45 #include "strftime.h" |
46 | |
3253 | 47 #include "lo-error.h" |
7231 | 48 #include "lo-math.h" |
3253 | 49 #include "lo-utils.h" |
50 #include "oct-time.h" | |
51 | |
52 octave_time::octave_time (const octave_base_tm& tm) | |
53 { | |
54 struct tm t; | |
55 | |
56 t.tm_sec = tm.sec (); | |
57 t.tm_min = tm.min (); | |
58 t.tm_hour = tm.hour (); | |
59 t.tm_mday = tm.mday (); | |
60 t.tm_mon = tm.mon (); | |
61 t.tm_year = tm.year (); | |
62 t.tm_wday = tm.wday (); | |
63 t.tm_yday = tm.yday (); | |
64 t.tm_isdst = tm.isdst (); | |
65 | |
3887 | 66 #if defined (HAVE_STRUCT_TM_TM_ZONE) |
3504 | 67 std::string s = tm.zone (); |
3344 | 68 char *ps = strsave (s.c_str ()); |
69 t.tm_zone = ps; | |
3253 | 70 #endif |
71 | |
72 ot_unix_time = mktime (&t); | |
73 | |
3887 | 74 #if defined (HAVE_STRUCT_TM_TM_ZONE) |
3344 | 75 delete [] ps; |
76 #endif | |
77 | |
3253 | 78 ot_usec = tm.usec (); |
79 } | |
80 | |
3504 | 81 std::string |
3255 | 82 octave_time::ctime (void) const |
83 { | |
84 return octave_localtime (*this) . asctime (); | |
85 } | |
86 | |
3253 | 87 void |
88 octave_time::stamp (void) | |
89 { | |
90 struct timeval tp; | |
91 | |
92 gettimeofday (&tp, 0); | |
93 | |
94 ot_unix_time = tp.tv_sec; | |
4085 | 95 ot_usec = tp.tv_usec; |
3253 | 96 } |
97 | |
3736 | 98 // From the mktime() manual page: |
99 // | |
100 // The mktime() function converts a broken-down time structure, | |
101 // expressed as local time, to calendar time representation. | |
102 // | |
103 // <snip> | |
104 // | |
105 // If structure members are outside their legal interval, they | |
106 // will be normalized (so that, e.g., 40 October is changed into | |
107 // 9 November). | |
108 // | |
109 // So, we no longer check limits here. | |
110 | |
111 #if 0 | |
3253 | 112 #define DEFINE_SET_INT_FIELD_FCN(f, lo, hi) \ |
113 octave_base_tm& \ | |
114 octave_base_tm::f (int v) \ | |
115 { \ | |
116 if (v < lo || v > hi) \ | |
117 (*current_liboctave_error_handler) \ | |
118 ("invalid value specified for " #f); \ | |
119 \ | |
120 tm_ ## f = v; \ | |
121 \ | |
122 return *this; \ | |
123 } | |
3736 | 124 #else |
125 #define DEFINE_SET_INT_FIELD_FCN(f, lo, hi) \ | |
126 octave_base_tm& \ | |
127 octave_base_tm::f (int v) \ | |
128 { \ | |
129 tm_ ## f = v; \ | |
130 \ | |
131 return *this; \ | |
132 } | |
133 #endif | |
3253 | 134 |
135 DEFINE_SET_INT_FIELD_FCN (usec, 0, 1000000) | |
136 DEFINE_SET_INT_FIELD_FCN (sec, 0, 61) | |
137 DEFINE_SET_INT_FIELD_FCN (min, 0, 59) | |
138 DEFINE_SET_INT_FIELD_FCN (hour, 0, 23) | |
139 DEFINE_SET_INT_FIELD_FCN (mday, 1, 31) | |
140 DEFINE_SET_INT_FIELD_FCN (mon, 0, 11) | |
141 DEFINE_SET_INT_FIELD_FCN (year, INT_MIN, INT_MAX) | |
142 DEFINE_SET_INT_FIELD_FCN (wday, 0, 6) | |
143 DEFINE_SET_INT_FIELD_FCN (yday, 0, 365) | |
144 DEFINE_SET_INT_FIELD_FCN (isdst, 0, 1) | |
145 | |
146 octave_base_tm& | |
3504 | 147 octave_base_tm::zone (const std::string& s) |
3253 | 148 { |
149 tm_zone = s; | |
150 return *this; | |
151 } | |
152 | |
153 #if !defined STRFTIME_BUF_INITIAL_SIZE | |
154 #define STRFTIME_BUF_INITIAL_SIZE 128 | |
155 #endif | |
156 | |
3504 | 157 std::string |
158 octave_base_tm::strftime (const std::string& fmt) const | |
3253 | 159 { |
3504 | 160 std::string retval; |
3253 | 161 |
3709 | 162 if (! fmt.empty ()) |
163 { | |
164 struct tm t; | |
3253 | 165 |
3709 | 166 t.tm_sec = tm_sec; |
167 t.tm_min = tm_min; | |
168 t.tm_hour = tm_hour; | |
169 t.tm_mday = tm_mday; | |
170 t.tm_mon = tm_mon; | |
171 t.tm_year = tm_year; | |
172 t.tm_wday = tm_wday; | |
173 t.tm_yday = tm_yday; | |
174 t.tm_isdst = tm_isdst; | |
3253 | 175 |
3887 | 176 #if defined (HAVE_STRUCT_TM_TM_ZONE) |
3709 | 177 char *ps = strsave (tm_zone.c_str ()); |
178 t.tm_zone = ps; | |
3253 | 179 #endif |
180 | |
3709 | 181 const char *fmt_str = fmt.c_str (); |
3253 | 182 |
3709 | 183 char *buf = 0; |
184 size_t bufsize = STRFTIME_BUF_INITIAL_SIZE; | |
185 size_t chars_written = 0; | |
3253 | 186 |
3709 | 187 while (chars_written == 0) |
188 { | |
189 delete [] buf; | |
190 buf = new char[bufsize]; | |
191 buf[0] = '\0'; | |
3253 | 192 |
9947
31436dcf7d0f
call nstrftime, not my_strftime
John W. Eaton <jwe@octave.org>
parents:
9946
diff
changeset
|
193 chars_written = nstrftime (buf, bufsize, fmt_str, &t, 0, 0); |
3253 | 194 |
3709 | 195 bufsize *= 2; |
196 } | |
3253 | 197 |
3887 | 198 #if defined (HAVE_STRUCT_TM_TM_ZONE) |
3709 | 199 delete [] ps; |
3344 | 200 #endif |
201 | |
3709 | 202 retval = buf; |
3253 | 203 |
3709 | 204 delete [] buf; |
205 } | |
3253 | 206 |
207 return retval; | |
208 } | |
209 | |
210 void | |
211 octave_base_tm::init (void *p) | |
212 { | |
7407 | 213 if (! p) |
214 return; | |
215 | |
3253 | 216 struct tm *t = static_cast<struct tm*> (p); |
217 | |
218 tm_sec = t->tm_sec; | |
219 tm_min = t->tm_min; | |
220 tm_hour = t->tm_hour; | |
221 tm_mday = t->tm_mday; | |
222 tm_mon = t->tm_mon; | |
223 tm_year = t->tm_year; | |
224 tm_wday = t->tm_wday; | |
225 tm_yday = t->tm_yday; | |
226 tm_isdst = t->tm_isdst; | |
227 | |
3887 | 228 #if defined (HAVE_STRUCT_TM_TM_ZONE) |
7270 | 229 if (t->tm_zone) |
230 tm_zone = t->tm_zone; | |
3253 | 231 #elif defined (HAVE_TZNAME) |
232 if (t->tm_isdst == 0 || t->tm_isdst == 1) | |
233 tm_zone = tzname[t->tm_isdst]; | |
234 #endif | |
235 } | |
236 | |
237 void | |
238 octave_localtime::init (const octave_time& ot) | |
239 { | |
240 tm_usec = ot.usec (); | |
241 | |
7065 | 242 time_t t = ot.unix_time (); |
3253 | 243 |
244 octave_base_tm::init (localtime (&t)); | |
245 } | |
246 | |
247 void | |
248 octave_gmtime::init (const octave_time& ot) | |
249 { | |
250 tm_usec = ot.usec (); | |
251 | |
7065 | 252 time_t t = ot.unix_time (); |
3253 | 253 |
254 octave_base_tm::init (gmtime (&t)); | |
255 } | |
256 | |
3465 | 257 void |
3504 | 258 octave_strptime::init (const std::string& str, const std::string& fmt) |
3465 | 259 { |
260 struct tm t; | |
261 | |
262 t.tm_sec = 0; | |
263 t.tm_min = 0; | |
264 t.tm_hour = 0; | |
265 t.tm_mday = 0; | |
6996 | 266 t.tm_mon = -1; |
267 t.tm_year = INT_MIN; | |
3465 | 268 t.tm_wday = 0; |
269 t.tm_yday = 0; | |
270 t.tm_isdst = 0; | |
271 | |
3887 | 272 #if defined (HAVE_STRUCT_TM_TM_ZONE) |
3465 | 273 char *ps = strsave (""); |
274 t.tm_zone = ps; | |
275 #endif | |
276 | |
277 char *p = strsave (str.c_str ()); | |
278 | |
3706 | 279 char *q = oct_strptime (p, fmt.c_str (), &t); |
3465 | 280 |
6995 | 281 // Fill in wday and yday, but only if mday is valid and the mon and year |
282 // are filled in, avoiding issues with mktime and invalid dates. | |
6996 | 283 if (t.tm_mday != 0 && t.tm_mon >= 0 && t.tm_year != INT_MIN) |
6995 | 284 { |
285 t.tm_isdst = -1; | |
286 mktime (&t); | |
287 } | |
6941 | 288 |
6996 | 289 if (t.tm_mon < 0) |
290 t.tm_mon = 0; | |
291 | |
292 if (t.tm_year == INT_MIN) | |
293 t.tm_year = 0; | |
294 | |
5675 | 295 if (q) |
296 nchars = q - p + 1; | |
297 else | |
298 nchars = 0; | |
3465 | 299 |
300 delete [] p; | |
301 | |
302 octave_base_tm::init (&t); | |
303 | |
3887 | 304 #if defined (HAVE_STRUCT_TM_TM_ZONE) |
7058 | 305 delete [] ps; |
3465 | 306 #endif |
307 } | |
308 | |
3253 | 309 /* |
310 ;;; Local Variables: *** | |
311 ;;; mode: C++ *** | |
312 ;;; End: *** | |
313 */ |