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