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 |
|
30 #include "lo-error.h" |
|
31 #include "lo-utils.h" |
3465
|
32 #define __USE_XOPEN |
3253
|
33 #include "oct-time.h" |
|
34 |
3496
|
35 #if !defined (HAVE_STRPTIME) |
3498
|
36 extern "C" char *strptime (const char *buf, const char *format, struct tm *tm); |
3496
|
37 #endif |
|
38 |
3253
|
39 octave_time::octave_time (const octave_base_tm& tm) |
|
40 { |
|
41 struct tm t; |
|
42 |
|
43 t.tm_sec = tm.sec (); |
|
44 t.tm_min = tm.min (); |
|
45 t.tm_hour = tm.hour (); |
|
46 t.tm_mday = tm.mday (); |
|
47 t.tm_mon = tm.mon (); |
|
48 t.tm_year = tm.year (); |
|
49 t.tm_wday = tm.wday (); |
|
50 t.tm_yday = tm.yday (); |
|
51 t.tm_isdst = tm.isdst (); |
|
52 |
|
53 #if defined (HAVE_TM_ZONE) |
|
54 string s = tm.zone (); |
3344
|
55 char *ps = strsave (s.c_str ()); |
|
56 t.tm_zone = ps; |
3253
|
57 #endif |
|
58 |
|
59 ot_unix_time = mktime (&t); |
|
60 |
3344
|
61 #if defined (HAVE_TM_ZONE) |
|
62 delete [] ps; |
|
63 #endif |
|
64 |
3253
|
65 ot_usec = tm.usec (); |
|
66 } |
|
67 |
3255
|
68 string |
|
69 octave_time::ctime (void) const |
|
70 { |
|
71 return octave_localtime (*this) . asctime (); |
|
72 } |
|
73 |
3253
|
74 void |
|
75 octave_time::stamp (void) |
|
76 { |
|
77 #if defined (HAVE_GETTIMEOFDAY) |
|
78 |
|
79 struct timeval tp; |
|
80 |
|
81 #if defined (GETTIMEOFDAY_NO_TZ) |
|
82 gettimeofday (&tp); |
|
83 #else |
|
84 gettimeofday (&tp, 0); |
|
85 #endif |
|
86 |
|
87 ot_unix_time = tp.tv_sec; |
|
88 |
|
89 ot_usec = tp.tv_usec; |
|
90 |
|
91 #else |
|
92 |
|
93 ot_unix_time = time (0); |
|
94 |
|
95 #endif |
|
96 } |
|
97 |
|
98 #define DEFINE_SET_INT_FIELD_FCN(f, lo, hi) \ |
|
99 octave_base_tm& \ |
|
100 octave_base_tm::f (int v) \ |
|
101 { \ |
|
102 if (v < lo || v > hi) \ |
|
103 (*current_liboctave_error_handler) \ |
|
104 ("invalid value specified for " #f); \ |
|
105 \ |
|
106 tm_ ## f = v; \ |
|
107 \ |
|
108 return *this; \ |
|
109 } |
|
110 |
|
111 DEFINE_SET_INT_FIELD_FCN (usec, 0, 1000000) |
|
112 DEFINE_SET_INT_FIELD_FCN (sec, 0, 61) |
|
113 DEFINE_SET_INT_FIELD_FCN (min, 0, 59) |
|
114 DEFINE_SET_INT_FIELD_FCN (hour, 0, 23) |
|
115 DEFINE_SET_INT_FIELD_FCN (mday, 1, 31) |
|
116 DEFINE_SET_INT_FIELD_FCN (mon, 0, 11) |
|
117 DEFINE_SET_INT_FIELD_FCN (year, INT_MIN, INT_MAX) |
|
118 DEFINE_SET_INT_FIELD_FCN (wday, 0, 6) |
|
119 DEFINE_SET_INT_FIELD_FCN (yday, 0, 365) |
|
120 DEFINE_SET_INT_FIELD_FCN (isdst, 0, 1) |
|
121 |
|
122 octave_base_tm& |
|
123 octave_base_tm::zone (const string& s) |
|
124 { |
|
125 tm_zone = s; |
|
126 return *this; |
|
127 } |
|
128 |
|
129 #if !defined STRFTIME_BUF_INITIAL_SIZE |
|
130 #define STRFTIME_BUF_INITIAL_SIZE 128 |
|
131 #endif |
|
132 |
|
133 string |
3255
|
134 octave_base_tm::strftime (const string& fmt) const |
3253
|
135 { |
|
136 string retval; |
|
137 |
|
138 struct tm t; |
|
139 |
|
140 t.tm_sec = tm_sec; |
|
141 t.tm_min = tm_min; |
|
142 t.tm_hour = tm_hour; |
|
143 t.tm_mday = tm_mday; |
|
144 t.tm_mon = tm_mon; |
|
145 t.tm_year = tm_year; |
|
146 t.tm_wday = tm_wday; |
|
147 t.tm_yday = tm_yday; |
|
148 t.tm_isdst = tm_isdst; |
|
149 |
|
150 #if defined (HAVE_TM_ZONE) |
3344
|
151 char *ps = strsave (tm_zone.c_str ()); |
|
152 t.tm_zone = ps; |
3253
|
153 #endif |
|
154 |
|
155 const char *fmt_str = fmt.c_str (); |
|
156 |
|
157 char *buf = 0; |
|
158 size_t bufsize = STRFTIME_BUF_INITIAL_SIZE; |
|
159 size_t chars_written = 0; |
|
160 |
|
161 while (chars_written == 0) |
|
162 { |
|
163 delete [] buf; |
|
164 buf = new char[bufsize]; |
|
165 buf[0] = '\0'; |
|
166 |
3255
|
167 chars_written = ::strftime (buf, bufsize, fmt_str, &t); |
3253
|
168 |
|
169 bufsize *= 2; |
|
170 } |
|
171 |
3344
|
172 #if defined (HAVE_TM_ZONE) |
|
173 delete [] ps; |
|
174 #endif |
|
175 |
3253
|
176 retval = buf; |
|
177 |
|
178 delete [] buf; |
|
179 |
|
180 return retval; |
|
181 } |
|
182 |
|
183 void |
|
184 octave_base_tm::init (void *p) |
|
185 { |
|
186 struct tm *t = static_cast<struct tm*> (p); |
|
187 |
|
188 tm_sec = t->tm_sec; |
|
189 tm_min = t->tm_min; |
|
190 tm_hour = t->tm_hour; |
|
191 tm_mday = t->tm_mday; |
|
192 tm_mon = t->tm_mon; |
|
193 tm_year = t->tm_year; |
|
194 tm_wday = t->tm_wday; |
|
195 tm_yday = t->tm_yday; |
|
196 tm_isdst = t->tm_isdst; |
|
197 |
|
198 #if defined (HAVE_TM_ZONE) |
|
199 tm_zone = t->tm_zone; |
|
200 #elif defined (HAVE_TZNAME) |
|
201 if (t->tm_isdst == 0 || t->tm_isdst == 1) |
|
202 tm_zone = tzname[t->tm_isdst]; |
|
203 #endif |
|
204 } |
|
205 |
|
206 void |
|
207 octave_localtime::init (const octave_time& ot) |
|
208 { |
|
209 tm_usec = ot.usec (); |
|
210 |
3255
|
211 time_t t = ot; |
3253
|
212 |
|
213 octave_base_tm::init (localtime (&t)); |
|
214 } |
|
215 |
|
216 void |
|
217 octave_gmtime::init (const octave_time& ot) |
|
218 { |
|
219 tm_usec = ot.usec (); |
|
220 |
3255
|
221 time_t t = ot; |
3253
|
222 |
|
223 octave_base_tm::init (gmtime (&t)); |
|
224 } |
|
225 |
3465
|
226 void |
|
227 octave_strptime::init (const string& str, const string& fmt) |
|
228 { |
|
229 struct tm t; |
|
230 |
|
231 t.tm_sec = 0; |
|
232 t.tm_min = 0; |
|
233 t.tm_hour = 0; |
|
234 t.tm_mday = 0; |
|
235 t.tm_mon = 0; |
|
236 t.tm_year = 0; |
|
237 t.tm_wday = 0; |
|
238 t.tm_yday = 0; |
|
239 t.tm_isdst = 0; |
|
240 |
|
241 #if defined (HAVE_TM_ZONE) |
|
242 char *ps = strsave (""); |
|
243 t.tm_zone = ps; |
|
244 #endif |
|
245 |
|
246 char *p = strsave (str.c_str ()); |
|
247 |
|
248 char *q = strptime (p, fmt.c_str (), &t); |
|
249 |
|
250 nchars = p - q; |
|
251 |
|
252 delete [] p; |
|
253 |
|
254 octave_base_tm::init (&t); |
|
255 |
|
256 #if defined (HAVE_TM_ZONE) |
|
257 delete ps; |
|
258 #endif |
|
259 } |
|
260 |
3253
|
261 /* |
|
262 ;;; Local Variables: *** |
|
263 ;;; mode: C++ *** |
|
264 ;;; End: *** |
|
265 */ |