523
|
1 // timefns.cc -*- C++ -*- |
|
2 /* |
|
3 |
1009
|
4 Copyright (C) 1992, 1993, 1994, 1995 John W. Eaton |
523
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include "config.h" |
|
26 #endif |
|
27 |
|
28 #include "tree-const.h" |
|
29 #include "oct-obj.h" |
792
|
30 #include "systime.h" |
1184
|
31 #include "oct-map.h" |
523
|
32 #include "defun.h" |
1184
|
33 #include "utils.h" |
|
34 #include "help.h" |
523
|
35 |
1184
|
36 // Date and time functions. |
|
37 |
|
38 static Octave_map |
|
39 mk_tm_map (struct tm *tm, double fraction) |
|
40 { |
|
41 Octave_map m; |
|
42 |
1190
|
43 m ["tm_usec"] = fraction * 1e6; |
|
44 m ["tm_sec"] = (double) tm->tm_sec; |
|
45 m ["tm_min"] = (double) tm->tm_min; |
|
46 m ["tm_hour"] = (double) tm->tm_hour; |
|
47 m ["tm_mday"] = (double) tm->tm_mday; |
|
48 m ["tm_mon"] = (double) tm->tm_mon; |
|
49 m ["tm_year"] = (double) tm->tm_year; |
|
50 m ["tm_wday"] = (double) tm->tm_wday; |
|
51 m ["tm_yday"] = (double) tm->tm_yday; |
1184
|
52 m ["tm_isdst"] = (double) tm->tm_isdst; |
|
53 #if defined (HAVE_TM_ZONE) |
|
54 m ["tm_zone"] = tm->tm_zone; |
|
55 #elif defined (HAVE_TZNAME) |
|
56 if (tm->tm_isdst && tzname[1] && *tzname[1]) |
|
57 m ["tm_zone"] = tzname[1]; |
|
58 else |
|
59 m ["tm_zone"] = tzname[0]; |
|
60 #else |
|
61 m ["tm_zone"] = zone_name (tm); |
|
62 #endif |
|
63 |
|
64 return m; |
|
65 } |
|
66 |
|
67 static struct tm* |
1190
|
68 extract_tm (Octave_map &m, double& fraction) |
1184
|
69 { |
|
70 static struct tm tm; |
|
71 |
1190
|
72 fraction = (m ["tm_usec"] . double_value ()) / 1e6; |
1184
|
73 tm.tm_sec = NINT (m ["tm_sec"] . double_value ()); |
|
74 tm.tm_min = NINT (m ["tm_min"] . double_value ()); |
|
75 tm.tm_hour = NINT (m ["tm_hour"] . double_value ()); |
|
76 tm.tm_mday = NINT (m ["tm_mday"] . double_value ()); |
|
77 tm.tm_mon = NINT (m ["tm_mon"] . double_value ()); |
|
78 tm.tm_year = NINT (m ["tm_year"] . double_value ()); |
|
79 tm.tm_wday = NINT (m ["tm_wday"] . double_value ()); |
|
80 tm.tm_yday = NINT (m ["tm_yday"] . double_value ()); |
|
81 tm.tm_isdst = NINT (m ["tm_isdst"] . double_value ()); |
1190
|
82 #ifdef HAVE_TMZONE |
1184
|
83 tm.tm_zone = (m ["tm_zone"] . string_value ()); |
1190
|
84 #endif |
1184
|
85 |
|
86 return &tm; |
|
87 } |
|
88 |
|
89 DEFUN ("time", Ftime, Stime, 1, 0, |
|
90 "time ()\n\ |
|
91 \n\ |
|
92 Return current time. On Unix systems, this is the number of\n\ |
|
93 seconds since the epoch.") |
|
94 { |
|
95 time_t now; |
|
96 double fraction = 0.0; |
|
97 |
|
98 #ifdef HAVE_GETTIMEOFDAY |
|
99 |
|
100 struct timeval tp; |
|
101 |
|
102 gettimeofday (&tp, 0); |
|
103 |
|
104 now = tp.tv_sec; |
|
105 |
|
106 fraction = tp.tv_usec / 1e6; |
|
107 |
|
108 #else |
|
109 |
|
110 now = time (0); |
|
111 |
|
112 #endif |
|
113 |
|
114 return (double) now + fraction; |
|
115 } |
|
116 |
|
117 DEFUN ("gmtime", Fgmtime, Sgmtime, 1, 1, |
|
118 "gmtime (TIME)\n\ |
|
119 \n\ |
|
120 Given a value returned from time(), return a structure like that\n\ |
|
121 returned from localtime() but with values corresponding to\n\ |
|
122 Coordinated Universal Time (UTC).") |
|
123 { |
|
124 Octave_object retval; |
|
125 |
|
126 if (args.length () == 1) |
|
127 { |
|
128 double tmp = args(0).double_value (); |
|
129 |
|
130 if (! error_state) |
|
131 { |
|
132 time_t timeval = NINT (tmp); |
|
133 double ip; |
|
134 double fraction = modf (tmp, &ip); |
|
135 |
|
136 retval = tree_constant (mk_tm_map (gmtime (&timeval), fraction)); |
|
137 } |
|
138 } |
|
139 else |
|
140 print_usage ("gmtime"); |
|
141 |
|
142 return retval; |
|
143 } |
|
144 |
|
145 DEFUN ("localtime", Flocaltime, Slocaltime, 1, 1, |
|
146 "localtime (TIME)\n\ |
|
147 \n\ |
|
148 Given a value returned from time(), return a structure with\n\ |
|
149 the following elements:\n\ |
|
150 \n\ |
|
151 tm_usec : microseconds after the second (0, 999999)\n\ |
|
152 tm_sec : seconds after the minute (0, 61)\n\ |
|
153 tm_min : minutes after the hour (0, 59)\n\ |
|
154 tm_hour : hours since midnight (0, 23)\n\ |
|
155 tm_mday : day of the month (1, 31)\n\ |
|
156 tm_mon : months since January (0, 11)\n\ |
|
157 tm_year : years since 1900\n\ |
|
158 tm_wday : days since Sunday (0, 6)\n\ |
|
159 tm_yday : days since January 1 (0, 365)\n\ |
|
160 tm_isdst : Daylight Savings Time flag\n\ |
|
161 tm_zone : Time zone") |
523
|
162 { |
|
163 Octave_object retval; |
|
164 |
1184
|
165 if (args.length () == 1) |
|
166 { |
|
167 double tmp = args(0).double_value (); |
|
168 |
|
169 if (! error_state) |
|
170 { |
|
171 time_t timeval = NINT (tmp); |
|
172 double ip; |
|
173 double fraction = modf (tmp, &ip); |
|
174 |
|
175 retval = tree_constant (mk_tm_map (localtime (&timeval), fraction)); |
|
176 } |
|
177 } |
|
178 else |
|
179 print_usage ("localtime"); |
|
180 |
|
181 return retval; |
|
182 } |
|
183 |
|
184 DEFUN ("mktime", Fmktime, Smktime, 1, 2, |
|
185 "mktime (TMSTRUCT)") |
|
186 { |
|
187 Octave_object retval; |
|
188 |
1187
|
189 if (args.length () == 1 && args(0).is_map ()) |
1184
|
190 { |
|
191 Octave_map map = args(0).map_value (); |
|
192 |
|
193 double fraction; |
|
194 |
|
195 struct tm *tm = extract_tm (map, fraction); |
|
196 |
|
197 if (! error_state) |
|
198 retval = (double) mktime (tm) + fraction; |
|
199 } |
|
200 else |
|
201 print_usage ("mktime"); |
|
202 |
|
203 return retval; |
|
204 } |
523
|
205 |
1184
|
206 DEFUN ("strftime", Fstrftime, Sstrftime, 1, 2, |
|
207 "strftime (FMT, TMSTRUCT)\n\ |
|
208 \n\ |
|
209 Performs `%' substitutions similar to those in printf. Except where\n\ |
|
210 noted, substituted fields have a fixed size; numeric fields are\n\ |
|
211 padded if necessary. Padding is with zeros by default; for fields\n\ |
|
212 that display a single number, padding can be changed or inhibited by\n\ |
|
213 following the `%' with one of the modifiers described below.\n\ |
|
214 Unknown field specifiers are copied as normal characters. All other\n\ |
|
215 characters are copied to the output without change.\n\ |
|
216 \n\ |
|
217 Supports a superset of the ANSI C field specifiers.\n\ |
|
218 \n\ |
|
219 Literal character fields:\n\ |
|
220 \n\ |
|
221 % %\n\ |
|
222 n newline\n\ |
|
223 t tab\n\ |
|
224 \n\ |
|
225 Numeric modifiers (a nonstandard extension):\n\ |
|
226 \n\ |
|
227 - do not pad the field\n\ |
|
228 _ pad the field with spaces\n\ |
|
229 \n\ |
|
230 Time fields:\n\ |
|
231 \n\ |
|
232 %H hour (00..23)\n\ |
|
233 %I hour (01..12)\n\ |
|
234 %k hour ( 0..23)\n\ |
|
235 %l hour ( 1..12)\n\ |
|
236 %M minute (00..59)\n\ |
|
237 %p locale's AM or PM\n\ |
|
238 %r time, 12-hour (hh:mm:ss [AP]M)\n\ |
|
239 %R time, 24-hour (hh:mm)\n\ |
|
240 %s time in seconds since 00:00:00, Jan 1, 1970 (a nonstandard extension)\n\ |
|
241 %S second (00..61)\n\ |
|
242 %T time, 24-hour (hh:mm:ss)\n\ |
|
243 %X locale's time representation (%H:%M:%S)\n\ |
|
244 %Z time zone (EDT), or nothing if no time zone is determinable\n\ |
|
245 \n\ |
|
246 Date fields:\n\ |
|
247 \n\ |
|
248 %a locale's abbreviated weekday name (Sun..Sat)\n\ |
|
249 %A locale's full weekday name, variable length (Sunday..Saturday)\n\ |
|
250 %b locale's abbreviated month name (Jan..Dec)\n\ |
|
251 %B locale's full month name, variable length (January..December)\n\ |
|
252 %c locale's date and time (Sat Nov 04 12:02:33 EST 1989)\n\ |
|
253 %C century (00..99)\n\ |
|
254 %d day of month (01..31)\n\ |
|
255 %e day of month ( 1..31)\n\ |
|
256 %D date (mm/dd/yy)\n\ |
|
257 %h same as %b\n\ |
|
258 %j day of year (001..366)\n\ |
|
259 %m month (01..12)\n\ |
|
260 %U week number of year with Sunday as first day of week (00..53)\n\ |
|
261 %w day of week (0..6)\n\ |
|
262 %W week number of year with Monday as first day of week (00..53)\n\ |
|
263 %x locale's date representation (mm/dd/yy)\n\ |
|
264 %y last two digits of year (00..99)\n\ |
|
265 %Y year (1970...)") |
|
266 { |
|
267 Octave_object retval; |
|
268 |
|
269 if (args.length () == 2 && args(0).is_string () && args(1).is_map ()) |
|
270 { |
|
271 char *fmt = args(0).string_value (); |
|
272 Octave_map map = args(1).map_value (); |
|
273 |
|
274 double fraction; |
|
275 |
|
276 struct tm *tm = extract_tm (map, fraction); |
|
277 |
|
278 if (! error_state) |
|
279 { |
|
280 int bufsize = 128; |
|
281 char *buf = new char [bufsize]; |
|
282 |
|
283 while (! strftime (buf, bufsize, fmt, tm)) |
|
284 { |
|
285 delete [] buf; |
|
286 bufsize *= 2; |
|
287 buf = new char [bufsize]; |
|
288 } |
|
289 |
|
290 retval = buf; |
|
291 |
|
292 delete [] buf; |
|
293 } |
|
294 } |
|
295 else |
|
296 print_usage ("strftime"); |
523
|
297 |
|
298 return retval; |
|
299 } |
|
300 |
|
301 /* |
|
302 ;;; Local Variables: *** |
|
303 ;;; mode: C++ *** |
|
304 ;;; page-delimiter: "^/\\*" *** |
|
305 ;;; End: *** |
|
306 */ |