GNU Octave 7.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-2022 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
34static inline double
35as_double (OCTAVE_TIME_T sec, long usec)
36{
37 // Unix time will be exactly representable as a double for more than
38 // 100 million years, so no worry there, and microseconds has a
39 // range of 0-1e6, so we are safe there as well.
40
41 return (static_cast<double> (sec) + static_cast<double> (usec) / 1.0e6);
42}
43
44namespace octave
45{
46 namespace sys
47 {
48 class base_tm;
49
50 class
51 time
52 {
53 public:
54
55 time (void)
56 : m_ot_unix_time (0), m_ot_usec (0) { stamp (); }
57
58 time (OCTAVE_TIME_T t)
59 : m_ot_unix_time (t), m_ot_usec (0) { }
60
61 time (OCTAVE_TIME_T t, long us)
62 : m_ot_unix_time (t), m_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 m_ot_usec = rem;
80 m_ot_unix_time += extra;
81 }
82
83 OCTAVE_API time (double d);
84
85 OCTAVE_API time (const base_tm& tm);
86
87 time (const time& ot)
88 : m_ot_unix_time (ot.m_ot_unix_time), m_ot_usec (ot.m_ot_usec) { }
89
90 time& operator = (const time& ot)
91 {
92 if (this != &ot)
93 {
94 m_ot_unix_time = ot.m_ot_unix_time;
95 m_ot_usec = ot.m_ot_usec;
96 }
97
98 return *this;
99 }
100
101 ~time (void) = default;
102
103 OCTAVE_API void stamp (void);
104
105 double double_value (void) const
106 {
107 return as_double (m_ot_unix_time, m_ot_usec);
108 }
109
110 OCTAVE_TIME_T unix_time (void) const { return m_ot_unix_time; }
111
112 long usec (void) const { return m_ot_usec; }
113
114 OCTAVE_API std::string ctime (void) const;
115
116 friend OCTAVE_API std::ostream& operator << (std::ostream& os, const time& ot);
117
118 private:
119
120 // Seconds since the epoch.
121 OCTAVE_TIME_T m_ot_unix_time;
122
123 // Additional microseconds.
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 base_tm
186 {
187 public:
188
189 base_tm (void)
190 : m_usec (0), m_sec (0), m_min (0), m_hour (0),
191 m_mday (0), m_mon (0), m_year (0), m_wday (0),
192 m_yday (0), m_isdst (0), m_gmtoff (0), m_zone ("unknown")
193 { }
194
195 base_tm (const base_tm& tm)
196 : m_usec (tm.m_usec), m_sec (tm.m_sec), m_min (tm.m_min),
197 m_hour (tm.m_hour), m_mday (tm.m_mday), m_mon (tm.m_mon),
198 m_year (tm.m_year), m_wday (tm.m_wday), m_yday (tm.m_yday),
199 m_isdst (tm.m_isdst), m_gmtoff (tm.m_gmtoff), m_zone (tm.m_zone)
200 { }
201
202 base_tm& operator = (const base_tm& tm)
203 {
204 if (this != &tm)
205 {
206 m_usec = tm.m_usec;
207 m_sec = tm.m_sec;
208 m_min = tm.m_min;
209 m_hour = tm.m_hour;
210 m_mday = tm.m_mday;
211 m_mon = tm.m_mon;
212 m_year = tm.m_year;
213 m_wday = tm.m_wday;
214 m_yday = tm.m_yday;
215 m_isdst = tm.m_isdst;
216 m_gmtoff = tm.m_gmtoff;
217 m_zone = tm.m_zone;
218 }
219
220 return *this;
221 }
222
223 virtual ~base_tm (void) = default;
224
225 int usec (void) const { return m_usec; }
226 int sec (void) const { return m_sec; }
227 int min (void) const { return m_min; }
228 int hour (void) const { return m_hour; }
229 int mday (void) const { return m_mday; }
230 int mon (void) const { return m_mon; }
231 int year (void) const { return m_year; }
232 int wday (void) const { return m_wday; }
233 int yday (void) const { return m_yday; }
234 int isdst (void) const { return m_isdst; }
235 long gmtoff (void) const { return m_gmtoff; }
236 std::string zone (void) const { return m_zone; }
237
238 OCTAVE_API base_tm& usec (int v);
239 OCTAVE_API base_tm& sec (int v);
240 OCTAVE_API base_tm& min (int v);
241 OCTAVE_API base_tm& hour (int v);
242 OCTAVE_API base_tm& mday (int v);
243 OCTAVE_API base_tm& mon (int v);
244 OCTAVE_API base_tm& year (int v);
245 OCTAVE_API base_tm& wday (int v);
246 OCTAVE_API base_tm& yday (int v);
247 OCTAVE_API base_tm& isdst (int v);
248 OCTAVE_API base_tm& gmtoff (long v);
249 OCTAVE_API base_tm& zone (const std::string& s);
250
251 OCTAVE_API std::string strftime (const std::string& fmt) const;
252
253 std::string asctime (void) const
254 { return strftime ("%a %b %d %H:%M:%S %Y\n"); }
255
256 protected:
257
258 // Microseconds after the second (0, 999999).
260
261 // Seconds after the minute (0, 61).
262 int m_sec;
263
264 // Minutes after the hour (0, 59).
265 int m_min;
266
267 // Hours since midnight (0, 23).
269
270 // Day of the month (1, 31).
272
273 // Months since January (0, 11).
274 int m_mon;
275
276 // Years since 1900.
278
279 // Days since Sunday (0, 6).
281
282 // Days since January 1 (0, 365).
284
285 // Daylight saving time flag.
287
288 // Time zone.
290
291 // Time zone.
292 std::string m_zone;
293
294 OCTAVE_API void init (void *p);
295 };
296
297 class
298 localtime : public base_tm
299 {
300 public:
301
303 : base_tm () { init (time ()); }
304
305 localtime (const time& ot)
306 : base_tm () { init (ot); }
307
309 : base_tm (t) { }
310
311 localtime& operator = (const localtime& t)
312 {
314 return *this;
315 }
316
317 ~localtime (void) = default;
318
319 private:
320
321 OCTAVE_API void init (const time& ot);
322 };
323
324 class
325 gmtime : public base_tm
326 {
327 public:
328
329 gmtime (void)
330 : base_tm () { init (time ()); }
331
332 gmtime (const time& ot)
333 : base_tm () { init (ot); }
334
335 gmtime& operator = (const gmtime& t)
336 {
338 return *this;
339 }
340
341 ~gmtime (void) = default;
342
343 private:
344
345 OCTAVE_API void init (const time& ot);
346 };
347
348 class
349 strptime : public base_tm
350 {
351 public:
352
353 strptime (const std::string& str, const std::string& fmt)
354 : base_tm (), m_nchars (0)
355 {
356 init (str, fmt);
357 }
358
360 : base_tm (s), m_nchars (s.m_nchars) { }
361
362 strptime& operator = (const strptime& s)
363 {
365 m_nchars = s.m_nchars;
366 return *this;
367 }
368
369 int characters_converted (void) const { return m_nchars; }
370
371 ~strptime (void) = default;
372
373 private:
374
376
377 OCTAVE_API void init (const std::string& str, const std::string& fmt);
378 };
379
380 class
382 {
383 public:
384
385 friend class resource_usage;
386
387 cpu_time (void)
388 : m_usr_sec (0), m_sys_sec (0), m_usr_usec (0), m_sys_usec (0)
389 {
390 stamp ();
391 }
392
393 cpu_time (const cpu_time& tm)
394 : m_usr_sec (tm.m_usr_sec), m_sys_sec (tm.m_sys_sec),
395 m_usr_usec (tm.m_usr_usec), m_sys_usec (tm.m_sys_usec)
396 { }
397
398 cpu_time& operator = (const cpu_time& tm)
399 {
400 if (&tm != this)
401 {
402 m_usr_sec = tm.m_usr_sec;
403 m_sys_sec = tm.m_sys_sec;
404 m_usr_usec = tm.m_usr_usec;
405 m_sys_usec = tm.m_sys_usec;
406 }
407
408 return *this;
409 }
410
411 OCTAVE_API void stamp (void);
412
413 double user (void) const
414 {
415 return as_double (m_usr_sec, m_usr_usec);
416 }
417
418 double system (void) const
419 {
420 return as_double (m_sys_sec, m_sys_usec);
421 }
422
423 OCTAVE_TIME_T user_sec (void) const { return m_usr_sec; }
424 long user_usec (void) const { return m_usr_usec; }
425
426 OCTAVE_TIME_T system_sec (void) const { return m_sys_sec; }
427 long system_usec (void) const { return m_sys_usec; }
428
429 private:
430
431 OCTAVE_TIME_T m_usr_sec;
432 OCTAVE_TIME_T m_sys_sec;
433
436
437 cpu_time (OCTAVE_TIME_T usr_sec, OCTAVE_TIME_T sys_sec, long usr_usec, long sys_usec)
438 : m_usr_sec (usr_sec), m_sys_sec (sys_sec),
439 m_usr_usec (usr_usec), m_sys_usec (sys_usec)
440 { }
441 };
442
443 class
445 {
446 public:
447
449 : m_cpu (), m_maxrss (0), m_ixrss (0), m_idrss (0),
450 m_isrss (0), m_minflt (0), m_majflt (0), m_nswap (0),
451 m_inblock (0), m_oublock (0), m_msgsnd (0), m_msgrcv (0),
452 m_nsignals (0), m_nvcsw (0), m_nivcsw (0)
453 {
454 stamp ();
455 }
456
458 : m_cpu (ru.m_cpu), m_maxrss (ru.m_maxrss),
459 m_ixrss (ru.m_ixrss), m_idrss (ru.m_idrss),
460 m_isrss (ru.m_isrss), m_minflt (ru.m_minflt),
461 m_majflt (ru.m_majflt), m_nswap (ru.m_nswap),
462 m_inblock (ru.m_inblock), m_oublock (ru.m_oublock),
463 m_msgsnd (ru.m_msgsnd), m_msgrcv (ru.m_msgrcv),
464 m_nsignals (ru.m_nsignals), m_nvcsw (ru.m_nvcsw),
465 m_nivcsw (ru.m_nivcsw)
466 { }
467
468 resource_usage& operator = (const resource_usage& ru)
469 {
470 if (&ru != this)
471 {
472 m_cpu = ru.m_cpu;
473
474 m_maxrss = ru.m_maxrss;
475 m_ixrss = ru.m_ixrss;
476 m_idrss = ru.m_idrss;
477 m_isrss = ru.m_isrss;
478 m_minflt = ru.m_minflt;
479 m_majflt = ru.m_majflt;
480 m_nswap = ru.m_nswap;
481 m_inblock = ru.m_inblock;
482 m_oublock = ru.m_oublock;
483 m_msgsnd = ru.m_msgsnd;
484 m_msgrcv = ru.m_msgrcv;
485 m_nsignals = ru.m_nsignals;
486 m_nvcsw = ru.m_nvcsw;
487 m_nivcsw = ru.m_nivcsw;
488 }
489
490 return *this;
491 }
492
493 OCTAVE_API void stamp (void);
494
495 cpu_time cpu (void) const { return m_cpu; }
496
497 long maxrss (void) const { return m_maxrss; }
498 long ixrss (void) const { return m_ixrss; }
499 long idrss (void) const { return m_idrss; }
500 long isrss (void) const { return m_isrss; }
501 long minflt (void) const { return m_minflt; }
502 long majflt (void) const { return m_majflt; }
503 long nswap (void) const { return m_nswap; }
504 long inblock (void) const { return m_inblock; }
505 long oublock (void) const { return m_oublock; }
506 long msgsnd (void) const { return m_msgsnd; }
507 long msgrcv (void) const { return m_msgrcv; }
508 long nsignals (void) const { return m_nsignals; }
509 long nvcsw (void) const { return m_nvcsw; }
510 long nivcsw (void) const { return m_nivcsw; }
511
512 private:
513
515
530 };
531 }
532}
533
534#endif
charNDArray min(char d, const charNDArray &m)
Definition: chNDArray.cc:207
std::string zone(void) const
Definition: oct-time.h:236
int isdst(void) const
Definition: oct-time.h:234
int hour(void) const
Definition: oct-time.h:228
base_tm & operator=(const base_tm &tm)
Definition: oct-time.h:202
int mon(void) const
Definition: oct-time.h:230
base_tm(const base_tm &tm)
Definition: oct-time.h:195
int usec(void) const
Definition: oct-time.h:225
int mday(void) const
Definition: oct-time.h:229
std::string m_zone
Definition: oct-time.h:292
int min(void) const
Definition: oct-time.h:227
int wday(void) const
Definition: oct-time.h:232
int yday(void) const
Definition: oct-time.h:233
virtual ~base_tm(void)=default
int sec(void) const
Definition: oct-time.h:226
std::string asctime(void) const
Definition: oct-time.h:253
long gmtoff(void) const
Definition: oct-time.h:235
int year(void) const
Definition: oct-time.h:231
cpu_time(OCTAVE_TIME_T usr_sec, OCTAVE_TIME_T sys_sec, long usr_usec, long sys_usec)
Definition: oct-time.h:437
long system_usec(void) const
Definition: oct-time.h:427
cpu_time(const cpu_time &tm)
Definition: oct-time.h:393
OCTAVE_TIME_T m_usr_sec
Definition: oct-time.h:431
OCTAVE_TIME_T system_sec(void) const
Definition: oct-time.h:426
double system(void) const
Definition: oct-time.h:418
long user_usec(void) const
Definition: oct-time.h:424
OCTAVE_TIME_T user_sec(void) const
Definition: oct-time.h:423
OCTAVE_TIME_T m_sys_sec
Definition: oct-time.h:432
double user(void) const
Definition: oct-time.h:413
~gmtime(void)=default
gmtime(const time &ot)
Definition: oct-time.h:332
localtime(const time &ot)
Definition: oct-time.h:305
~localtime(void)=default
localtime(const localtime &t)
Definition: oct-time.h:308
long maxrss(void) const
Definition: oct-time.h:497
long nsignals(void) const
Definition: oct-time.h:508
long idrss(void) const
Definition: oct-time.h:499
long inblock(void) const
Definition: oct-time.h:504
cpu_time cpu(void) const
Definition: oct-time.h:495
long minflt(void) const
Definition: oct-time.h:501
long nswap(void) const
Definition: oct-time.h:503
long isrss(void) const
Definition: oct-time.h:500
long msgrcv(void) const
Definition: oct-time.h:507
long msgsnd(void) const
Definition: oct-time.h:506
long ixrss(void) const
Definition: oct-time.h:498
resource_usage(const resource_usage &ru)
Definition: oct-time.h:457
long oublock(void) const
Definition: oct-time.h:505
long majflt(void) const
Definition: oct-time.h:502
long nvcsw(void) const
Definition: oct-time.h:509
long nivcsw(void) const
Definition: oct-time.h:510
strptime(const strptime &s)
Definition: oct-time.h:359
int characters_converted(void) const
Definition: oct-time.h:369
~strptime(void)=default
strptime(const std::string &str, const std::string &fmt)
Definition: oct-time.h:353
OCTAVE_TIME_T unix_time(void) const
Definition: oct-time.h:110
time(const time &ot)
Definition: oct-time.h:87
double double_value(void) const
Definition: oct-time.h:105
time(OCTAVE_TIME_T t, long us)
Definition: oct-time.h:61
~time(void)=default
time(OCTAVE_TIME_T t)
Definition: oct-time.h:58
OCTAVE_TIME_T m_ot_unix_time
Definition: oct-time.h:121
long usec(void) const
Definition: oct-time.h:112
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
#define OCTAVE_API
Definition: main.in.cc:55
bool operator!=(const time &t1, const time &t2)
Definition: oct-time.h:134
bool operator==(const time &t1, const time &t2)
Definition: oct-time.h:128
bool operator<=(const time &t1, const time &t2)
Definition: oct-time.h:153
bool operator>=(const time &t1, const time &t2)
Definition: oct-time.h:172
std::ostream & operator<<(std::ostream &os, const time &ot)
Definition: oct-time.cc:103
bool operator<(const time &t1, const time &t2)
Definition: oct-time.h:140
bool operator>(const time &t1, const time &t2)
Definition: oct-time.h:159
time operator+(const time &t1, const time &t2)
Definition: oct-time.h:178
octave_int< T > rem(const octave_int< T > &x, const octave_int< T > &y)
Definition: oct-inttypes.h:909
static double as_double(OCTAVE_TIME_T sec, long usec)
Definition: oct-time.h:35