GNU Octave  4.0.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
time.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1996-2015 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 3 of the License, or (at your
10 option) any 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, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 
27 #include <string>
28 
29 #include "defun.h"
30 #include "error.h"
31 #include "oct-map.h"
32 #include "oct-time.h"
33 #include "ov.h"
34 #include "oct-obj.h"
35 
36 // Date and time functions.
37 
38 static octave_scalar_map
40 {
42 
43  m.assign ("usec", static_cast<double> (t.usec ()));
44  m.assign ("sec", static_cast<double> (t.sec ()));
45  m.assign ("min", static_cast<double> (t.min ()));
46  m.assign ("hour", static_cast<double> (t.hour ()));
47  m.assign ("mday", static_cast<double> (t.mday ()));
48  m.assign ("mon", static_cast<double> (t.mon ()));
49  m.assign ("year", static_cast<double> (t.year ()));
50  m.assign ("wday", static_cast<double> (t.wday ()));
51  m.assign ("yday", static_cast<double> (t.yday ()));
52  m.assign ("isdst", static_cast<double> (t.isdst ()));
53  m.assign ("zone", t.zone ());
54 
55  return m;
56 }
57 
58 static inline int
59 intfield (const octave_scalar_map& m, const std::string& k)
60 {
61  int retval = 0;
62 
63  octave_value v = m.getfield (k);
64 
65  if (! v.is_empty ())
66  retval = v.int_value ();
67 
68  return retval;
69 }
70 
71 static inline std::string
72 stringfield (const octave_scalar_map& m, const std::string& k)
73 {
74  std::string retval;
75 
76  octave_value v = m.getfield (k);
77 
78  if (! v.is_empty ())
79  retval = v.string_value ();
80 
81  return retval;
82 }
83 
84 static octave_base_tm
86 {
87  octave_base_tm tm;
88 
89  tm.usec (intfield (m, "usec"));
90  tm.sec (intfield (m, "sec"));
91  tm.min (intfield (m, "min"));
92  tm.hour (intfield (m, "hour"));
93  tm.mday (intfield (m, "mday"));
94  tm.mon (intfield (m, "mon"));
95  tm.year (intfield (m, "year"));
96  tm.wday (intfield (m, "wday"));
97  tm.yday (intfield (m, "yday"));
98  tm.isdst (intfield (m, "isdst"));
99  tm.zone (stringfield (m, "zone"));
100 
101  return tm;
102 }
103 
104 DEFUN (time, args, ,
105  "-*- texinfo -*-\n\
106 @deftypefn {Built-in Function} {@var{seconds} =} time ()\n\
107 Return the current time as the number of seconds since the epoch.\n\
108 \n\
109 The epoch is referenced to 00:00:00 CUT (Coordinated Universal Time) 1 Jan\n\
110 1970. For example, on Monday February 17, 1997 at 07:15:06 CUT, the value\n\
111 returned by @code{time} was 856163706.\n\
112 @seealso{strftime, strptime, localtime, gmtime, mktime, now, date, clock, datenum, datestr, datevec, calendar, weekday}\n\
113 @end deftypefn")
114 {
115  octave_value retval;
116 
117  if (args.length () == 0)
118  retval = octave_time ();
119  else
120  print_usage ();
121 
122  return retval;
123 }
124 
125 /*
126 %!assert (time () > 0)
127 
128 %!error time (1)
129 */
130 
131 DEFUN (gmtime, args, ,
132  "-*- texinfo -*-\n\
133 @deftypefn {Built-in Function} {@var{tm_struct} =} gmtime (@var{t})\n\
134 Given a value returned from @code{time}, or any non-negative integer,\n\
135 return a time structure corresponding to CUT (Coordinated Universal Time).\n\
136 \n\
137 For example:\n\
138 \n\
139 @example\n\
140 @group\n\
141 gmtime (time ())\n\
142  @result{} @{\n\
143  usec = 0\n\
144  sec = 6\n\
145  min = 15\n\
146  hour = 7\n\
147  mday = 17\n\
148  mon = 1\n\
149  year = 97\n\
150  wday = 1\n\
151  yday = 47\n\
152  isdst = 0\n\
153  zone = CST\n\
154  @}\n\
155 @end group\n\
156 @end example\n\
157 @seealso{strftime, strptime, localtime, mktime, time, now, date, clock, datenum, datestr, datevec, calendar, weekday}\n\
158 @end deftypefn")
159 {
160  octave_value retval;
161 
162  if (args.length () == 1)
163  {
164  double tmp = args(0).double_value ();
165 
166  if (! error_state)
167  retval = octave_value (mk_tm_map (octave_gmtime (tmp)));
168  }
169  else
170  print_usage ();
171 
172  return retval;
173 }
174 
175 /*
176 %!test
177 %! ts = gmtime (time ());
178 %! assert (isstruct (ts));
179 %! assert (isfield (ts, "usec"));
180 %! assert (isfield (ts, "year"));
181 %! assert (isfield (ts, "mon"));
182 %! assert (isfield (ts, "mday"));
183 %! assert (isfield (ts, "sec"));
184 %! assert (isfield (ts, "min"));
185 %! assert (isfield (ts, "wday"));
186 %! assert (isfield (ts, "hour"));
187 %! assert (isfield (ts, "isdst"));
188 %! assert (isfield (ts, "yday"));
189 
190 %!error gmtime ()
191 %!error gmtime (1, 2)
192 */
193 
194 DEFUN (localtime, args, ,
195  "-*- texinfo -*-\n\
196 @deftypefn {Built-in Function} {@var{tm_struct} =} localtime (@var{t})\n\
197 Given a value returned from @code{time}, or any non-negative integer,\n\
198 return a time structure corresponding to the local time zone.\n\
199 \n\
200 @example\n\
201 @group\n\
202 localtime (time ())\n\
203  @result{} @{\n\
204  usec = 0\n\
205  sec = 6\n\
206  min = 15\n\
207  hour = 1\n\
208  mday = 17\n\
209  mon = 1\n\
210  year = 97\n\
211  wday = 1\n\
212  yday = 47\n\
213  isdst = 0\n\
214  zone = CST\n\
215  @}\n\
216 @end group\n\
217 @end example\n\
218 @seealso{strftime, strptime, gmtime, mktime, time, now, date, clock, datenum, datestr, datevec, calendar, weekday}\n\
219 @end deftypefn")
220 {
221  octave_value retval;
222 
223  if (args.length () == 1)
224  {
225  double tmp = args(0).double_value ();
226 
227  if (! error_state)
228  retval = octave_value (mk_tm_map (octave_localtime (tmp)));
229  }
230  else
231  print_usage ();
232 
233  return retval;
234 }
235 
236 /*
237 %!test
238 %! ts = localtime (time ());
239 %! assert (isstruct (ts));
240 %! assert (isfield (ts, "usec"));
241 %! assert (isfield (ts, "year"));
242 %! assert (isfield (ts, "mon"));
243 %! assert (isfield (ts, "mday"));
244 %! assert (isfield (ts, "sec"));
245 %! assert (isfield (ts, "min"));
246 %! assert (isfield (ts, "wday"));
247 %! assert (isfield (ts, "hour"));
248 %! assert (isfield (ts, "isdst"));
249 %! assert (isfield (ts, "yday"));
250 
251 %!error localtime ()
252 %!error localtime (1, 2)
253 */
254 
255 DEFUN (mktime, args, ,
256  "-*- texinfo -*-\n\
257 @deftypefn {Built-in Function} {@var{seconds} =} mktime (@var{tm_struct})\n\
258 Convert a time structure corresponding to the local time to the number of\n\
259 seconds since the epoch.\n\
260 \n\
261 For example:\n\
262 \n\
263 @example\n\
264 @group\n\
265 mktime (localtime (time ()))\n\
266  @result{} 856163706\n\
267 @end group\n\
268 @end example\n\
269 @seealso{strftime, strptime, localtime, gmtime, time, now, date, clock, datenum, datestr, datevec, calendar, weekday}\n\
270 @end deftypefn")
271 {
272  octave_value retval;
273 
274  if (args.length () == 1)
275  {
276  octave_scalar_map map = args(0).scalar_map_value ();
277 
278  if (! error_state)
279  {
280  octave_base_tm tm = extract_tm (map);
281 
282  if (! error_state)
283  retval = octave_time (tm);
284  else
285  error ("mktime: invalid TM_STRUCT argument");
286  }
287  else
288  error ("mktime: TM_STRUCT argument must be a structure");
289  }
290  else
291  print_usage ();
292 
293  return retval;
294 }
295 
296 /*
297 %!test
298 %! t = time ();
299 %! assert (fix (mktime (localtime (t))) == fix (t));
300 
301 ## These tests fail on systems with mktime functions of limited
302 ## intelligence:
303 %!assert (datestr (datenum (1969, 1, 1), 0), "01-Jan-1969 00:00:00")
304 %!assert (datestr (datenum (1901, 1, 1), 0), "01-Jan-1901 00:00:00")
305 %!assert (datestr (datenum (1795, 1, 1), 0), "01-Jan-1795 00:00:00")
306 
307 %!error mktime ()
308 %!error mktime (1)
309 %!error mktime (1, 2, 3)
310 %!error mktime (struct ("year", "foo"))
311 */
312 
313 DEFUN (strftime, args, ,
314  "-*- texinfo -*-\n\
315 @deftypefn {Built-in Function} {} strftime (@var{fmt}, @var{tm_struct})\n\
316 Format the time structure @var{tm_struct} in a flexible way using the format\n\
317 string @var{fmt} that contains @samp{%} substitutions similar to those in\n\
318 @code{printf}.\n\
319 \n\
320 Except where noted, substituted fields have a fixed size; numeric fields are\n\
321 padded if necessary. Padding is with zeros by default; for fields that\n\
322 display a single number, padding can be changed or inhibited by following\n\
323 the @samp{%} with one of the modifiers described below. Unknown field\n\
324 specifiers are copied as normal characters. All other characters are copied\n\
325 to the output without change. For example:\n\
326 \n\
327 @example\n\
328 @group\n\
329 strftime (\"%r (%Z) %A %e %B %Y\", localtime (time ()))\n\
330  @result{} \"01:15:06 AM (CST) Monday 17 February 1997\"\n\
331 @end group\n\
332 @end example\n\
333 \n\
334 Octave's @code{strftime} function supports a superset of the ANSI C field\n\
335 specifiers.\n\
336 \n\
337 @noindent\n\
338 Literal character fields:\n\
339 \n\
340 @table @code\n\
341 @item %%\n\
342 % character.\n\
343 \n\
344 @item %n\n\
345 Newline character.\n\
346 \n\
347 @item %t\n\
348 Tab character.\n\
349 @end table\n\
350 \n\
351 @noindent\n\
352 Numeric modifiers (a nonstandard extension):\n\
353 \n\
354 @table @code\n\
355 @item - (dash)\n\
356 Do not pad the field.\n\
357 \n\
358 @item _ (underscore)\n\
359 Pad the field with spaces.\n\
360 @end table\n\
361 \n\
362 @noindent\n\
363 Time fields:\n\
364 \n\
365 @table @code\n\
366 @item %H\n\
367 Hour (00-23).\n\
368 \n\
369 @item %I\n\
370 Hour (01-12).\n\
371 \n\
372 @item %k\n\
373 Hour (0-23).\n\
374 \n\
375 @item %l\n\
376 Hour (1-12).\n\
377 \n\
378 @item %M\n\
379 Minute (00-59).\n\
380 \n\
381 @item %p\n\
382 Locale's AM or PM.\n\
383 \n\
384 @item %r\n\
385 Time, 12-hour (hh:mm:ss [AP]M).\n\
386 \n\
387 @item %R\n\
388 Time, 24-hour (hh:mm).\n\
389 \n\
390 @item %s\n\
391 Time in seconds since 00:00:00, Jan 1, 1970 (a nonstandard extension).\n\
392 \n\
393 @item %S\n\
394 Second (00-61).\n\
395 \n\
396 @item %T\n\
397 Time, 24-hour (hh:mm:ss).\n\
398 \n\
399 @item %X\n\
400 Locale's time representation (%H:%M:%S).\n\
401 \n\
402 @item %Z\n\
403 Time zone (EDT), or nothing if no time zone is determinable.\n\
404 @end table\n\
405 \n\
406 @noindent\n\
407 Date fields:\n\
408 \n\
409 @table @code\n\
410 @item %a\n\
411 Locale's abbreviated weekday name (Sun-Sat).\n\
412 \n\
413 @item %A\n\
414 Locale's full weekday name, variable length (Sunday-Saturday).\n\
415 \n\
416 @item %b\n\
417 Locale's abbreviated month name (Jan-Dec).\n\
418 \n\
419 @item %B\n\
420 Locale's full month name, variable length (January-December).\n\
421 \n\
422 @item %c\n\
423 Locale's date and time (Sat Nov 04 12:02:33 EST 1989).\n\
424 \n\
425 @item %C\n\
426 Century (00-99).\n\
427 \n\
428 @item %d\n\
429 Day of month (01-31).\n\
430 \n\
431 @item %e\n\
432 Day of month ( 1-31).\n\
433 \n\
434 @item %D\n\
435 Date (mm/dd/yy).\n\
436 \n\
437 @item %h\n\
438 Same as %b.\n\
439 \n\
440 @item %j\n\
441 Day of year (001-366).\n\
442 \n\
443 @item %m\n\
444 Month (01-12).\n\
445 \n\
446 @item %U\n\
447 Week number of year with Sunday as first day of week (00-53).\n\
448 \n\
449 @item %w\n\
450 Day of week (0-6).\n\
451 \n\
452 @item %W\n\
453 Week number of year with Monday as first day of week (00-53).\n\
454 \n\
455 @item %x\n\
456 Locale's date representation (mm/dd/yy).\n\
457 \n\
458 @item %y\n\
459 Last two digits of year (00-99).\n\
460 \n\
461 @item %Y\n\
462 Year (1970-).\n\
463 @end table\n\
464 @seealso{strptime, localtime, gmtime, mktime, time, now, date, clock, datenum, datestr, datevec, calendar, weekday}\n\
465 @end deftypefn")
466 {
467  octave_value retval;
468 
469  if (args.length () == 2)
470  {
471  if (args(0).is_string ())
472  {
473  std::string fmt = args(0).string_value ();
474 
475  octave_scalar_map map = args(1).scalar_map_value ();
476 
477  if (! error_state)
478  {
479  octave_base_tm tm = extract_tm (map);
480 
481  if (! error_state)
482  retval = tm.strftime (fmt);
483  else
484  error ("strftime: invalid TM_STRUCT argument");
485  }
486  else
487  error ("strftime: TM_STRUCT must be a structure");
488  }
489  else
490  error ("strftime: FMT must be a string");
491  }
492  else
493  print_usage ();
494 
495  return retval;
496 }
497 
498 /*
499 %!assert (ischar (strftime ("%%%n%t%H%I%k%l", localtime (time ()))));
500 %!assert (ischar (strftime ("%M%p%r%R%s%S%T", localtime (time ()))));
501 %!assert (ischar (strftime ("%X%Z%z%a%A%b%B", localtime (time ()))));
502 %!assert (ischar (strftime ("%c%C%d%e%D%h%j", localtime (time ()))));
503 %!assert (ischar (strftime ("%m%U%w%W%x%y%Y", localtime (time ()))));
504 
505 %!error strftime ()
506 %!error strftime ("foo", 1)
507 %!error strftime ("foo", struct ("year", "foo"))
508 %!error strftime ("foo", localtime (time ()), 1)
509 */
510 
511 DEFUN (strptime, args, ,
512  "-*- texinfo -*-\n\
513 @deftypefn {Built-in Function} {[@var{tm_struct}, @var{nchars}] =} strptime (@var{str}, @var{fmt})\n\
514 Convert the string @var{str} to the time structure @var{tm_struct} under\n\
515 the control of the format string @var{fmt}.\n\
516 \n\
517 If @var{fmt} fails to match, @var{nchars} is 0; otherwise, it is set to the\n\
518 position of last matched character plus 1. Always check for this unless\n\
519 you're absolutely sure the date string will be parsed correctly.\n\
520 @seealso{strftime, localtime, gmtime, mktime, time, now, date, clock, datenum, datestr, datevec, calendar, weekday}\n\
521 @end deftypefn")
522 {
523  octave_value_list retval;
524 
525  if (args.length () == 2)
526  {
527  if (args(0).is_string ())
528  {
529  std::string str = args(0).string_value ();
530 
531  if (args(1).is_string ())
532  {
533  std::string fmt = args(1).string_value ();
534 
535  octave_strptime t (str, fmt);
536 
537  retval(1) = t.characters_converted ();
538  retval(0) = octave_value (mk_tm_map (t));
539  }
540  else
541  error ("strptime: FMT must be a string");
542  }
543  else
544  error ("strptime: argument STR must be a string");
545  }
546  else
547  print_usage ();
548 
549  return retval;
550 }
551 
552 /*
553 %!test
554 %! fmt = "%Y-%m-%d %H:%M:%S";
555 %! s = strftime (fmt, localtime (time ()));
556 %! ts = strptime (s, fmt);
557 %! assert (isstruct (ts));
558 %! assert (isfield (ts, "usec"));
559 %! assert (isfield (ts, "year"));
560 %! assert (isfield (ts, "mon"));
561 %! assert (isfield (ts, "mday"));
562 %! assert (isfield (ts, "sec"));
563 %! assert (isfield (ts, "min"));
564 %! assert (isfield (ts, "wday"));
565 %! assert (isfield (ts, "hour"));
566 %! assert (isfield (ts, "isdst"));
567 %! assert (isfield (ts, "yday"));
568 
569 %!error strptime ()
570 */
int mday(void) const
Definition: oct-time.h:214
int wday(void) const
Definition: oct-time.h:217
int isdst(void) const
Definition: oct-time.h:219
OCTINTERP_API void print_usage(void)
Definition: defun.cc:51
std::string strftime(const std::string &fmt) const
Definition: oct-time.cc:144
std::string zone(void) const
Definition: oct-time.h:221
int int_value(bool req_int=false, bool frc_str_conv=false) const
Definition: ov.h:730
#define DEFUN(name, args_name, nargout_name, doc)
Definition: defun.h:44
int min(void) const
Definition: oct-time.h:212
void error(const char *fmt,...)
Definition: error.cc:476
int characters_converted(void) const
Definition: oct-time.h:357
static std::string stringfield(const octave_scalar_map &m, const std::string &k)
Definition: time.cc:72
static int intfield(const octave_scalar_map &m, const std::string &k)
Definition: time.cc:59
int mon(void) const
Definition: oct-time.h:215
int year(void) const
Definition: oct-time.h:216
int hour(void) const
Definition: oct-time.h:213
std::string string_value(bool force=false) const
Definition: ov.h:897
int error_state
Definition: error.cc:101
int usec(void) const
Definition: oct-time.h:210
int yday(void) const
Definition: oct-time.h:218
bool is_empty(void) const
Definition: ov.h:526
void assign(const std::string &k, const octave_value &val)
Definition: oct-map.h:225
int sec(void) const
Definition: oct-time.h:211
octave_value getfield(const std::string &key) const
Definition: oct-map.cc:164
static octave_scalar_map mk_tm_map(const octave_base_tm &t)
Definition: time.cc:39
double double_value(bool frc_str_conv=false) const
Definition: ov.h:759
static octave_base_tm extract_tm(const octave_scalar_map &m)
Definition: time.cc:85
return octave_value(v1.char_array_value().concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string())? '\'': '"'))