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