3253
|
1 /* |
|
2 |
|
3 Copyright (C) 1999 John W. Eaton |
|
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 |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
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 |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
3290
|
27 #include <climits> |
3253
|
28 #include <cmath> |
|
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 |
3253
|
37 #include "lo-error.h" |
|
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 |
|
55 #if defined (HAVE_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 |
|
61 ot_unix_time = mktime (&t); |
|
62 |
3344
|
63 #if defined (HAVE_TM_ZONE) |
|
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 #if defined (HAVE_GETTIMEOFDAY) |
|
80 |
|
81 struct timeval tp; |
|
82 |
|
83 #if defined (GETTIMEOFDAY_NO_TZ) |
|
84 gettimeofday (&tp); |
|
85 #else |
|
86 gettimeofday (&tp, 0); |
|
87 #endif |
|
88 |
|
89 ot_unix_time = tp.tv_sec; |
|
90 |
|
91 ot_usec = tp.tv_usec; |
|
92 |
|
93 #else |
|
94 |
|
95 ot_unix_time = time (0); |
|
96 |
|
97 #endif |
|
98 } |
|
99 |
3736
|
100 // From the mktime() manual page: |
|
101 // |
|
102 // The mktime() function converts a broken-down time structure, |
|
103 // expressed as local time, to calendar time representation. |
|
104 // |
|
105 // <snip> |
|
106 // |
|
107 // If structure members are outside their legal interval, they |
|
108 // will be normalized (so that, e.g., 40 October is changed into |
|
109 // 9 November). |
|
110 // |
|
111 // So, we no longer check limits here. |
|
112 |
|
113 #if 0 |
3253
|
114 #define DEFINE_SET_INT_FIELD_FCN(f, lo, hi) \ |
|
115 octave_base_tm& \ |
|
116 octave_base_tm::f (int v) \ |
|
117 { \ |
|
118 if (v < lo || v > hi) \ |
|
119 (*current_liboctave_error_handler) \ |
|
120 ("invalid value specified for " #f); \ |
|
121 \ |
|
122 tm_ ## f = v; \ |
|
123 \ |
|
124 return *this; \ |
|
125 } |
3736
|
126 #else |
|
127 #define DEFINE_SET_INT_FIELD_FCN(f, lo, hi) \ |
|
128 octave_base_tm& \ |
|
129 octave_base_tm::f (int v) \ |
|
130 { \ |
|
131 tm_ ## f = v; \ |
|
132 \ |
|
133 return *this; \ |
|
134 } |
|
135 #endif |
3253
|
136 |
|
137 DEFINE_SET_INT_FIELD_FCN (usec, 0, 1000000) |
|
138 DEFINE_SET_INT_FIELD_FCN (sec, 0, 61) |
|
139 DEFINE_SET_INT_FIELD_FCN (min, 0, 59) |
|
140 DEFINE_SET_INT_FIELD_FCN (hour, 0, 23) |
|
141 DEFINE_SET_INT_FIELD_FCN (mday, 1, 31) |
|
142 DEFINE_SET_INT_FIELD_FCN (mon, 0, 11) |
|
143 DEFINE_SET_INT_FIELD_FCN (year, INT_MIN, INT_MAX) |
|
144 DEFINE_SET_INT_FIELD_FCN (wday, 0, 6) |
|
145 DEFINE_SET_INT_FIELD_FCN (yday, 0, 365) |
|
146 DEFINE_SET_INT_FIELD_FCN (isdst, 0, 1) |
|
147 |
|
148 octave_base_tm& |
3504
|
149 octave_base_tm::zone (const std::string& s) |
3253
|
150 { |
|
151 tm_zone = s; |
|
152 return *this; |
|
153 } |
|
154 |
|
155 #if !defined STRFTIME_BUF_INITIAL_SIZE |
|
156 #define STRFTIME_BUF_INITIAL_SIZE 128 |
|
157 #endif |
|
158 |
3504
|
159 std::string |
|
160 octave_base_tm::strftime (const std::string& fmt) const |
3253
|
161 { |
3504
|
162 std::string retval; |
3253
|
163 |
3709
|
164 if (! fmt.empty ()) |
|
165 { |
|
166 struct tm t; |
3253
|
167 |
3709
|
168 t.tm_sec = tm_sec; |
|
169 t.tm_min = tm_min; |
|
170 t.tm_hour = tm_hour; |
|
171 t.tm_mday = tm_mday; |
|
172 t.tm_mon = tm_mon; |
|
173 t.tm_year = tm_year; |
|
174 t.tm_wday = tm_wday; |
|
175 t.tm_yday = tm_yday; |
|
176 t.tm_isdst = tm_isdst; |
3253
|
177 |
|
178 #if defined (HAVE_TM_ZONE) |
3709
|
179 char *ps = strsave (tm_zone.c_str ()); |
|
180 t.tm_zone = ps; |
3253
|
181 #endif |
|
182 |
3709
|
183 const char *fmt_str = fmt.c_str (); |
3253
|
184 |
3709
|
185 char *buf = 0; |
|
186 size_t bufsize = STRFTIME_BUF_INITIAL_SIZE; |
|
187 size_t chars_written = 0; |
3253
|
188 |
3709
|
189 while (chars_written == 0) |
|
190 { |
|
191 delete [] buf; |
|
192 buf = new char[bufsize]; |
|
193 buf[0] = '\0'; |
3253
|
194 |
3709
|
195 chars_written = ::strftime (buf, bufsize, fmt_str, &t); |
3253
|
196 |
3709
|
197 bufsize *= 2; |
|
198 } |
3253
|
199 |
3344
|
200 #if defined (HAVE_TM_ZONE) |
3709
|
201 delete [] ps; |
3344
|
202 #endif |
|
203 |
3709
|
204 retval = buf; |
3253
|
205 |
3709
|
206 delete [] buf; |
|
207 } |
3253
|
208 |
|
209 return retval; |
|
210 } |
|
211 |
|
212 void |
|
213 octave_base_tm::init (void *p) |
|
214 { |
|
215 struct tm *t = static_cast<struct tm*> (p); |
|
216 |
|
217 tm_sec = t->tm_sec; |
|
218 tm_min = t->tm_min; |
|
219 tm_hour = t->tm_hour; |
|
220 tm_mday = t->tm_mday; |
|
221 tm_mon = t->tm_mon; |
|
222 tm_year = t->tm_year; |
|
223 tm_wday = t->tm_wday; |
|
224 tm_yday = t->tm_yday; |
|
225 tm_isdst = t->tm_isdst; |
|
226 |
|
227 #if defined (HAVE_TM_ZONE) |
|
228 tm_zone = t->tm_zone; |
|
229 #elif defined (HAVE_TZNAME) |
|
230 if (t->tm_isdst == 0 || t->tm_isdst == 1) |
|
231 tm_zone = tzname[t->tm_isdst]; |
|
232 #endif |
|
233 } |
|
234 |
|
235 void |
|
236 octave_localtime::init (const octave_time& ot) |
|
237 { |
|
238 tm_usec = ot.usec (); |
|
239 |
3255
|
240 time_t t = ot; |
3253
|
241 |
|
242 octave_base_tm::init (localtime (&t)); |
|
243 } |
|
244 |
|
245 void |
|
246 octave_gmtime::init (const octave_time& ot) |
|
247 { |
|
248 tm_usec = ot.usec (); |
|
249 |
3255
|
250 time_t t = ot; |
3253
|
251 |
|
252 octave_base_tm::init (gmtime (&t)); |
|
253 } |
|
254 |
3465
|
255 void |
3504
|
256 octave_strptime::init (const std::string& str, const std::string& fmt) |
3465
|
257 { |
|
258 struct tm t; |
|
259 |
|
260 t.tm_sec = 0; |
|
261 t.tm_min = 0; |
|
262 t.tm_hour = 0; |
|
263 t.tm_mday = 0; |
|
264 t.tm_mon = 0; |
|
265 t.tm_year = 0; |
|
266 t.tm_wday = 0; |
|
267 t.tm_yday = 0; |
|
268 t.tm_isdst = 0; |
|
269 |
|
270 #if defined (HAVE_TM_ZONE) |
|
271 char *ps = strsave (""); |
|
272 t.tm_zone = ps; |
|
273 #endif |
|
274 |
|
275 char *p = strsave (str.c_str ()); |
|
276 |
3706
|
277 char *q = oct_strptime (p, fmt.c_str (), &t); |
3465
|
278 |
|
279 nchars = p - q; |
|
280 |
|
281 delete [] p; |
|
282 |
|
283 octave_base_tm::init (&t); |
|
284 |
|
285 #if defined (HAVE_TM_ZONE) |
|
286 delete ps; |
|
287 #endif |
|
288 } |
|
289 |
3253
|
290 /* |
|
291 ;;; Local Variables: *** |
|
292 ;;; mode: C++ *** |
|
293 ;;; End: *** |
|
294 */ |