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