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