2928
|
1 /* |
|
2 |
|
3 Copyright (C) 1996, 1997 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 <string> |
|
28 |
|
29 #include "defun-dld.h" |
|
30 #include "error.h" |
|
31 #include "oct-map.h" |
3254
|
32 #include "oct-time.h" |
2928
|
33 #include "ov.h" |
|
34 #include "oct-obj.h" |
|
35 |
|
36 // Date and time functions. |
|
37 |
|
38 static Octave_map |
3254
|
39 mk_tm_map (const octave_base_tm& t) |
2928
|
40 { |
|
41 Octave_map m; |
|
42 |
3254
|
43 m ["usec"] = static_cast<double> (t.usec ()); |
|
44 m ["sec"] = static_cast<double> (t.sec ()); |
|
45 m ["min"] = static_cast<double> (t.min ()); |
|
46 m ["hour"] = static_cast<double> (t.hour ()); |
|
47 m ["mday"] = static_cast<double> (t.mday ()); |
|
48 m ["mon"] = static_cast<double> (t.mon ()); |
|
49 m ["year"] = static_cast<double> (t.year ()); |
|
50 m ["wday"] = static_cast<double> (t.wday ()); |
|
51 m ["yday"] = static_cast<double> (t.yday ()); |
|
52 m ["isdst"] = static_cast<double> (t.isdst ()); |
|
53 m ["zone"] = t.zone (); |
2928
|
54 |
|
55 return m; |
|
56 } |
|
57 |
3254
|
58 static octave_base_tm |
|
59 extract_tm (Octave_map &m) |
2928
|
60 { |
3254
|
61 octave_base_tm tm; |
2928
|
62 |
3254
|
63 tm.usec (static_cast<int> (m ["usec"] . double_value ())); |
|
64 tm.sec (static_cast<int> (m ["sec"] . double_value ())); |
|
65 tm.min (static_cast<int> (m ["min"] . double_value ())); |
|
66 tm.hour (static_cast<int> (m ["hour"] . double_value ())); |
|
67 tm.mday (static_cast<int> (m ["mday"] . double_value ())); |
|
68 tm.mon (static_cast<int> (m ["mon"] . double_value ())); |
|
69 tm.year (static_cast<int> (m ["year"] . double_value ())); |
|
70 tm.wday (static_cast<int> (m ["wday"] . double_value ())); |
|
71 tm.yday (static_cast<int> (m ["yday"] . double_value ())); |
|
72 tm.isdst (static_cast<int> (m ["isdst"] . double_value ())); |
|
73 tm.zone (m ["zone"] . string_value ()); |
2928
|
74 |
3254
|
75 return tm; |
2928
|
76 } |
|
77 |
3255
|
78 DEFUN_DLD (time, args, , |
2928
|
79 "time ()\n\ |
|
80 \n\ |
|
81 Return current time. On Unix systems, this is the number of\n\ |
|
82 seconds since the epoch.") |
|
83 { |
3255
|
84 octave_value retval; |
2928
|
85 |
3255
|
86 if (args.length () == 0) |
|
87 retval = static_cast<double> (octave_time ()); |
|
88 else |
|
89 print_usage ("time"); |
|
90 |
|
91 return retval; |
2928
|
92 } |
|
93 |
|
94 DEFUN_DLD (gmtime, args, , |
|
95 "gmtime (TIME)\n\ |
|
96 \n\ |
|
97 Given a value returned from time(), return a structure like that\n\ |
|
98 returned from localtime() but with values corresponding to\n\ |
|
99 Coordinated Universal Time (UTC).") |
|
100 { |
|
101 octave_value_list retval; |
|
102 |
|
103 if (args.length () == 1) |
|
104 { |
|
105 double tmp = args(0).double_value (); |
|
106 |
|
107 if (! error_state) |
3254
|
108 retval = octave_value (mk_tm_map (octave_gmtime (tmp))); |
2928
|
109 } |
|
110 else |
|
111 print_usage ("gmtime"); |
|
112 |
|
113 return retval; |
|
114 } |
|
115 |
|
116 DEFUN_DLD (localtime, args, , |
|
117 "localtime (TIME)\n\ |
|
118 \n\ |
|
119 Given a value returned from time(), return a structure with\n\ |
|
120 the following elements:\n\ |
|
121 \n\ |
|
122 usec : microseconds after the second (0, 999999)\n\ |
|
123 sec : seconds after the minute (0, 61)\n\ |
|
124 min : minutes after the hour (0, 59)\n\ |
|
125 hour : hours since midnight (0, 23)\n\ |
|
126 mday : day of the month (1, 31)\n\ |
|
127 mon : months since January (0, 11)\n\ |
|
128 year : years since 1900\n\ |
|
129 wday : days since Sunday (0, 6)\n\ |
|
130 yday : days since January 1 (0, 365)\n\ |
3254
|
131 isdst : daylight savings time flag\n\ |
|
132 zone : time zone") |
2928
|
133 { |
|
134 octave_value_list retval; |
|
135 |
|
136 if (args.length () == 1) |
|
137 { |
|
138 double tmp = args(0).double_value (); |
|
139 |
|
140 if (! error_state) |
3254
|
141 retval = octave_value (mk_tm_map (octave_localtime (tmp))); |
2928
|
142 } |
|
143 else |
|
144 print_usage ("localtime"); |
|
145 |
|
146 return retval; |
|
147 } |
|
148 |
|
149 DEFUN_DLD (mktime, args, , |
|
150 "mktime (TMSTRUCT)") |
|
151 { |
|
152 octave_value_list retval; |
|
153 |
3254
|
154 if (args.length () == 1) |
2928
|
155 { |
|
156 Octave_map map = args(0).map_value (); |
|
157 |
3254
|
158 if (! error_state) |
|
159 { |
|
160 octave_base_tm tm = extract_tm (map); |
|
161 |
|
162 if (! error_state) |
3255
|
163 retval = static_cast<double> (octave_time (tm)); |
3254
|
164 else |
|
165 error ("mktime: invalid TMSTRUCT argument"); |
|
166 } |
|
167 else |
|
168 error ("mktime: expecting structure argument"); |
2928
|
169 } |
|
170 else |
|
171 print_usage ("mktime"); |
|
172 |
|
173 return retval; |
|
174 } |
|
175 |
|
176 DEFUN_DLD (strftime, args, , |
|
177 "strftime (FMT, TMSTRUCT)\n\ |
|
178 \n\ |
|
179 Performs `%' substitutions similar to those in printf. Except where\n\ |
|
180 noted, substituted fields have a fixed size; numeric fields are\n\ |
|
181 padded if necessary. Padding is with zeros by default; for fields\n\ |
|
182 that display a single number, padding can be changed or inhibited by\n\ |
|
183 following the `%' with one of the modifiers described below.\n\ |
|
184 Unknown field specifiers are copied as normal characters. All other\n\ |
|
185 characters are copied to the output without change.\n\ |
|
186 \n\ |
|
187 Supports a superset of the ANSI C field specifiers.\n\ |
|
188 \n\ |
|
189 Literal character fields:\n\ |
|
190 \n\ |
|
191 % %\n\ |
|
192 n newline\n\ |
|
193 t tab\n\ |
|
194 \n\ |
|
195 Numeric modifiers (a nonstandard extension):\n\ |
|
196 \n\ |
|
197 - do not pad the field\n\ |
|
198 _ pad the field with spaces\n\ |
|
199 \n\ |
|
200 Time fields:\n\ |
|
201 \n\ |
|
202 %H hour (00..23)\n\ |
|
203 %I hour (01..12)\n\ |
|
204 %k hour ( 0..23)\n\ |
|
205 %l hour ( 1..12)\n\ |
|
206 %M minute (00..59)\n\ |
|
207 %p locale's AM or PM\n\ |
|
208 %r time, 12-hour (hh:mm:ss [AP]M)\n\ |
|
209 %R time, 24-hour (hh:mm)\n\ |
|
210 %s time in seconds since 00:00:00, Jan 1, 1970 (a nonstandard extension)\n\ |
|
211 %S second (00..61)\n\ |
|
212 %T time, 24-hour (hh:mm:ss)\n\ |
|
213 %X locale's time representation (%H:%M:%S)\n\ |
|
214 %Z time zone (EDT), or nothing if no time zone is determinable\n\ |
|
215 %z offset from GMT\n\ |
|
216 \n\ |
|
217 Date fields:\n\ |
|
218 \n\ |
|
219 %a locale's abbreviated weekday name (Sun..Sat)\n\ |
|
220 %A locale's full weekday name, variable length (Sunday..Saturday)\n\ |
|
221 %b locale's abbreviated month name (Jan..Dec)\n\ |
|
222 %B locale's full month name, variable length (January..December)\n\ |
|
223 %c locale's date and time (Sat Nov 04 12:02:33 EST 1989)\n\ |
|
224 %C century (00..99)\n\ |
|
225 %d day of month (01..31)\n\ |
|
226 %e day of month ( 1..31)\n\ |
|
227 %D date (mm/dd/yy)\n\ |
|
228 %h same as %b\n\ |
|
229 %j day of year (001..366)\n\ |
|
230 %m month (01..12)\n\ |
|
231 %U week number of year with Sunday as first day of week (00..53)\n\ |
|
232 %w day of week (0..6)\n\ |
|
233 %W week number of year with Monday as first day of week (00..53)\n\ |
|
234 %x locale's date representation (mm/dd/yy)\n\ |
|
235 %y last two digits of year (00..99)\n\ |
|
236 %Y year (1970...)") |
|
237 { |
|
238 octave_value_list retval; |
|
239 |
3254
|
240 if (args.length () == 2) |
2928
|
241 { |
|
242 string fmt = args(0).string_value (); |
|
243 |
|
244 if (! error_state) |
|
245 { |
3254
|
246 Octave_map map = args(1).map_value (); |
2928
|
247 |
3254
|
248 if (! error_state) |
3233
|
249 { |
3254
|
250 octave_base_tm tm = extract_tm (map); |
3233
|
251 |
3254
|
252 if (! error_state) |
3255
|
253 retval = tm.strftime (fmt); |
3254
|
254 else |
|
255 error ("strftime: invalid TMSTRUCT argument"); |
3233
|
256 } |
3254
|
257 else |
|
258 error ("strftime: expecting structure as second argument"); |
2928
|
259 } |
3254
|
260 else |
|
261 error ("strftime: expecting format string as first argument"); |
2928
|
262 } |
|
263 else |
|
264 print_usage ("strftime"); |
|
265 |
|
266 return retval; |
|
267 } |
|
268 |
|
269 /* |
|
270 ;;; Local Variables: *** |
|
271 ;;; mode: C++ *** |
|
272 ;;; End: *** |
|
273 */ |