GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
oct-time.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1999-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 (octave_oct_time_h)
24 #define octave_oct_time_h 1
25 
26 #include "octave-config.h"
27 
28 #include <ctime>
29 
30 #include <iosfwd>
31 #include <string>
32 
33 static inline double
34 as_double (time_t sec, long usec)
35 {
36  // Unix time will be exactly representable as a double for more than
37  // 100 million years, so no worry there, and microseconds has a
38  // range of 0-1e6, so we are safe there as well.
39 
40  return (static_cast<double> (sec) + static_cast<double> (usec) / 1.0e6);
41 }
42 
43 namespace octave
44 {
45  namespace sys
46  {
47  class base_tm;
48 
49  class
50  OCTAVE_API
51  time
52  {
53  public:
54 
55  time (void)
56  : ot_unix_time (0), ot_usec (0) { stamp (); }
57 
58  time (time_t t)
59  : ot_unix_time (t), ot_usec (0) { }
60 
61  time (time_t t, long us)
62  : ot_unix_time (t), ot_usec ()
63  {
64  long rem, extra;
65 
66  if (us >= 0)
67  {
68  rem = us % 1000000;
69  extra = (us - rem) / 1000000;
70  }
71  else
72  {
73  us = -us;
74  rem = us % 1000000;
75  extra = - (1 + (us - rem) / 1000000);
76  rem = 1000000 - us % 1000000;
77  }
78 
79  ot_usec = rem;
80  ot_unix_time += extra;
81  }
82 
83  time (double d);
84 
85  time (const base_tm& tm);
86 
87  time (const time& ot)
88  : ot_unix_time (ot.ot_unix_time), ot_usec (ot.ot_usec) { }
89 
90  time& operator = (const time& ot)
91  {
92  if (this != &ot)
93  {
94  ot_unix_time = ot.ot_unix_time;
95  ot_usec = ot.ot_usec;
96  }
97 
98  return *this;
99  }
100 
101  ~time (void) = default;
102 
103  void stamp (void);
104 
105  double double_value (void) const
106  {
107  return as_double (ot_unix_time, ot_usec);
108  }
109 
110  time_t unix_time (void) const { return ot_unix_time; }
111 
112  long usec (void) const { return ot_usec; }
113 
114  std::string ctime (void) const;
115 
116  friend std::ostream& operator << (std::ostream& os, const time& ot);
117 
118  private:
119 
120  // Seconds since the epoch.
121  time_t ot_unix_time;
122 
123  // Additional microseconds.
124  long ot_usec;
125  };
126 
127  inline bool
128  operator == (const time& t1, const time& t2)
129  {
130  return (t1.unix_time () == t2.unix_time () && t1.usec () == t2.usec ());
131  }
132 
133  inline bool
134  operator != (const time& t1, const time& t2)
135  {
136  return ! (t1 == t2);
137  }
138 
139  inline bool
140  operator < (const time& t1, const time& t2)
141  {
142  if (t1.unix_time () < t2.unix_time ())
143  return true;
144  else if (t1.unix_time () > t2.unix_time ())
145  return false;
146  else if (t1.usec () < t2.usec ())
147  return true;
148  else
149  return false;
150  }
151 
152  inline bool
153  operator <= (const time& t1, const time& t2)
154  {
155  return (t1 < t2 || t1 == t2);
156  }
157 
158  inline bool
159  operator > (const time& t1, const time& t2)
160  {
161  if (t1.unix_time () > t2.unix_time ())
162  return true;
163  else if (t1.unix_time () < t2.unix_time ())
164  return false;
165  else if (t1.usec () > t2.usec ())
166  return true;
167  else
168  return false;
169  }
170 
171  inline bool
172  operator >= (const time& t1, const time& t2)
173  {
174  return (t1 > t2 || t1 == t2);
175  }
176 
177  inline time
178  operator + (const time& t1, const time& t2)
179  {
180  return time (t1.unix_time () + t2.unix_time (),
181  t1.usec () + t2.usec ());
182  }
183 
184  class
185  OCTAVE_API
186  base_tm
187  {
188  public:
189 
190  base_tm (void)
191  : m_usec (0), m_sec (0), m_min (0), m_hour (0),
192  m_mday (0), m_mon (0), m_year (0), m_wday (0),
193  m_yday (0), m_isdst (0), m_gmtoff (0), m_zone ("unknown")
194  { }
195 
196  base_tm (const base_tm& tm)
197  : m_usec (tm.m_usec), m_sec (tm.m_sec), m_min (tm.m_min),
198  m_hour (tm.m_hour), m_mday (tm.m_mday), m_mon (tm.m_mon),
199  m_year (tm.m_year), m_wday (tm.m_wday), m_yday (tm.m_yday),
200  m_isdst (tm.m_isdst), m_gmtoff (tm.m_gmtoff), m_zone (tm.m_zone)
201  { }
202 
203  base_tm& operator = (const base_tm& tm)
204  {
205  if (this != &tm)
206  {
207  m_usec = tm.m_usec;
208  m_sec = tm.m_sec;
209  m_min = tm.m_min;
210  m_hour = tm.m_hour;
211  m_mday = tm.m_mday;
212  m_mon = tm.m_mon;
213  m_year = tm.m_year;
214  m_wday = tm.m_wday;
215  m_yday = tm.m_yday;
216  m_isdst = tm.m_isdst;
217  m_gmtoff = tm.m_gmtoff;
218  m_zone = tm.m_zone;
219  }
220 
221  return *this;
222  }
223 
224  virtual ~base_tm (void) = default;
225 
226  int usec (void) const { return m_usec; }
227  int sec (void) const { return m_sec; }
228  int min (void) const { return m_min; }
229  int hour (void) const { return m_hour; }
230  int mday (void) const { return m_mday; }
231  int mon (void) const { return m_mon; }
232  int year (void) const { return m_year; }
233  int wday (void) const { return m_wday; }
234  int yday (void) const { return m_yday; }
235  int isdst (void) const { return m_isdst; }
236  long gmtoff (void) const { return m_gmtoff; }
237  std::string zone (void) const { return m_zone; }
238 
239  base_tm& usec (int v);
240  base_tm& sec (int v);
241  base_tm& min (int v);
242  base_tm& hour (int v);
243  base_tm& mday (int v);
244  base_tm& mon (int v);
245  base_tm& year (int v);
246  base_tm& wday (int v);
247  base_tm& yday (int v);
248  base_tm& isdst (int v);
249  base_tm& gmtoff (long v);
250  base_tm& zone (const std::string& s);
251 
252  std::string strftime (const std::string& fmt) const;
253 
254  std::string asctime (void) const
255  { return strftime ("%a %b %d %H:%M:%S %Y\n"); }
256 
257  protected:
258 
259  // Microseconds after the second (0, 999999).
260  int m_usec;
261 
262  // Seconds after the minute (0, 61).
263  int m_sec;
264 
265  // Minutes after the hour (0, 59).
266  int m_min;
267 
268  // Hours since midnight (0, 23).
269  int m_hour;
270 
271  // Day of the month (1, 31).
272  int m_mday;
273 
274  // Months since January (0, 11).
275  int m_mon;
276 
277  // Years since 1900.
278  int m_year;
279 
280  // Days since Sunday (0, 6).
281  int m_wday;
282 
283  // Days since January 1 (0, 365).
284  int m_yday;
285 
286  // Daylight saving time flag.
287  int m_isdst;
288 
289  // Time zone.
290  long m_gmtoff;
291 
292  // Time zone.
294 
295  void init (void *p);
296  };
297 
298  class
299  OCTAVE_API
300  localtime : public base_tm
301  {
302  public:
303 
304  localtime (void)
305  : base_tm () { init (time ()); }
306 
307  localtime (const time& ot)
308  : base_tm () { init (ot); }
309 
311  : base_tm (t) { }
312 
313  localtime& operator = (const localtime& t)
314  {
316  return *this;
317  }
318 
319  ~localtime (void) = default;
320 
321  private:
322 
323  void init (const time& ot);
324  };
325 
326  class
327  OCTAVE_API
328  gmtime : public base_tm
329  {
330  public:
331 
332  gmtime (void)
333  : base_tm () { init (time ()); }
334 
335  gmtime (const time& ot)
336  : base_tm () { init (ot); }
337 
338  gmtime& operator = (const gmtime& t)
339  {
341  return *this;
342  }
343 
344  ~gmtime (void) = default;
345 
346  private:
347 
348  void init (const time& ot);
349  };
350 
351  class
352  OCTAVE_API
353  strptime : public base_tm
354  {
355  public:
356 
357  strptime (const std::string& str, const std::string& fmt)
358  : base_tm (), nchars (0)
359  {
360  init (str, fmt);
361  }
362 
363  strptime (const strptime& s)
364  : base_tm (s), nchars (s.nchars) { }
365 
366  strptime& operator = (const strptime& s)
367  {
369  nchars = s.nchars;
370  return *this;
371  }
372 
373  int characters_converted (void) const { return nchars; }
374 
375  ~strptime (void) = default;
376 
377  private:
378 
379  int nchars;
380 
381  void init (const std::string& str, const std::string& fmt);
382  };
383 
384  class
385  OCTAVE_API
386  cpu_time
387  {
388  public:
389 
390  friend class resource_usage;
391 
392  cpu_time (void)
393  : m_usr_sec (0), m_sys_sec (0), m_usr_usec (0), m_sys_usec (0)
394  {
395  stamp ();
396  }
397 
398  cpu_time (const cpu_time& tm)
399  : m_usr_sec (tm.m_usr_sec), m_sys_sec (tm.m_sys_sec),
400  m_usr_usec (tm.m_usr_usec), m_sys_usec (tm.m_sys_usec)
401  { }
402 
403  cpu_time& operator = (const cpu_time& tm)
404  {
405  if (&tm != this)
406  {
407  m_usr_sec = tm.m_usr_sec;
408  m_sys_sec = tm.m_sys_sec;
409  m_usr_usec = tm.m_usr_usec;
410  m_sys_usec = tm.m_sys_usec;
411  }
412 
413  return *this;
414  }
415 
416  void stamp (void);
417 
418  double user (void) const
419  {
420  return as_double (m_usr_sec, m_usr_usec);
421  }
422 
423  double system (void) const
424  {
425  return as_double (m_sys_sec, m_sys_usec);
426  }
427 
428  time_t user_sec (void) const { return m_usr_sec; }
429  long user_usec (void) const { return m_usr_usec; }
430 
431  time_t system_sec (void) const { return m_sys_sec; }
432  long system_usec (void) const { return m_sys_usec; }
433 
434  private:
435 
436  time_t m_usr_sec;
437  time_t m_sys_sec;
438 
441 
442  cpu_time (time_t usr_sec, time_t sys_sec, long usr_usec, long sys_usec)
443  : m_usr_sec (usr_sec), m_sys_sec (sys_sec),
444  m_usr_usec (usr_usec), m_sys_usec (sys_usec)
445  { }
446  };
447 
448  class
450  {
451  public:
452 
454  : m_cpu (), m_maxrss (0), m_ixrss (0), m_idrss (0),
455  m_isrss (0), m_minflt (0), m_majflt (0), m_nswap (0),
456  m_inblock (0), m_oublock (0), m_msgsnd (0), m_msgrcv (0),
457  m_nsignals (0), m_nvcsw (0), m_nivcsw (0)
458  {
459  stamp ();
460  }
461 
463  : m_cpu (ru.m_cpu), m_maxrss (ru.m_maxrss),
464  m_ixrss (ru.m_ixrss), m_idrss (ru.m_idrss),
465  m_isrss (ru.m_isrss), m_minflt (ru.m_minflt),
466  m_majflt (ru.m_majflt), m_nswap (ru.m_nswap),
467  m_inblock (ru.m_inblock), m_oublock (ru.m_oublock),
468  m_msgsnd (ru.m_msgsnd), m_msgrcv (ru.m_msgrcv),
469  m_nsignals (ru.m_nsignals), m_nvcsw (ru.m_nvcsw),
470  m_nivcsw (ru.m_nivcsw)
471  { }
472 
473  resource_usage& operator = (const resource_usage& ru)
474  {
475  if (&ru != this)
476  {
477  m_cpu = ru.m_cpu;
478 
479  m_maxrss = ru.m_maxrss;
480  m_ixrss = ru.m_ixrss;
481  m_idrss = ru.m_idrss;
482  m_isrss = ru.m_isrss;
483  m_minflt = ru.m_minflt;
484  m_majflt = ru.m_majflt;
485  m_nswap = ru.m_nswap;
486  m_inblock = ru.m_inblock;
487  m_oublock = ru.m_oublock;
488  m_msgsnd = ru.m_msgsnd;
489  m_msgrcv = ru.m_msgrcv;
490  m_nsignals = ru.m_nsignals;
491  m_nvcsw = ru.m_nvcsw;
492  m_nivcsw = ru.m_nivcsw;
493  }
494 
495  return *this;
496  }
497 
498  void stamp (void);
499 
500  cpu_time cpu (void) const { return m_cpu; }
501 
502  long maxrss (void) const { return m_maxrss; }
503  long ixrss (void) const { return m_ixrss; }
504  long idrss (void) const { return m_idrss; }
505  long isrss (void) const { return m_isrss; }
506  long minflt (void) const { return m_minflt; }
507  long majflt (void) const { return m_majflt; }
508  long nswap (void) const { return m_nswap; }
509  long inblock (void) const { return m_inblock; }
510  long oublock (void) const { return m_oublock; }
511  long msgsnd (void) const { return m_msgsnd; }
512  long msgrcv (void) const { return m_msgrcv; }
513  long nsignals (void) const { return m_nsignals; }
514  long nvcsw (void) const { return m_nvcsw; }
515  long nivcsw (void) const { return m_nivcsw; }
516 
517  private:
518 
520 
521  long m_maxrss;
522  long m_ixrss;
523  long m_idrss;
524  long m_isrss;
525  long m_minflt;
526  long m_majflt;
527  long m_nswap;
528  long m_inblock;
529  long m_oublock;
530  long m_msgsnd;
531  long m_msgrcv;
533  long m_nvcsw;
534  long m_nivcsw;
535  };
536  }
537 }
538 
539 #if defined (OCTAVE_USE_DEPRECATED_FUNCTIONS)
540 
541 OCTAVE_DEPRECATED (4.2, "use 'octave::sys::time' instead")
542 typedef octave::sys::time octave_time;
543 
544 OCTAVE_DEPRECATED (4.2, "use 'octave::sys::base_tm' instead")
545 typedef octave::sys::base_tm octave_base_tm;
546 
547 OCTAVE_DEPRECATED (4.2, "use 'octave::sys::localtime' instead")
548 typedef octave::sys::localtime octave_localtime;
549 
550 OCTAVE_DEPRECATED (4.2, "use 'octave::sys::gmtime' instead")
551 typedef octave::sys::gmtime octave_gmtime;
552 
553 OCTAVE_DEPRECATED (4.2, "use 'octave::sys::strptime' instead")
554 typedef octave::sys::strptime octave_strptime;
555 
556 #endif
557 
558 #endif
for fields that display a single padding can be changed or inhibited by following the hour(hh:mm:ss [AP]M). tem %R Time
std::ostream & operator<<(std::ostream &os, const time &ot)
Definition: oct-time.cc:95
std::string asctime(void) const
Definition: oct-time.h:254
cpu_time(const cpu_time &tm)
Definition: oct-time.h:398
bool operator!=(const time &t1, const time &t2)
Definition: oct-time.h:134
long oublock(void) const
Definition: oct-time.h:510
bool operator<=(const time &t1, const time &t2)
Definition: oct-time.h:153
int hour(void) const
Definition: oct-time.h:229
long user_usec(void) const
Definition: oct-time.h:429
long system_usec(void) const
Definition: oct-time.h:432
time(time_t t)
Definition: oct-time.h:58
long idrss(void) const
Definition: oct-time.h:504
gmtime(const time &ot)
Definition: oct-time.h:335
time_t ot_unix_time
Definition: oct-time.h:121
bool operator>=(const time &t1, const time &t2)
Definition: oct-time.h:172
cpu_time(time_t usr_sec, time_t sys_sec, long usr_usec, long sys_usec)
Definition: oct-time.h:442
bool operator==(const time &t1, const time &t2)
Definition: oct-time.h:128
int year(void) const
Definition: oct-time.h:232
long ixrss(void) const
Definition: oct-time.h:503
base_tm(const base_tm &tm)
Definition: oct-time.h:196
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
time(time_t t, long us)
Definition: oct-time.h:61
long isrss(void) const
Definition: oct-time.h:505
s
Definition: file-io.cc:2729
int sec(void) const
Definition: oct-time.h:227
long inblock(void) const
Definition: oct-time.h:509
F77_RET_T const F77_REAL const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
static double as_double(time_t sec, long usec)
Definition: oct-time.h:34
long usec(void) const
Definition: oct-time.h:112
bool operator>(const time &t1, const time &t2)
Definition: oct-time.h:159
long msgrcv(void) const
Definition: oct-time.h:512
localtime(const time &ot)
Definition: oct-time.h:307
double double_value(void) const
Definition: oct-time.h:105
std::string zone(void) const
Definition: oct-time.h:237
int yday(void) const
Definition: oct-time.h:234
time_t user_sec(void) const
Definition: oct-time.h:428
int min(void) const
Definition: oct-time.h:228
int characters_converted(void) const
Definition: oct-time.h:373
resource_usage(const resource_usage &ru)
Definition: oct-time.h:462
long majflt(void) const
Definition: oct-time.h:507
std::string str
Definition: hash.cc:118
double user(void) const
Definition: oct-time.h:418
long nivcsw(void) const
Definition: oct-time.h:515
int mon(void) const
Definition: oct-time.h:231
base_tm & operator=(const base_tm &tm)
Definition: oct-time.h:203
time_t unix_time(void) const
Definition: oct-time.h:110
time_t system_sec(void) const
Definition: oct-time.h:431
strptime(const std::string &str, const std::string &fmt)
Definition: oct-time.h:357
bool operator<(const time &t1, const time &t2)
Definition: oct-time.h:140
time operator+(const time &t1, const time &t2)
Definition: oct-time.h:178
long msgsnd(void) const
Definition: oct-time.h:511
long maxrss(void) const
Definition: oct-time.h:502
long minflt(void) const
Definition: oct-time.h:506
p
Definition: lu.cc:138
localtime(const localtime &t)
Definition: oct-time.h:310
octave_int< T > rem(const octave_int< T > &x, const octave_int< T > &y)
Definition: oct-inttypes.h:853
int wday(void) const
Definition: oct-time.h:233
cpu_time cpu(void) const
Definition: oct-time.h:500
double system(void) const
Definition: oct-time.h:423
long gmtoff(void) const
Definition: oct-time.h:236
int usec(void) const
Definition: oct-time.h:226
long nsignals(void) const
Definition: oct-time.h:513
strptime(const strptime &s)
Definition: oct-time.h:363
time(const time &ot)
Definition: oct-time.h:87
std::string m_zone
Definition: oct-time.h:293
long nvcsw(void) const
Definition: oct-time.h:514
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
long nswap(void) const
Definition: oct-time.h:508
int mday(void) const
Definition: oct-time.h:230
octave::stream os
Definition: file-io.cc:627
int isdst(void) const
Definition: oct-time.h:235
charNDArray min(char d, const charNDArray &m)
Definition: chNDArray.cc:204