Mercurial > octave-nkf
annotate liboctave/oct-time.cc @ 13974:f5bd61eb032f
handle tm_gmtoff field in struct tm
* oct-time.h, oct-time.cc (octave_time::tm_gmtoff): New data member.
Update all functions that manipulate time fields.
(octave_time::gmtoff): New functions.
(DEFINE_SET_FIELD_FCN): New macro, adapted from
DEFINE_SET_INT_FIELD_FCN.
(DEFINE_SET_INT_FIELD_FCN): Define using DEFINE_SET_FIELD_FCN.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 02 Dec 2011 03:51:38 -0500 |
parents | 12df7854fa7c |
children | 16158606112d |
rev | line source |
---|---|
3253 | 1 /* |
2 | |
11523 | 3 Copyright (C) 1999-2011 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> |
10463
bbe99b2a5ba7
undo recent gnulib-related changes
John W. Eaton <jwe@octave.org>
parents:
10447
diff
changeset
|
28 #include <ctime> |
3253 | 29 |
10240
fa7b5751730c
use gnulib time, sys_time, and sys_times modules
John W. Eaton <jwe@octave.org>
parents:
10239
diff
changeset
|
30 #include <sys/time.h> |
3607 | 31 #include <sys/types.h> |
32 #include <unistd.h> | |
33 | |
10279 | 34 #include "strftime.h" |
9946 | 35 |
3253 | 36 #include "lo-error.h" |
7231 | 37 #include "lo-math.h" |
3253 | 38 #include "lo-utils.h" |
39 #include "oct-time.h" | |
40 | |
41 octave_time::octave_time (const octave_base_tm& tm) | |
11501
331fcc41ca23
data member initialization fixes
John W. Eaton <jwe@octave.org>
parents:
11005
diff
changeset
|
42 : ot_unix_time (), ot_usec () |
3253 | 43 { |
44 struct tm t; | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
45 |
3253 | 46 t.tm_sec = tm.sec (); |
47 t.tm_min = tm.min (); | |
48 t.tm_hour = tm.hour (); | |
49 t.tm_mday = tm.mday (); | |
50 t.tm_mon = tm.mon (); | |
51 t.tm_year = tm.year (); | |
52 t.tm_wday = tm.wday (); | |
53 t.tm_yday = tm.yday (); | |
54 t.tm_isdst = tm.isdst (); | |
13974
f5bd61eb032f
handle tm_gmtoff field in struct tm
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
55 t.tm_gmtoff = tm.gmtoff (); |
3253 | 56 |
3887 | 57 #if defined (HAVE_STRUCT_TM_TM_ZONE) |
3504 | 58 std::string s = tm.zone (); |
3344 | 59 char *ps = strsave (s.c_str ()); |
60 t.tm_zone = ps; | |
3253 | 61 #endif |
62 | |
11005
0de4eff677d6
use mktime module from gnulib
John W. Eaton <jwe@octave.org>
parents:
10463
diff
changeset
|
63 ot_unix_time = gnulib::mktime (&t); |
3253 | 64 |
3887 | 65 #if defined (HAVE_STRUCT_TM_TM_ZONE) |
3344 | 66 delete [] ps; |
67 #endif | |
68 | |
3253 | 69 ot_usec = tm.usec (); |
70 } | |
71 | |
3504 | 72 std::string |
3255 | 73 octave_time::ctime (void) const |
74 { | |
75 return octave_localtime (*this) . asctime (); | |
76 } | |
77 | |
3253 | 78 void |
79 octave_time::stamp (void) | |
80 { | |
81 struct timeval tp; | |
82 | |
10411 | 83 gnulib::gettimeofday (&tp, 0); |
3253 | 84 |
85 ot_unix_time = tp.tv_sec; | |
4085 | 86 ot_usec = tp.tv_usec; |
3253 | 87 } |
88 | |
3736 | 89 // From the mktime() manual page: |
90 // | |
91 // The mktime() function converts a broken-down time structure, | |
92 // expressed as local time, to calendar time representation. | |
93 // | |
94 // <snip> | |
95 // | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10279
diff
changeset
|
96 // If structure members are outside their legal interval, they |
3736 | 97 // will be normalized (so that, e.g., 40 October is changed into |
98 // 9 November). | |
99 // | |
100 // So, we no longer check limits here. | |
101 | |
13974
f5bd61eb032f
handle tm_gmtoff field in struct tm
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
102 #define DEFINE_SET_FIELD_FCN(type, f, lo, hi) \ |
3253 | 103 octave_base_tm& \ |
13974
f5bd61eb032f
handle tm_gmtoff field in struct tm
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
104 octave_base_tm::f (type v) \ |
3736 | 105 { \ |
106 tm_ ## f = v; \ | |
107 \ | |
108 return *this; \ | |
109 } | |
13974
f5bd61eb032f
handle tm_gmtoff field in struct tm
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
110 |
f5bd61eb032f
handle tm_gmtoff field in struct tm
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
111 #define DEFINE_SET_INT_FIELD_FCN(f, lo, hi) \ |
f5bd61eb032f
handle tm_gmtoff field in struct tm
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
112 DEFINE_SET_FIELD_FCN (int, f, lo, hi) |
3253 | 113 |
114 DEFINE_SET_INT_FIELD_FCN (usec, 0, 1000000) | |
115 DEFINE_SET_INT_FIELD_FCN (sec, 0, 61) | |
116 DEFINE_SET_INT_FIELD_FCN (min, 0, 59) | |
117 DEFINE_SET_INT_FIELD_FCN (hour, 0, 23) | |
118 DEFINE_SET_INT_FIELD_FCN (mday, 1, 31) | |
119 DEFINE_SET_INT_FIELD_FCN (mon, 0, 11) | |
120 DEFINE_SET_INT_FIELD_FCN (year, INT_MIN, INT_MAX) | |
121 DEFINE_SET_INT_FIELD_FCN (wday, 0, 6) | |
122 DEFINE_SET_INT_FIELD_FCN (yday, 0, 365) | |
123 DEFINE_SET_INT_FIELD_FCN (isdst, 0, 1) | |
13974
f5bd61eb032f
handle tm_gmtoff field in struct tm
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
124 DEFINE_SET_FIELD_FCN (long, gmtoff, -86400, 0) |
3253 | 125 |
126 octave_base_tm& | |
3504 | 127 octave_base_tm::zone (const std::string& s) |
3253 | 128 { |
129 tm_zone = s; | |
130 return *this; | |
131 } | |
132 | |
133 #if !defined STRFTIME_BUF_INITIAL_SIZE | |
134 #define STRFTIME_BUF_INITIAL_SIZE 128 | |
135 #endif | |
136 | |
3504 | 137 std::string |
138 octave_base_tm::strftime (const std::string& fmt) const | |
3253 | 139 { |
3504 | 140 std::string retval; |
3253 | 141 |
3709 | 142 if (! fmt.empty ()) |
143 { | |
144 struct tm t; | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
145 |
3709 | 146 t.tm_sec = tm_sec; |
147 t.tm_min = tm_min; | |
148 t.tm_hour = tm_hour; | |
149 t.tm_mday = tm_mday; | |
150 t.tm_mon = tm_mon; | |
151 t.tm_year = tm_year; | |
152 t.tm_wday = tm_wday; | |
153 t.tm_yday = tm_yday; | |
154 t.tm_isdst = tm_isdst; | |
3253 | 155 |
13974
f5bd61eb032f
handle tm_gmtoff field in struct tm
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
156 #if defined (HAVE_STRUCT_TM_GMTOFF) |
f5bd61eb032f
handle tm_gmtoff field in struct tm
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
157 t.tm_gmtoff = tm_gmtoff; |
f5bd61eb032f
handle tm_gmtoff field in struct tm
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
158 #endif |
f5bd61eb032f
handle tm_gmtoff field in struct tm
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
159 |
3887 | 160 #if defined (HAVE_STRUCT_TM_TM_ZONE) |
3709 | 161 char *ps = strsave (tm_zone.c_str ()); |
162 t.tm_zone = ps; | |
3253 | 163 #endif |
164 | |
3709 | 165 const char *fmt_str = fmt.c_str (); |
3253 | 166 |
3709 | 167 char *buf = 0; |
168 size_t bufsize = STRFTIME_BUF_INITIAL_SIZE; | |
169 size_t chars_written = 0; | |
3253 | 170 |
3709 | 171 while (chars_written == 0) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10279
diff
changeset
|
172 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10279
diff
changeset
|
173 delete [] buf; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10279
diff
changeset
|
174 buf = new char[bufsize]; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10279
diff
changeset
|
175 buf[0] = '\0'; |
3253 | 176 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10279
diff
changeset
|
177 chars_written = nstrftime (buf, bufsize, fmt_str, &t, 0, 0); |
3253 | 178 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10279
diff
changeset
|
179 bufsize *= 2; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10279
diff
changeset
|
180 } |
3253 | 181 |
3887 | 182 #if defined (HAVE_STRUCT_TM_TM_ZONE) |
3709 | 183 delete [] ps; |
3344 | 184 #endif |
185 | |
3709 | 186 retval = buf; |
3253 | 187 |
3709 | 188 delete [] buf; |
189 } | |
3253 | 190 |
191 return retval; | |
192 } | |
193 | |
194 void | |
195 octave_base_tm::init (void *p) | |
196 { | |
7407 | 197 if (! p) |
198 return; | |
199 | |
3253 | 200 struct tm *t = static_cast<struct tm*> (p); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
201 |
3253 | 202 tm_sec = t->tm_sec; |
203 tm_min = t->tm_min; | |
204 tm_hour = t->tm_hour; | |
205 tm_mday = t->tm_mday; | |
206 tm_mon = t->tm_mon; | |
207 tm_year = t->tm_year; | |
208 tm_wday = t->tm_wday; | |
209 tm_yday = t->tm_yday; | |
210 tm_isdst = t->tm_isdst; | |
211 | |
13974
f5bd61eb032f
handle tm_gmtoff field in struct tm
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
212 #if defined (HAVE_STRUCT_TM_GMTOFF) |
f5bd61eb032f
handle tm_gmtoff field in struct tm
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
213 tm_gmtoff = t->tm_gmtoff; |
f5bd61eb032f
handle tm_gmtoff field in struct tm
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
214 #endif |
f5bd61eb032f
handle tm_gmtoff field in struct tm
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
215 |
3887 | 216 #if defined (HAVE_STRUCT_TM_TM_ZONE) |
7270 | 217 if (t->tm_zone) |
218 tm_zone = t->tm_zone; | |
3253 | 219 #elif defined (HAVE_TZNAME) |
220 if (t->tm_isdst == 0 || t->tm_isdst == 1) | |
221 tm_zone = tzname[t->tm_isdst]; | |
222 #endif | |
223 } | |
224 | |
225 void | |
226 octave_localtime::init (const octave_time& ot) | |
227 { | |
228 tm_usec = ot.usec (); | |
229 | |
7065 | 230 time_t t = ot.unix_time (); |
3253 | 231 |
232 octave_base_tm::init (localtime (&t)); | |
233 } | |
234 | |
235 void | |
236 octave_gmtime::init (const octave_time& ot) | |
237 { | |
238 tm_usec = ot.usec (); | |
239 | |
7065 | 240 time_t t = ot.unix_time (); |
3253 | 241 |
242 octave_base_tm::init (gmtime (&t)); | |
243 } | |
244 | |
3465 | 245 void |
3504 | 246 octave_strptime::init (const std::string& str, const std::string& fmt) |
3465 | 247 { |
248 struct tm t; | |
249 | |
250 t.tm_sec = 0; | |
251 t.tm_min = 0; | |
252 t.tm_hour = 0; | |
253 t.tm_mday = 0; | |
6996 | 254 t.tm_mon = -1; |
255 t.tm_year = INT_MIN; | |
3465 | 256 t.tm_wday = 0; |
257 t.tm_yday = 0; | |
258 t.tm_isdst = 0; | |
259 | |
13974
f5bd61eb032f
handle tm_gmtoff field in struct tm
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
260 #if defined (HAVE_STRUCT_TM_GMTOFF) |
f5bd61eb032f
handle tm_gmtoff field in struct tm
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
261 t.tm_gmtoff = 0; |
f5bd61eb032f
handle tm_gmtoff field in struct tm
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
262 #endif |
f5bd61eb032f
handle tm_gmtoff field in struct tm
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
263 |
3887 | 264 #if defined (HAVE_STRUCT_TM_TM_ZONE) |
3465 | 265 char *ps = strsave (""); |
266 t.tm_zone = ps; | |
267 #endif | |
268 | |
269 char *p = strsave (str.c_str ()); | |
270 | |
10411 | 271 char *q = gnulib::strptime (p, fmt.c_str (), &t); |
3465 | 272 |
6995 | 273 // Fill in wday and yday, but only if mday is valid and the mon and year |
274 // are filled in, avoiding issues with mktime and invalid dates. | |
6996 | 275 if (t.tm_mday != 0 && t.tm_mon >= 0 && t.tm_year != INT_MIN) |
6995 | 276 { |
277 t.tm_isdst = -1; | |
11005
0de4eff677d6
use mktime module from gnulib
John W. Eaton <jwe@octave.org>
parents:
10463
diff
changeset
|
278 gnulib::mktime (&t); |
6995 | 279 } |
6941 | 280 |
6996 | 281 if (t.tm_mon < 0) |
282 t.tm_mon = 0; | |
283 | |
284 if (t.tm_year == INT_MIN) | |
285 t.tm_year = 0; | |
286 | |
5675 | 287 if (q) |
288 nchars = q - p + 1; | |
289 else | |
290 nchars = 0; | |
3465 | 291 |
292 delete [] p; | |
293 | |
294 octave_base_tm::init (&t); | |
295 | |
3887 | 296 #if defined (HAVE_STRUCT_TM_TM_ZONE) |
7058 | 297 delete [] ps; |
3465 | 298 #endif |
299 } |