GNU Octave  9.1.0
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-2024 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if ! defined (octave_oct_time_h)
27 #define octave_oct_time_h 1
28 
29 #include "octave-config.h"
30 
31 #include <iosfwd>
32 #include <string>
33 
34 #if defined (OCTAVE_USE_WINDOWS_API)
35 // Some Windows headers must be included in a certain order.
36 // Don't include "windows.h" here to avoid potential issues due to that.
37 // Instead just define the one type we need for the interface of one function.
38 struct OCTAVE_WIN_FILETIME
39 {
40  uint32_t dwLowDateTime;
41  uint32_t dwHighDateTime;
42 };
43 #endif
44 
45 
46 static inline double
47 as_double (OCTAVE_TIME_T sec, long usec)
48 {
49  // Unix time will be exactly representable as a double for more than
50  // 100 million years, so no worry there, and microseconds has a
51  // range of 0-1e6, so we are safe there as well.
52 
53  return (static_cast<double> (sec) + static_cast<double> (usec) / 1.0e6);
54 }
55 
57 
59 
60 class base_tm;
61 
62 class
63 time
64 {
65 public:
66 
67  time ()
68  : m_ot_unix_time (0), m_ot_usec (0) { stamp (); }
69 
70  time (OCTAVE_TIME_T t)
71  : m_ot_unix_time (t), m_ot_usec (0) { }
72 
73  time (OCTAVE_TIME_T t, long us)
74  : m_ot_unix_time (t), m_ot_usec ()
75  {
76  long rem, extra;
77 
78  if (us >= 0)
79  {
80  rem = us % 1000000;
81  extra = (us - rem) / 1000000;
82  }
83  else
84  {
85  us = -us;
86  rem = us % 1000000;
87  extra = - (1 + (us - rem) / 1000000);
88  rem = 1000000 - us % 1000000;
89  }
90 
91  m_ot_usec = rem;
92  m_ot_unix_time += extra;
93  }
94 
95  OCTAVE_API time (double d);
96 
97  OCTAVE_API time (const base_tm& tm);
98 
99  time (const time& ot)
100  : m_ot_unix_time (ot.m_ot_unix_time), m_ot_usec (ot.m_ot_usec) { }
101 
102  time& operator = (const time& ot)
103  {
104  if (this != &ot)
105  {
106  m_ot_unix_time = ot.m_ot_unix_time;
107  m_ot_usec = ot.m_ot_usec;
108  }
109 
110  return *this;
111  }
112 
113  ~time () = default;
114 
115  OCTAVE_API void stamp ();
116 
117  double double_value () const
118  {
119  return as_double (m_ot_unix_time, m_ot_usec);
120  }
121 
122  OCTAVE_TIME_T unix_time () const { return m_ot_unix_time; }
123 
124  long usec () const { return m_ot_usec; }
125 
126  OCTAVE_API std::string ctime () const;
127 
128  friend OCTAVE_API std::ostream& operator << (std::ostream& os, const time& ot);
129 
130 private:
131 
132  // Seconds since the epoch.
133  OCTAVE_TIME_T m_ot_unix_time;
134 
135  // Additional microseconds.
136  long m_ot_usec;
137 };
138 
139 inline bool
140 operator == (const time& t1, const time& t2)
141 {
142  return (t1.unix_time () == t2.unix_time () && t1.usec () == t2.usec ());
143 }
144 
145 inline bool
146 operator != (const time& t1, const time& t2)
147 {
148  return ! (t1 == t2);
149 }
150 
151 inline bool
152 operator < (const time& t1, const time& t2)
153 {
154  if (t1.unix_time () < t2.unix_time ())
155  return true;
156  else if (t1.unix_time () > t2.unix_time ())
157  return false;
158  else if (t1.usec () < t2.usec ())
159  return true;
160  else
161  return false;
162 }
163 
164 inline bool
165 operator <= (const time& t1, const time& t2)
166 {
167  return (t1 < t2 || t1 == t2);
168 }
169 
170 inline bool
171 operator > (const time& t1, const time& t2)
172 {
173  if (t1.unix_time () > t2.unix_time ())
174  return true;
175  else if (t1.unix_time () < t2.unix_time ())
176  return false;
177  else if (t1.usec () > t2.usec ())
178  return true;
179  else
180  return false;
181 }
182 
183 inline bool
184 operator >= (const time& t1, const time& t2)
185 {
186  return (t1 > t2 || t1 == t2);
187 }
188 
189 inline time
190 operator + (const time& t1, const time& t2)
191 {
192  return time (t1.unix_time () + t2.unix_time (),
193  t1.usec () + t2.usec ());
194 }
195 
196 class
197 base_tm
198 {
199 public:
200 
202  : m_usec (0), m_sec (0), m_min (0), m_hour (0),
203  m_mday (0), m_mon (0), m_year (0), m_wday (0),
204  m_yday (0), m_isdst (0), m_gmtoff (0), m_zone ("unknown")
205  { }
206 
207  base_tm (const base_tm& tm)
208  : m_usec (tm.m_usec), m_sec (tm.m_sec), m_min (tm.m_min),
209  m_hour (tm.m_hour), m_mday (tm.m_mday), m_mon (tm.m_mon),
210  m_year (tm.m_year), m_wday (tm.m_wday), m_yday (tm.m_yday),
211  m_isdst (tm.m_isdst), m_gmtoff (tm.m_gmtoff), m_zone (tm.m_zone)
212  { }
213 
215  {
216  if (this != &tm)
217  {
218  m_usec = tm.m_usec;
219  m_sec = tm.m_sec;
220  m_min = tm.m_min;
221  m_hour = tm.m_hour;
222  m_mday = tm.m_mday;
223  m_mon = tm.m_mon;
224  m_year = tm.m_year;
225  m_wday = tm.m_wday;
226  m_yday = tm.m_yday;
227  m_isdst = tm.m_isdst;
228  m_gmtoff = tm.m_gmtoff;
229  m_zone = tm.m_zone;
230  }
231 
232  return *this;
233  }
234 
235  virtual ~base_tm () = default;
236 
237  int usec () const { return m_usec; }
238  int sec () const { return m_sec; }
239  int min () const { return m_min; }
240  int hour () const { return m_hour; }
241  int mday () const { return m_mday; }
242  int mon () const { return m_mon; }
243  int year () const { return m_year; }
244  int wday () const { return m_wday; }
245  int yday () const { return m_yday; }
246  int isdst () const { return m_isdst; }
247  long gmtoff () const { return m_gmtoff; }
248  std::string zone () const { return m_zone; }
249 
250  OCTAVE_API base_tm& usec (int v);
251  OCTAVE_API base_tm& sec (int v);
252  OCTAVE_API base_tm& min (int v);
253  OCTAVE_API base_tm& hour (int v);
254  OCTAVE_API base_tm& mday (int v);
255  OCTAVE_API base_tm& mon (int v);
256  OCTAVE_API base_tm& year (int v);
257  OCTAVE_API base_tm& wday (int v);
258  OCTAVE_API base_tm& yday (int v);
259  OCTAVE_API base_tm& isdst (int v);
260  OCTAVE_API base_tm& gmtoff (long v);
261  OCTAVE_API base_tm& zone (const std::string& s);
262 
263  OCTAVE_API std::string strftime (const std::string& fmt) const;
264 
265  std::string asctime () const
266  { return strftime ("%a %b %d %H:%M:%S %Y\n"); }
267 
268 protected:
269 
270  // Microseconds after the second (0, 999999).
271  int m_usec;
272 
273  // Seconds after the minute (0, 61).
274  int m_sec;
275 
276  // Minutes after the hour (0, 59).
277  int m_min;
278 
279  // Hours since midnight (0, 23).
280  int m_hour;
281 
282  // Day of the month (1, 31).
283  int m_mday;
284 
285  // Months since January (0, 11).
286  int m_mon;
287 
288  // Years since 1900.
289  int m_year;
290 
291  // Days since Sunday (0, 6).
292  int m_wday;
293 
294  // Days since January 1 (0, 365).
295  int m_yday;
296 
297  // Daylight saving time flag.
298  int m_isdst;
299 
300  // Time zone.
301  long m_gmtoff;
302 
303  // Time zone.
304  std::string m_zone;
305 
306  OCTAVE_API void init (void *p);
307 };
308 
309 class
310 localtime : public base_tm
311 {
312 public:
313 
315  : base_tm () { init (time ()); }
316 
317  localtime (const time& ot)
318  : base_tm () { init (ot); }
319 
320  localtime (const localtime& t)
321  : base_tm (t) { }
322 
324  {
326  return *this;
327  }
328 
329  ~localtime () = default;
330 
331 private:
332 
333  OCTAVE_API void init (const time& ot);
334 };
335 
336 class
337 gmtime : public base_tm
338 {
339 public:
340 
342  : base_tm () { init (time ()); }
343 
344  gmtime (const time& ot)
345  : base_tm () { init (ot); }
346 
347  OCTAVE_DEFAULT_COPY_MOVE_DELETE (gmtime)
348 
349 private:
350 
351  OCTAVE_API void init (const time& ot);
352 };
353 
354 class
355 strptime : public base_tm
356 {
357 public:
358 
359  strptime () = delete;
360 
361  strptime (const std::string& str, const std::string& fmt)
362  : base_tm (), m_nchars (0)
363  {
364  init (str, fmt);
365  }
366 
367  OCTAVE_DEFAULT_COPY_MOVE_DELETE (strptime)
368 
369  int characters_converted () const { return m_nchars; }
370 
371 private:
372 
373  int m_nchars;
374 
375  OCTAVE_API void init (const std::string& str, const std::string& fmt);
376 };
377 
378 class
379 cpu_time
380 {
381 public:
382 
383  friend class resource_usage;
384 
386  : m_usr_sec (0), m_sys_sec (0), m_usr_usec (0), m_sys_usec (0)
387  {
388  stamp ();
389  }
390 
391  OCTAVE_DEFAULT_COPY_MOVE_DELETE (cpu_time)
392 
393  OCTAVE_API void stamp ();
394 
395  double user () const
396  {
397  return as_double (m_usr_sec, m_usr_usec);
398  }
399 
400  double system () const
401  {
402  return as_double (m_sys_sec, m_sys_usec);
403  }
404 
405  OCTAVE_TIME_T user_sec () const { return m_usr_sec; }
406  long user_usec () const { return m_usr_usec; }
407 
408  OCTAVE_TIME_T system_sec () const { return m_sys_sec; }
409  long system_usec () const { return m_sys_usec; }
410 
411 private:
412 
413  OCTAVE_TIME_T m_usr_sec;
414  OCTAVE_TIME_T m_sys_sec;
415 
416  long m_usr_usec;
417  long m_sys_usec;
418 
419  cpu_time (OCTAVE_TIME_T usr_sec, OCTAVE_TIME_T sys_sec, long usr_usec, long sys_usec)
420  : m_usr_sec (usr_sec), m_sys_sec (sys_sec),
421  m_usr_usec (usr_usec), m_sys_usec (sys_usec)
422  { }
423 };
424 
425 class
427 {
428 public:
429 
431  : m_cpu (), m_maxrss (0), m_ixrss (0), m_idrss (0),
432  m_isrss (0), m_minflt (0), m_majflt (0), m_nswap (0),
433  m_inblock (0), m_oublock (0), m_msgsnd (0), m_msgrcv (0),
434  m_nsignals (0), m_nvcsw (0), m_nivcsw (0)
435  {
436  stamp ();
437  }
438 
439  OCTAVE_DEFAULT_COPY_MOVE_DELETE (resource_usage)
440 
441  OCTAVE_API void stamp ();
442 
443  cpu_time cpu () const { return m_cpu; }
444 
445  long maxrss () const { return m_maxrss; }
446  long ixrss () const { return m_ixrss; }
447  long idrss () const { return m_idrss; }
448  long isrss () const { return m_isrss; }
449  long minflt () const { return m_minflt; }
450  long majflt () const { return m_majflt; }
451  long nswap () const { return m_nswap; }
452  long inblock () const { return m_inblock; }
453  long oublock () const { return m_oublock; }
454  long msgsnd () const { return m_msgsnd; }
455  long msgrcv () const { return m_msgrcv; }
456  long nsignals () const { return m_nsignals; }
457  long nvcsw () const { return m_nvcsw; }
458  long nivcsw () const { return m_nivcsw; }
459 
460 private:
461 
462  cpu_time m_cpu;
463 
464  long m_maxrss;
465  long m_ixrss;
466  long m_idrss;
467  long m_isrss;
468  long m_minflt;
469  long m_majflt;
470  long m_nswap;
471  long m_inblock;
472  long m_oublock;
473  long m_msgsnd;
474  long m_msgrcv;
475  long m_nsignals;
476  long m_nvcsw;
477  long m_nivcsw;
478 };
479 
480 // class to handle file time efficiently on different platforms
481 
483 {
484 public:
485 
486  file_time ();
487 
488  file_time (OCTAVE_TIME_T t)
489  : m_time (t)
490  { }
491 
492 #if defined (OCTAVE_USE_WINDOWS_API)
493  file_time (OCTAVE_WIN_FILETIME& t)
494  {
495  m_time = (static_cast<OCTAVE_TIME_T> (t.dwHighDateTime)) >> 32
496  | t.dwLowDateTime;
497  }
498 #endif
499 
500  file_time (const std::string& filename);
501 
502  file_time (const file_time& ot)
503  {
504  m_time = ot.time ();
505  }
506 
507  file_time& operator = (const file_time& ot)
508  {
509  if (this != &ot)
510  m_time = ot.time ();
511 
512  return *this;
513  }
514 
515  ~file_time () = default;
516 
517  inline static file_time time_resolution ()
518  {
519 #if defined (OCTAVE_USE_WINDOWS_API)
520  // FAT file systems have 2 seconds resolution for the modification time.
521  static OCTAVE_TIME_T time_resolution = 20000;
522 #else
523  // Assume 1 second (see file_stat)
524  static OCTAVE_TIME_T time_resolution = 1;
525 #endif
526  return time_resolution;
527  }
528 
529  inline bool
530  operator == (const file_time& t2) const
531  {
532  return time () == t2.time ();
533  }
534 
535  inline bool
536  operator != (const file_time& t2) const
537  {
538  return ! (*this == t2);
539  }
540 
541  inline bool
542  operator < (const file_time& t2) const
543  {
544  return time () < t2.time ();
545  }
546 
547  inline bool
548  operator <= (const file_time& t2) const
549  {
550  return (*this < t2 || *this == t2);
551  }
552 
553  inline bool
554  operator > (const file_time& t2) const
555  {
556  return time () > t2.time ();
557  }
558 
559  inline bool
560  operator >= (const file_time& t2) const
561  {
562  return (*this > t2 || *this == t2);
563  }
564 
565  inline file_time
566  operator + (const file_time& t2) const
567  {
568  return file_time (time () + t2.time ());
569  }
570 
571  inline file_time
572  operator + (const OCTAVE_TIME_T t2) const
573  {
574  return file_time (time () + t2);
575  }
576 
577  OCTAVE_TIME_T time () const { return m_time; }
578 
579 private:
580 
581  // The native file time type differs per platform.
582  // On POSIX, this is the number of 1 second intervals since the epoch.
583  // On Windows, this is the number of 0.1 ms intervals since a different epoch.
584  OCTAVE_TIME_T m_time;
585 };
586 
587 OCTAVE_END_NAMESPACE(sys)
588 OCTAVE_END_NAMESPACE(octave)
589 
590 #endif
template std::ostream & operator<<(std::ostream &, const Array< bool > &)
charNDArray min(char d, const charNDArray &m)
Definition: chNDArray.cc:207
int mday() const
Definition: oct-time.h:241
int yday() const
Definition: oct-time.h:245
int m_sec
Definition: oct-time.h:274
int m_isdst
Definition: oct-time.h:298
int m_year
Definition: oct-time.h:289
int hour() const
Definition: oct-time.h:240
int m_usec
Definition: oct-time.h:271
int m_mday
Definition: oct-time.h:283
int wday() const
Definition: oct-time.h:244
int m_min
Definition: oct-time.h:277
std::string m_zone
Definition: oct-time.h:304
int mon() const
Definition: oct-time.h:242
void init(void *p)
Definition: oct-time.cc:231
int min() const
Definition: oct-time.h:239
std::string asctime() const
Definition: oct-time.h:265
long m_gmtoff
Definition: oct-time.h:301
int isdst() const
Definition: oct-time.h:246
int m_hour
Definition: oct-time.h:280
int sec() const
Definition: oct-time.h:238
long gmtoff() const
Definition: oct-time.h:247
base_tm & operator=(const base_tm &tm)
Definition: oct-time.h:214
virtual ~base_tm()=default
std::string zone() const
Definition: oct-time.h:248
int usec() const
Definition: oct-time.h:237
int m_wday
Definition: oct-time.h:292
int year() const
Definition: oct-time.h:243
std::string strftime(const std::string &fmt) const
Definition: oct-time.cc:173
base_tm(const base_tm &tm)
Definition: oct-time.h:207
int m_yday
Definition: oct-time.h:295
int m_mon
Definition: oct-time.h:286
base_tm()
Definition: oct-time.h:201
cpu_time()
Definition: oct-time.h:385
double system() const
Definition: oct-time.h:400
long user_usec() const
Definition: oct-time.h:406
double user() const
Definition: oct-time.h:395
OCTAVE_TIME_T user_sec() const
Definition: oct-time.h:405
OCTAVE_TIME_T system_sec() const
Definition: oct-time.h:408
long system_usec() const
Definition: oct-time.h:409
static file_time time_resolution()
Definition: oct-time.h:517
OCTAVE_TIME_T time() const
Definition: oct-time.h:577
file_time(const file_time &ot)
Definition: oct-time.h:502
~file_time()=default
file_time(OCTAVE_TIME_T t)
Definition: oct-time.h:488
gmtime(const time &ot)
Definition: oct-time.h:344
gmtime()
Definition: oct-time.h:341
localtime(const localtime &t)
Definition: oct-time.h:320
localtime(const time &ot)
Definition: oct-time.h:317
localtime()
Definition: oct-time.h:314
~localtime()=default
long maxrss() const
Definition: oct-time.h:445
long nvcsw() const
Definition: oct-time.h:457
long msgsnd() const
Definition: oct-time.h:454
long nsignals() const
Definition: oct-time.h:456
cpu_time cpu() const
Definition: oct-time.h:443
void stamp()
Definition: oct-time.cc:360
long minflt() const
Definition: oct-time.h:449
long inblock() const
Definition: oct-time.h:452
long idrss() const
Definition: oct-time.h:447
long nivcsw() const
Definition: oct-time.h:458
long isrss() const
Definition: oct-time.h:448
long ixrss() const
Definition: oct-time.h:446
long oublock() const
Definition: oct-time.h:453
long majflt() const
Definition: oct-time.h:450
long nswap() const
Definition: oct-time.h:451
long msgrcv() const
Definition: oct-time.h:455
strptime()=delete
strptime(const std::string &str, const std::string &fmt)
Definition: oct-time.h:361
Definition: oct-time.h:64
time()
Definition: oct-time.h:67
time(OCTAVE_TIME_T t, long us)
Definition: oct-time.h:73
~time()=default
long usec() const
Definition: oct-time.h:124
time(const time &ot)
Definition: oct-time.h:99
OCTAVE_TIME_T unix_time() const
Definition: oct-time.h:122
double double_value() const
Definition: oct-time.h:117
time(OCTAVE_TIME_T t)
Definition: oct-time.h:70
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
T rem(T x, T y)
Definition: lo-mappers.h:327
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
#define OCTAVE_API
Definition: main.cc:55
bool operator!=(const time &t1, const time &t2)
Definition: oct-time.h:146
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:190
bool operator>=(const time &t1, const time &t2)
Definition: oct-time.h:184
bool operator<(const time &t1, const time &t2)
Definition: oct-time.h:152
bool operator<=(const time &t1, const time &t2)
Definition: oct-time.h:165
bool operator>(const time &t1, const time &t2)
Definition: oct-time.h:171