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