GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
time.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1996-2018 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
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License 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 <https://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if defined (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 "ovl.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 ("gmtoff", static_cast<double> (t.gmtoff ()));
54  m.assign ("zone", t.zone ());
55 
56  return m;
57 }
58 
59 static inline int
60 intfield (const octave_scalar_map& m, const std::string& k, const char *who)
61 {
62  int retval = 0;
63 
64  octave_value v = m.getfield (k);
65 
66  if (! v.isempty ())
67  retval = v.xint_value ("%s: invalid TM_STRUCT argument", who);
68 
69  return retval;
70 }
71 
72 static inline std::string
73 stringfield (const octave_scalar_map& m, const std::string& k, const char *who)
74 {
76 
77  octave_value v = m.getfield (k);
78 
79  if (! v.isempty ())
80  retval = v.xstring_value ("%s: invalid TM_STRUCT argument", who);
81 
82  return retval;
83 }
84 
86 extract_tm (const octave_scalar_map& m, const char *who)
87 {
89 
90  tm.usec (intfield (m, "usec", who));
91  tm.sec (intfield (m, "sec", who));
92  tm.min (intfield (m, "min", who));
93  tm.hour (intfield (m, "hour", who));
94  tm.mday (intfield (m, "mday", who));
95  tm.mon (intfield (m, "mon", who));
96  tm.year (intfield (m, "year", who));
97  tm.wday (intfield (m, "wday", who));
98  tm.yday (intfield (m, "yday", who));
99  tm.isdst (intfield (m, "isdst", who));
100  tm.gmtoff (intfield (m, "gmtoff", who));
101  tm.zone (stringfield (m, "zone", who));
102 
103  return tm;
104 }
105 
106 DEFUN (time, args, ,
107  doc: /* -*- texinfo -*-
108 @deftypefn {} {@var{seconds} =} time ()
109 Return the current time as the number of seconds since the epoch.
110 
111 The epoch is referenced to 00:00:00 UTC (Coordinated Universal Time) 1 Jan
112 1970. For example, on Monday February 17, 1997 at 07:15:06 UTC, the value
113 returned by @code{time} was 856163706.
114 @seealso{strftime, strptime, localtime, gmtime, mktime, now, date, clock, datenum, datestr, datevec, calendar, weekday}
115 @end deftypefn */)
116 {
117  if (args.length () != 0)
118  print_usage ();
119 
120  return ovl (octave::sys::time ());
121 }
122 
123 /*
124 %!assert (time () > 0)
125 
126 %!error time (1)
127 */
128 
129 DEFUN (gmtime, args, ,
130  doc: /* -*- texinfo -*-
131 @deftypefn {} {@var{tm_struct} =} gmtime (@var{t})
132 Given a value returned from @code{time}, or any non-negative integer,
133 return a time structure corresponding to UTC (Coordinated Universal Time).
134 
135 For example:
136 
137 @example
138 @group
139 gmtime (time ())
140  @result{} @{
141  usec = 0
142  sec = 6
143  min = 15
144  hour = 7
145  mday = 17
146  mon = 1
147  year = 97
148  wday = 1
149  yday = 47
150  isdst = 0
151  gmtoff = 0
152  zone = GMT
153  @}
154 @end group
155 @end example
156 @seealso{strftime, strptime, localtime, mktime, time, now, date, clock, datenum, datestr, datevec, calendar, weekday}
157 @end deftypefn */)
158 {
159  if (args.length () != 1)
160  print_usage ();
161 
162  double tmp = args(0).double_value ();
163 
164  return ovl (mk_tm_map (octave::sys::gmtime (tmp)));
165 }
166 
167 /*
168 %!test
169 %! ts = gmtime (time ());
170 %! assert (isstruct (ts));
171 %! assert (isfield (ts, "usec"));
172 %! assert (isfield (ts, "year"));
173 %! assert (isfield (ts, "mon"));
174 %! assert (isfield (ts, "mday"));
175 %! assert (isfield (ts, "sec"));
176 %! assert (isfield (ts, "min"));
177 %! assert (isfield (ts, "wday"));
178 %! assert (isfield (ts, "hour"));
179 %! assert (isfield (ts, "isdst"));
180 %! assert (isfield (ts, "yday"));
181 
182 %!error gmtime ()
183 %!error gmtime (1, 2)
184 */
185 
186 DEFUN (localtime, args, ,
187  doc: /* -*- texinfo -*-
188 @deftypefn {} {@var{tm_struct} =} localtime (@var{t})
189 Given a value returned from @code{time}, or any non-negative integer,
190 return a time structure corresponding to the local time zone.
191 
192 @example
193 @group
194 localtime (time ())
195  @result{} @{
196  usec = 0
197  sec = 6
198  min = 15
199  hour = 1
200  mday = 17
201  mon = 1
202  year = 97
203  wday = 1
204  yday = 47
205  isdst = 0
206  gmtoff = -21600
207  zone = CST
208  @}
209 @end group
210 @end example
211 @seealso{strftime, strptime, gmtime, mktime, time, now, date, clock, datenum, datestr, datevec, calendar, weekday}
212 @end deftypefn */)
213 {
214  if (args.length () != 1)
215  print_usage ();
216 
217  double tmp = args(0).double_value ();
218 
220 }
221 
222 /*
223 %!test
224 %! ts = localtime (time ());
225 %! assert (isstruct (ts));
226 %! assert (isfield (ts, "usec"));
227 %! assert (isfield (ts, "year"));
228 %! assert (isfield (ts, "mon"));
229 %! assert (isfield (ts, "mday"));
230 %! assert (isfield (ts, "sec"));
231 %! assert (isfield (ts, "min"));
232 %! assert (isfield (ts, "wday"));
233 %! assert (isfield (ts, "hour"));
234 %! assert (isfield (ts, "isdst"));
235 %! assert (isfield (ts, "yday"));
236 
237 %!error localtime ()
238 %!error localtime (1, 2)
239 */
240 
241 DEFUN (mktime, args, ,
242  doc: /* -*- texinfo -*-
243 @deftypefn {} {@var{seconds} =} mktime (@var{tm_struct})
244 Convert a time structure corresponding to the local time to the number of
245 seconds since the epoch.
246 
247 For example:
248 
249 @example
250 @group
251 mktime (localtime (time ()))
252  @result{} 856163706
253 @end group
254 @end example
255 @seealso{strftime, strptime, localtime, gmtime, time, now, date, clock, datenum, datestr, datevec, calendar, weekday}
256 @end deftypefn */)
257 {
258  if (args.length () != 1)
259  print_usage ();
260 
261  octave_scalar_map map = args(0).xscalar_map_value ("mktime: TM_STRUCT argument must be a structure");
262 
263  octave::sys::base_tm tm = extract_tm (map, "mktime");
264 
265  return ovl (octave::sys::time (tm));
266 }
267 
268 /*
269 %!test
270 %! t = time ();
271 %! assert (fix (mktime (localtime (t))) == fix (t));
272 
273 ## These tests fail on systems with mktime functions of limited
274 ## intelligence:
275 %!assert (datestr (datenum (1969, 1, 1), 0), "01-Jan-1969 00:00:00")
276 %!assert (datestr (datenum (1901, 1, 1), 0), "01-Jan-1901 00:00:00")
277 %!assert (datestr (datenum (1795, 1, 1), 0), "01-Jan-1795 00:00:00")
278 
279 %!error mktime ()
280 %!error mktime (1)
281 %!error mktime (1, 2, 3)
282 %!error mktime (struct ("year", "foo"))
283 */
284 
285 DEFUN (strftime, args, ,
286  doc: /* -*- texinfo -*-
287 @deftypefn {} {} strftime (@var{fmt}, @var{tm_struct})
288 Format the time structure @var{tm_struct} in a flexible way using the format
289 string @var{fmt} that contains @samp{%} substitutions similar to those in
290 @code{printf}.
291 
292 Except where noted, substituted fields have a fixed size; numeric fields are
293 padded if necessary. Padding is with zeros by default; for fields that
294 display a single number, padding can be changed or inhibited by following
295 the @samp{%} with one of the modifiers described below. Unknown field
296 specifiers are copied as normal characters. All other characters are copied
297 to the output without change. For example:
298 
299 @example
300 @group
301 strftime ("%r (%Z) %A %e %B %Y", localtime (time ()))
302  @result{} "01:15:06 AM (CST) Monday 17 February 1997"
303 @end group
304 @end example
305 
306 Octave's @code{strftime} function supports a superset of the ANSI C field
307 specifiers.
308 
309 @noindent
310 Literal character fields:
311 
312 @table @code
313 @item %%
314 % character.
315 
316 @item %n
317 Newline character.
318 
319 @item %t
320 Tab character.
321 @end table
322 
323 @noindent
324 Numeric modifiers (a nonstandard extension):
325 
326 @table @code
327 @item - (dash)
328 Do not pad the field.
329 
330 @item _ (underscore)
331 Pad the field with spaces.
332 @end table
333 
334 @noindent
335 Time fields:
336 
337 @table @code
338 @item %H
339 Hour (00-23).
340 
341 @item %I
342 Hour (01-12).
343 
344 @item %k
345 Hour (0-23).
346 
347 @item %l
348 Hour (1-12).
349 
350 @item %M
351 Minute (00-59).
352 
353 @item %p
354 Locale's AM or PM.
355 
356 @item %r
357 Time, 12-hour (hh:mm:ss [AP]M).
358 
359 @item %R
360 Time, 24-hour (hh:mm).
361 
362 @item %s
363 Time in seconds since 00:00:00, Jan 1, 1970 (a nonstandard extension).
364 
365 @item %S
366 Second (00-61).
367 
368 @item %T
369 Time, 24-hour (hh:mm:ss).
370 
371 @item %X
372 Locale's time representation (%H:%M:%S).
373 
374 @item %z
375 Offset from UTC (±@nospell{hhmm}), or nothing if no time zone is
376 determinable.
377 
378 @item %Z
379 Time zone (EDT), or nothing if no time zone is determinable.
380 @end table
381 
382 @noindent
383 Date fields:
384 
385 @table @code
386 @item %a
387 Locale's abbreviated weekday name (Sun-Sat).
388 
389 @item %A
390 Locale's full weekday name, variable length (Sunday-Saturday).
391 
392 @item %b
393 Locale's abbreviated month name (Jan-Dec).
394 
395 @item %B
396 Locale's full month name, variable length (January-December).
397 
398 @item %c
399 Locale's date and time (Sat Nov 04 12:02:33 EST 1989).
400 
401 @item %C
402 Century (00-99).
403 
404 @item %d
405 Day of month (01-31).
406 
407 @item %e
408 Day of month ( 1-31).
409 
410 @item %D
411 Date (mm/dd/yy).
412 
413 @item %h
414 Same as %b.
415 
416 @item %j
417 Day of year (001-366).
418 
419 @item %m
420 Month (01-12).
421 
422 @item %U
423 Week number of year with Sunday as first day of week (00-53).
424 
425 @item %w
426 Day of week (0-6).
427 
428 @item %W
429 Week number of year with Monday as first day of week (00-53).
430 
431 @item %x
432 Locale's date representation (mm/dd/yy).
433 
434 @item %y
435 Last two digits of year (00-99).
436 
437 @item %Y
438 Year (1970-).
439 @end table
440 @seealso{strptime, localtime, gmtime, mktime, time, now, date, clock, datenum, datestr, datevec, calendar, weekday}
441 @end deftypefn */)
442 {
443  if (args.length () != 2)
444  print_usage ();
445 
446  std::string fmt = args(0).xstring_value ("strftime: FMT must be a string");
447 
448  octave_scalar_map map = args(1).xscalar_map_value ("strftime: TM_STRUCT must be a structure");
449 
450  octave::sys::base_tm tm = extract_tm (map, "strftime");
451 
452  return ovl (tm.strftime (fmt));
453 }
454 
455 /*
456 %!assert (ischar (strftime ("%%%n%t%H%I%k%l", localtime (time ()))))
457 %!assert (ischar (strftime ("%M%p%r%R%s%S%T", localtime (time ()))))
458 %!assert (ischar (strftime ("%X%Z%z%a%A%b%B", localtime (time ()))))
459 %!assert (ischar (strftime ("%c%C%d%e%D%h%j", localtime (time ()))))
460 %!assert (ischar (strftime ("%m%U%w%W%x%y%Y", localtime (time ()))))
461 
462 %!error strftime ()
463 %!error strftime ("foo", 1)
464 %!error strftime ("foo", struct ("year", "foo"))
465 %!error strftime ("foo", localtime (time ()), 1)
466 */
467 
468 DEFUN (strptime, args, ,
469  doc: /* -*- texinfo -*-
470 @deftypefn {} {[@var{tm_struct}, @var{nchars}] =} strptime (@var{str}, @var{fmt})
471 Convert the string @var{str} to the time structure @var{tm_struct} under
472 the control of the format string @var{fmt}.
473 
474 If @var{fmt} fails to match, @var{nchars} is 0; otherwise, it is set to the
475 position of last matched character plus 1. Always check for this unless
476 you're absolutely sure the date string will be parsed correctly.
477 @seealso{strftime, localtime, gmtime, mktime, time, now, date, clock, datenum, datestr, datevec, calendar, weekday}
478 @end deftypefn */)
479 {
480  if (args.length () != 2)
481  print_usage ();
482 
483  std::string str = args(0).xstring_value ("strptime: argument STR must be a string");
484 
485  std::string fmt = args(1).xstring_value ("strptime: FMT must be a string");
486 
487  octave::sys::strptime t (str, fmt);
488 
489  return ovl (mk_tm_map (t), t.characters_converted ());
490 }
491 
492 /*
493 %!test
494 %! fmt = "%Y-%m-%d %H:%M:%S";
495 %! s = strftime (fmt, localtime (time ()));
496 %! ts = strptime (s, fmt);
497 %! assert (isstruct (ts));
498 %! assert (isfield (ts, "usec"));
499 %! assert (isfield (ts, "year"));
500 %! assert (isfield (ts, "mon"));
501 %! assert (isfield (ts, "mday"));
502 %! assert (isfield (ts, "sec"));
503 %! assert (isfield (ts, "min"));
504 %! assert (isfield (ts, "wday"));
505 %! assert (isfield (ts, "hour"));
506 %! assert (isfield (ts, "isdst"));
507 %! assert (isfield (ts, "yday"));
508 
509 %!error strptime ()
510 */
int hour(void) const
Definition: oct-time.h:229
bool isempty(void) const
Definition: ov.h:529
OCTINTERP_API void print_usage(void)
Definition: defun.cc:54
std::string xstring_value(const char *fmt,...) const
for large enough k
Definition: lu.cc:617
#define DEFUN(name, args_name, nargout_name, doc)
Macro to define a builtin function.
Definition: defun.h:53
int year(void) const
Definition: oct-time.h:232
OCTAVE_EXPORT octave_value_list return the number of command line arguments passed to Octave If called with the optional argument the function t
Definition: ov-usr-fcn.cc:997
int sec(void) const
Definition: oct-time.h:227
int xint_value(const char *fmt,...) const
std::string strftime(const std::string &fmt) const
Definition: oct-time.cc:161
std::string zone(void) const
Definition: oct-time.h:237
int yday(void) const
Definition: oct-time.h:234
int min(void) const
Definition: oct-time.h:228
static octave::sys::base_tm extract_tm(const octave_scalar_map &m, const char *who)
Definition: time.cc:86
std::string str
Definition: hash.cc:118
double tmp
Definition: data.cc:6252
octave_value retval
Definition: data.cc:6246
static int intfield(const octave_scalar_map &m, const std::string &k, const char *who)
Definition: time.cc:60
int mon(void) const
Definition: oct-time.h:231
octave_value getfield(const std::string &key) const
Definition: oct-map.cc:184
static octave_scalar_map mk_tm_map(const octave::sys::base_tm &t)
Definition: time.cc:39
OCTAVE_EXPORT octave_value_list isa nd deftypefn *return ovl(args(0).isinteger())
void assign(const std::string &k, const octave_value &val)
Definition: oct-map.h:227
octave_map map(dims)
int wday(void) const
Definition: oct-time.h:233
long gmtoff(void) const
Definition: oct-time.h:236
int usec(void) const
Definition: oct-time.h:226
static std::string stringfield(const octave_scalar_map &m, const std::string &k, const char *who)
Definition: time.cc:73
If this string is the system will ring the terminal sometimes it is useful to be able to print the original representation of the string
Definition: utils.cc:888
int mday(void) const
Definition: oct-time.h:230
int isdst(void) const
Definition: oct-time.h:235