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