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