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 |
|
100 #define DEFINE_SET_INT_FIELD_FCN(f, lo, hi) \ |
|
101 octave_base_tm& \ |
|
102 octave_base_tm::f (int v) \ |
|
103 { \ |
|
104 if (v < lo || v > hi) \ |
|
105 (*current_liboctave_error_handler) \ |
|
106 ("invalid value specified for " #f); \ |
|
107 \ |
|
108 tm_ ## f = v; \ |
|
109 \ |
|
110 return *this; \ |
|
111 } |
|
112 |
|
113 DEFINE_SET_INT_FIELD_FCN (usec, 0, 1000000) |
|
114 DEFINE_SET_INT_FIELD_FCN (sec, 0, 61) |
|
115 DEFINE_SET_INT_FIELD_FCN (min, 0, 59) |
|
116 DEFINE_SET_INT_FIELD_FCN (hour, 0, 23) |
|
117 DEFINE_SET_INT_FIELD_FCN (mday, 1, 31) |
|
118 DEFINE_SET_INT_FIELD_FCN (mon, 0, 11) |
|
119 DEFINE_SET_INT_FIELD_FCN (year, INT_MIN, INT_MAX) |
|
120 DEFINE_SET_INT_FIELD_FCN (wday, 0, 6) |
|
121 DEFINE_SET_INT_FIELD_FCN (yday, 0, 365) |
|
122 DEFINE_SET_INT_FIELD_FCN (isdst, 0, 1) |
|
123 |
|
124 octave_base_tm& |
3504
|
125 octave_base_tm::zone (const std::string& s) |
3253
|
126 { |
|
127 tm_zone = s; |
|
128 return *this; |
|
129 } |
|
130 |
|
131 #if !defined STRFTIME_BUF_INITIAL_SIZE |
|
132 #define STRFTIME_BUF_INITIAL_SIZE 128 |
|
133 #endif |
|
134 |
3504
|
135 std::string |
|
136 octave_base_tm::strftime (const std::string& fmt) const |
3253
|
137 { |
3504
|
138 std::string retval; |
3253
|
139 |
|
140 struct tm t; |
|
141 |
|
142 t.tm_sec = tm_sec; |
|
143 t.tm_min = tm_min; |
|
144 t.tm_hour = tm_hour; |
|
145 t.tm_mday = tm_mday; |
|
146 t.tm_mon = tm_mon; |
|
147 t.tm_year = tm_year; |
|
148 t.tm_wday = tm_wday; |
|
149 t.tm_yday = tm_yday; |
|
150 t.tm_isdst = tm_isdst; |
|
151 |
|
152 #if defined (HAVE_TM_ZONE) |
3344
|
153 char *ps = strsave (tm_zone.c_str ()); |
|
154 t.tm_zone = ps; |
3253
|
155 #endif |
|
156 |
|
157 const char *fmt_str = fmt.c_str (); |
|
158 |
|
159 char *buf = 0; |
|
160 size_t bufsize = STRFTIME_BUF_INITIAL_SIZE; |
|
161 size_t chars_written = 0; |
|
162 |
|
163 while (chars_written == 0) |
|
164 { |
|
165 delete [] buf; |
|
166 buf = new char[bufsize]; |
|
167 buf[0] = '\0'; |
|
168 |
3255
|
169 chars_written = ::strftime (buf, bufsize, fmt_str, &t); |
3253
|
170 |
|
171 bufsize *= 2; |
|
172 } |
|
173 |
3344
|
174 #if defined (HAVE_TM_ZONE) |
|
175 delete [] ps; |
|
176 #endif |
|
177 |
3253
|
178 retval = buf; |
|
179 |
|
180 delete [] buf; |
|
181 |
|
182 return retval; |
|
183 } |
|
184 |
|
185 void |
|
186 octave_base_tm::init (void *p) |
|
187 { |
|
188 struct tm *t = static_cast<struct tm*> (p); |
|
189 |
|
190 tm_sec = t->tm_sec; |
|
191 tm_min = t->tm_min; |
|
192 tm_hour = t->tm_hour; |
|
193 tm_mday = t->tm_mday; |
|
194 tm_mon = t->tm_mon; |
|
195 tm_year = t->tm_year; |
|
196 tm_wday = t->tm_wday; |
|
197 tm_yday = t->tm_yday; |
|
198 tm_isdst = t->tm_isdst; |
|
199 |
|
200 #if defined (HAVE_TM_ZONE) |
|
201 tm_zone = t->tm_zone; |
|
202 #elif defined (HAVE_TZNAME) |
|
203 if (t->tm_isdst == 0 || t->tm_isdst == 1) |
|
204 tm_zone = tzname[t->tm_isdst]; |
|
205 #endif |
|
206 } |
|
207 |
|
208 void |
|
209 octave_localtime::init (const octave_time& ot) |
|
210 { |
|
211 tm_usec = ot.usec (); |
|
212 |
3255
|
213 time_t t = ot; |
3253
|
214 |
|
215 octave_base_tm::init (localtime (&t)); |
|
216 } |
|
217 |
|
218 void |
|
219 octave_gmtime::init (const octave_time& ot) |
|
220 { |
|
221 tm_usec = ot.usec (); |
|
222 |
3255
|
223 time_t t = ot; |
3253
|
224 |
|
225 octave_base_tm::init (gmtime (&t)); |
|
226 } |
|
227 |
3465
|
228 void |
3504
|
229 octave_strptime::init (const std::string& str, const std::string& fmt) |
3465
|
230 { |
|
231 struct tm t; |
|
232 |
|
233 t.tm_sec = 0; |
|
234 t.tm_min = 0; |
|
235 t.tm_hour = 0; |
|
236 t.tm_mday = 0; |
|
237 t.tm_mon = 0; |
|
238 t.tm_year = 0; |
|
239 t.tm_wday = 0; |
|
240 t.tm_yday = 0; |
|
241 t.tm_isdst = 0; |
|
242 |
|
243 #if defined (HAVE_TM_ZONE) |
|
244 char *ps = strsave (""); |
|
245 t.tm_zone = ps; |
|
246 #endif |
|
247 |
|
248 char *p = strsave (str.c_str ()); |
|
249 |
3706
|
250 char *q = oct_strptime (p, fmt.c_str (), &t); |
3465
|
251 |
|
252 nchars = p - q; |
|
253 |
|
254 delete [] p; |
|
255 |
|
256 octave_base_tm::init (&t); |
|
257 |
|
258 #if defined (HAVE_TM_ZONE) |
|
259 delete ps; |
|
260 #endif |
|
261 } |
|
262 |
3253
|
263 /* |
|
264 ;;; Local Variables: *** |
|
265 ;;; mode: C++ *** |
|
266 ;;; End: *** |
|
267 */ |