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 |
3704
|
27 #if 0 |
|
28 |
3703
|
29 // The following definitions are commented out because they cause |
|
30 // trouble on some systems. What is really needed is a feature test |
|
31 // in the configure script. |
|
32 |
3597
|
33 // I am told that without _BSD_SOURCE, tm_zone won't be declared on |
|
34 // some systems. Defining _XOPEN_SOURCE provides the declaration for |
|
35 // strptime on some others. |
|
36 // |
|
37 // These defines go here, before any system header files are included, |
|
38 // because the system header files may define other macros that are |
|
39 // actually used to determine the feature set. If we wait until after |
|
40 // some system header file is included, it may be too late. |
|
41 |
|
42 #if !defined (_BSD_SOURCE) |
|
43 #define _BSD_SOURCE 1 |
|
44 #define OCTAVE_UNDEFINE_BSD_SOURCE |
|
45 #endif |
|
46 |
|
47 #if !defined (_XOPEN_SOURCE) |
|
48 #define _XOPEN_SOURCE 1 |
|
49 #define OCTAVE_UNDEFINE_XOPEN_SOURCE |
|
50 #endif |
|
51 |
3703
|
52 #endif |
|
53 |
3290
|
54 #include <climits> |
3253
|
55 #include <cmath> |
|
56 |
3607
|
57 #ifdef HAVE_UNISTD_H |
|
58 #ifdef HAVE_SYS_TYPES_H |
|
59 #include <sys/types.h> |
|
60 #endif |
|
61 #include <unistd.h> |
|
62 #endif |
|
63 |
3253
|
64 #include "lo-error.h" |
|
65 #include "lo-utils.h" |
|
66 #include "oct-time.h" |
|
67 |
3597
|
68 #if defined (OCTAVE_UNDEFINE_BSD_SOURCE) |
|
69 #undef _BSD_SOURCE |
|
70 #endif |
|
71 |
|
72 #if defined (OCTAVE_UNDEFINE_XOPEN_SOURCE) |
|
73 #undef _XOPEN_SOURCE |
|
74 #endif |
|
75 |
3496
|
76 #if !defined (HAVE_STRPTIME) |
3498
|
77 extern "C" char *strptime (const char *buf, const char *format, struct tm *tm); |
3496
|
78 #endif |
|
79 |
3253
|
80 octave_time::octave_time (const octave_base_tm& tm) |
|
81 { |
|
82 struct tm t; |
|
83 |
|
84 t.tm_sec = tm.sec (); |
|
85 t.tm_min = tm.min (); |
|
86 t.tm_hour = tm.hour (); |
|
87 t.tm_mday = tm.mday (); |
|
88 t.tm_mon = tm.mon (); |
|
89 t.tm_year = tm.year (); |
|
90 t.tm_wday = tm.wday (); |
|
91 t.tm_yday = tm.yday (); |
|
92 t.tm_isdst = tm.isdst (); |
|
93 |
|
94 #if defined (HAVE_TM_ZONE) |
3504
|
95 std::string s = tm.zone (); |
3344
|
96 char *ps = strsave (s.c_str ()); |
|
97 t.tm_zone = ps; |
3253
|
98 #endif |
|
99 |
|
100 ot_unix_time = mktime (&t); |
|
101 |
3344
|
102 #if defined (HAVE_TM_ZONE) |
|
103 delete [] ps; |
|
104 #endif |
|
105 |
3253
|
106 ot_usec = tm.usec (); |
|
107 } |
|
108 |
3504
|
109 std::string |
3255
|
110 octave_time::ctime (void) const |
|
111 { |
|
112 return octave_localtime (*this) . asctime (); |
|
113 } |
|
114 |
3253
|
115 void |
|
116 octave_time::stamp (void) |
|
117 { |
|
118 #if defined (HAVE_GETTIMEOFDAY) |
|
119 |
|
120 struct timeval tp; |
|
121 |
|
122 #if defined (GETTIMEOFDAY_NO_TZ) |
|
123 gettimeofday (&tp); |
|
124 #else |
|
125 gettimeofday (&tp, 0); |
|
126 #endif |
|
127 |
|
128 ot_unix_time = tp.tv_sec; |
|
129 |
|
130 ot_usec = tp.tv_usec; |
|
131 |
|
132 #else |
|
133 |
|
134 ot_unix_time = time (0); |
|
135 |
|
136 #endif |
|
137 } |
|
138 |
|
139 #define DEFINE_SET_INT_FIELD_FCN(f, lo, hi) \ |
|
140 octave_base_tm& \ |
|
141 octave_base_tm::f (int v) \ |
|
142 { \ |
|
143 if (v < lo || v > hi) \ |
|
144 (*current_liboctave_error_handler) \ |
|
145 ("invalid value specified for " #f); \ |
|
146 \ |
|
147 tm_ ## f = v; \ |
|
148 \ |
|
149 return *this; \ |
|
150 } |
|
151 |
|
152 DEFINE_SET_INT_FIELD_FCN (usec, 0, 1000000) |
|
153 DEFINE_SET_INT_FIELD_FCN (sec, 0, 61) |
|
154 DEFINE_SET_INT_FIELD_FCN (min, 0, 59) |
|
155 DEFINE_SET_INT_FIELD_FCN (hour, 0, 23) |
|
156 DEFINE_SET_INT_FIELD_FCN (mday, 1, 31) |
|
157 DEFINE_SET_INT_FIELD_FCN (mon, 0, 11) |
|
158 DEFINE_SET_INT_FIELD_FCN (year, INT_MIN, INT_MAX) |
|
159 DEFINE_SET_INT_FIELD_FCN (wday, 0, 6) |
|
160 DEFINE_SET_INT_FIELD_FCN (yday, 0, 365) |
|
161 DEFINE_SET_INT_FIELD_FCN (isdst, 0, 1) |
|
162 |
|
163 octave_base_tm& |
3504
|
164 octave_base_tm::zone (const std::string& s) |
3253
|
165 { |
|
166 tm_zone = s; |
|
167 return *this; |
|
168 } |
|
169 |
|
170 #if !defined STRFTIME_BUF_INITIAL_SIZE |
|
171 #define STRFTIME_BUF_INITIAL_SIZE 128 |
|
172 #endif |
|
173 |
3504
|
174 std::string |
|
175 octave_base_tm::strftime (const std::string& fmt) const |
3253
|
176 { |
3504
|
177 std::string retval; |
3253
|
178 |
|
179 struct tm t; |
|
180 |
|
181 t.tm_sec = tm_sec; |
|
182 t.tm_min = tm_min; |
|
183 t.tm_hour = tm_hour; |
|
184 t.tm_mday = tm_mday; |
|
185 t.tm_mon = tm_mon; |
|
186 t.tm_year = tm_year; |
|
187 t.tm_wday = tm_wday; |
|
188 t.tm_yday = tm_yday; |
|
189 t.tm_isdst = tm_isdst; |
|
190 |
|
191 #if defined (HAVE_TM_ZONE) |
3344
|
192 char *ps = strsave (tm_zone.c_str ()); |
|
193 t.tm_zone = ps; |
3253
|
194 #endif |
|
195 |
|
196 const char *fmt_str = fmt.c_str (); |
|
197 |
|
198 char *buf = 0; |
|
199 size_t bufsize = STRFTIME_BUF_INITIAL_SIZE; |
|
200 size_t chars_written = 0; |
|
201 |
|
202 while (chars_written == 0) |
|
203 { |
|
204 delete [] buf; |
|
205 buf = new char[bufsize]; |
|
206 buf[0] = '\0'; |
|
207 |
3255
|
208 chars_written = ::strftime (buf, bufsize, fmt_str, &t); |
3253
|
209 |
|
210 bufsize *= 2; |
|
211 } |
|
212 |
3344
|
213 #if defined (HAVE_TM_ZONE) |
|
214 delete [] ps; |
|
215 #endif |
|
216 |
3253
|
217 retval = buf; |
|
218 |
|
219 delete [] buf; |
|
220 |
|
221 return retval; |
|
222 } |
|
223 |
|
224 void |
|
225 octave_base_tm::init (void *p) |
|
226 { |
|
227 struct tm *t = static_cast<struct tm*> (p); |
|
228 |
|
229 tm_sec = t->tm_sec; |
|
230 tm_min = t->tm_min; |
|
231 tm_hour = t->tm_hour; |
|
232 tm_mday = t->tm_mday; |
|
233 tm_mon = t->tm_mon; |
|
234 tm_year = t->tm_year; |
|
235 tm_wday = t->tm_wday; |
|
236 tm_yday = t->tm_yday; |
|
237 tm_isdst = t->tm_isdst; |
|
238 |
|
239 #if defined (HAVE_TM_ZONE) |
|
240 tm_zone = t->tm_zone; |
|
241 #elif defined (HAVE_TZNAME) |
|
242 if (t->tm_isdst == 0 || t->tm_isdst == 1) |
|
243 tm_zone = tzname[t->tm_isdst]; |
|
244 #endif |
|
245 } |
|
246 |
|
247 void |
|
248 octave_localtime::init (const octave_time& ot) |
|
249 { |
|
250 tm_usec = ot.usec (); |
|
251 |
3255
|
252 time_t t = ot; |
3253
|
253 |
|
254 octave_base_tm::init (localtime (&t)); |
|
255 } |
|
256 |
|
257 void |
|
258 octave_gmtime::init (const octave_time& ot) |
|
259 { |
|
260 tm_usec = ot.usec (); |
|
261 |
3255
|
262 time_t t = ot; |
3253
|
263 |
|
264 octave_base_tm::init (gmtime (&t)); |
|
265 } |
|
266 |
3465
|
267 void |
3504
|
268 octave_strptime::init (const std::string& str, const std::string& fmt) |
3465
|
269 { |
|
270 struct tm t; |
|
271 |
|
272 t.tm_sec = 0; |
|
273 t.tm_min = 0; |
|
274 t.tm_hour = 0; |
|
275 t.tm_mday = 0; |
|
276 t.tm_mon = 0; |
|
277 t.tm_year = 0; |
|
278 t.tm_wday = 0; |
|
279 t.tm_yday = 0; |
|
280 t.tm_isdst = 0; |
|
281 |
|
282 #if defined (HAVE_TM_ZONE) |
|
283 char *ps = strsave (""); |
|
284 t.tm_zone = ps; |
|
285 #endif |
|
286 |
|
287 char *p = strsave (str.c_str ()); |
|
288 |
|
289 char *q = strptime (p, fmt.c_str (), &t); |
|
290 |
|
291 nchars = p - q; |
|
292 |
|
293 delete [] p; |
|
294 |
|
295 octave_base_tm::init (&t); |
|
296 |
|
297 #if defined (HAVE_TM_ZONE) |
|
298 delete ps; |
|
299 #endif |
|
300 } |
|
301 |
3253
|
302 /* |
|
303 ;;; Local Variables: *** |
|
304 ;;; mode: C++ *** |
|
305 ;;; End: *** |
|
306 */ |