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