GNU Octave 10.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
oct-time.h
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 1999-2025 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
35static inline double
36as_double (OCTAVE_TIME_T sec, long usec)
37{
38 // Unix time will be exactly representable as a double for more than
39 // 100 million years, so no worry there, and microseconds has a
40 // range of 0-1e6, so we are safe there as well.
41
42 return (static_cast<double> (sec) + static_cast<double> (usec) / 1.0e6);
43}
44
46
48
49class base_tm;
50
51class time
52{
53public:
54
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 () = default;
102
103 OCTAVE_API void stamp ();
104
105 double double_value () const
106 {
107 return as_double (m_ot_unix_time, m_ot_usec);
108 }
109
110 OCTAVE_TIME_T unix_time () const { return m_ot_unix_time; }
111
112 long usec () const { return m_ot_usec; }
113
114 OCTAVE_API std::string ctime () const;
115
116 friend OCTAVE_API std::ostream& operator << (std::ostream& os, const time& ot);
117
118private:
119
120 // Seconds since the epoch.
121 OCTAVE_TIME_T m_ot_unix_time;
122
123 // Additional microseconds.
124 long m_ot_usec;
125};
126
127inline bool
128operator == (const time& t1, const time& t2)
129{
130 return (t1.unix_time () == t2.unix_time () && t1.usec () == t2.usec ());
131}
132
133inline bool
134operator != (const time& t1, const time& t2)
135{
136 return ! (t1 == t2);
137}
138
139inline bool
140operator < (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
152inline bool
153operator <= (const time& t1, const time& t2)
154{
155 return (t1 < t2 || t1 == t2);
156}
157
158inline bool
159operator > (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
171inline bool
172operator >= (const time& t1, const time& t2)
173{
174 return (t1 > t2 || t1 == t2);
175}
176
177inline time
178operator + (const time& t1, const time& t2)
179{
180 return time (t1.unix_time () + t2.unix_time (),
181 t1.usec () + t2.usec ());
182}
183
185{
186public:
187
189 : m_usec (0), m_sec (0), m_min (0), m_hour (0),
190 m_mday (0), m_mon (0), m_year (0), m_wday (0),
191 m_yday (0), m_isdst (0), m_gmtoff (0), m_zone ("unknown")
192 { }
193
194 base_tm (const base_tm& tm)
195 : m_usec (tm.m_usec), m_sec (tm.m_sec), m_min (tm.m_min),
196 m_hour (tm.m_hour), m_mday (tm.m_mday), m_mon (tm.m_mon),
197 m_year (tm.m_year), m_wday (tm.m_wday), m_yday (tm.m_yday),
199 { }
200
202 {
203 if (this != &tm)
204 {
205 m_usec = tm.m_usec;
206 m_sec = tm.m_sec;
207 m_min = tm.m_min;
208 m_hour = tm.m_hour;
209 m_mday = tm.m_mday;
210 m_mon = tm.m_mon;
211 m_year = tm.m_year;
212 m_wday = tm.m_wday;
213 m_yday = tm.m_yday;
214 m_isdst = tm.m_isdst;
215 m_gmtoff = tm.m_gmtoff;
216 m_zone = tm.m_zone;
217 }
218
219 return *this;
220 }
221
222 virtual ~base_tm () = default;
223
224 int usec () const { return m_usec; }
225 int sec () const { return m_sec; }
226 int min () const { return m_min; }
227 int hour () const { return m_hour; }
228 int mday () const { return m_mday; }
229 int mon () const { return m_mon; }
230 int year () const { return m_year; }
231 int wday () const { return m_wday; }
232 int yday () const { return m_yday; }
233 int isdst () const { return m_isdst; }
234 long gmtoff () const { return m_gmtoff; }
235 std::string zone () const { return m_zone; }
236
237 OCTAVE_API base_tm& usec (int v);
238 OCTAVE_API base_tm& sec (int v);
239 OCTAVE_API base_tm& min (int v);
240 OCTAVE_API base_tm& hour (int v);
241 OCTAVE_API base_tm& mday (int v);
242 OCTAVE_API base_tm& mon (int v);
243 OCTAVE_API base_tm& year (int v);
244 OCTAVE_API base_tm& wday (int v);
245 OCTAVE_API base_tm& yday (int v);
246 OCTAVE_API base_tm& isdst (int v);
247 OCTAVE_API base_tm& gmtoff (long v);
248 OCTAVE_API base_tm& zone (const std::string& s);
249
250 OCTAVE_API std::string strftime (const std::string& fmt) const;
251
252 std::string asctime () const
253 { return strftime ("%a %b %d %H:%M:%S %Y\n"); }
254
255protected:
256
257 // Microseconds after the second (0, 999999).
259
260 // Seconds after the minute (0, 61).
261 int m_sec;
262
263 // Minutes after the hour (0, 59).
264 int m_min;
265
266 // Hours since midnight (0, 23).
268
269 // Day of the month (1, 31).
271
272 // Months since January (0, 11).
273 int m_mon;
274
275 // Years since 1900.
277
278 // Days since Sunday (0, 6).
280
281 // Days since January 1 (0, 365).
283
284 // Daylight saving time flag.
286
287 // Time zone.
289
290 // Time zone.
291 std::string m_zone;
292
293 OCTAVE_API void init (void *p);
294};
295
296class localtime : public base_tm
297{
298public:
299
301 : base_tm () { init (time ()); }
302
303 localtime (const time& ot)
304 : base_tm () { init (ot); }
305
307 : base_tm (t) { }
308
310 {
312 return *this;
313 }
314
315 ~localtime () = default;
316
317private:
318
319 OCTAVE_API void init (const time& ot);
320};
321
322class gmtime : public base_tm
323{
324public:
325
327 : base_tm () { init (time ()); }
328
329 gmtime (const time& ot)
330 : base_tm () { init (ot); }
331
332 OCTAVE_DEFAULT_COPY_MOVE_DELETE (gmtime)
333
334private:
335
336 OCTAVE_API void init (const time& ot);
337};
338
339class strptime : public base_tm
340{
341public:
342
343 strptime () = delete;
344
345 strptime (const std::string& str, const std::string& fmt)
346 : base_tm (), m_nchars (0)
347 {
348 init (str, fmt);
349 }
350
351 OCTAVE_DEFAULT_COPY_MOVE_DELETE (strptime)
352
353 int characters_converted () const { return m_nchars; }
354
355private:
356
357 int m_nchars;
358
359 OCTAVE_API void init (const std::string& str, const std::string& fmt);
360};
361
363{
364public:
365
366 friend class resource_usage;
367
369 : m_usr_sec (0), m_sys_sec (0), m_usr_usec (0), m_sys_usec (0)
370 {
371 stamp ();
372 }
373
374 OCTAVE_DEFAULT_COPY_MOVE_DELETE (cpu_time)
375
376 OCTAVE_API void stamp ();
377
378 double user () const
379 {
380 return as_double (m_usr_sec, m_usr_usec);
381 }
382
383 double system () const
384 {
385 return as_double (m_sys_sec, m_sys_usec);
386 }
387
388 OCTAVE_TIME_T user_sec () const { return m_usr_sec; }
389 long user_usec () const { return m_usr_usec; }
390
391 OCTAVE_TIME_T system_sec () const { return m_sys_sec; }
392 long system_usec () const { return m_sys_usec; }
393
394private:
395
396 OCTAVE_TIME_T m_usr_sec;
397 OCTAVE_TIME_T m_sys_sec;
398
399 long m_usr_usec;
400 long m_sys_usec;
401
402 cpu_time (OCTAVE_TIME_T usr_sec, OCTAVE_TIME_T sys_sec, long usr_usec, long sys_usec)
403 : m_usr_sec (usr_sec), m_sys_sec (sys_sec),
404 m_usr_usec (usr_usec), m_sys_usec (sys_usec)
405 { }
406};
407
409{
410public:
411
413 : m_cpu (), m_maxrss (0), m_ixrss (0), m_idrss (0),
414 m_isrss (0), m_minflt (0), m_majflt (0), m_nswap (0),
415 m_inblock (0), m_oublock (0), m_msgsnd (0), m_msgrcv (0),
416 m_nsignals (0), m_nvcsw (0), m_nivcsw (0)
417 {
418 stamp ();
419 }
420
421 OCTAVE_DEFAULT_COPY_MOVE_DELETE (resource_usage)
422
423 OCTAVE_API void stamp ();
424
425 cpu_time cpu () const { return m_cpu; }
426
427 long maxrss () const { return m_maxrss; }
428 long ixrss () const { return m_ixrss; }
429 long idrss () const { return m_idrss; }
430 long isrss () const { return m_isrss; }
431 long minflt () const { return m_minflt; }
432 long majflt () const { return m_majflt; }
433 long nswap () const { return m_nswap; }
434 long inblock () const { return m_inblock; }
435 long oublock () const { return m_oublock; }
436 long msgsnd () const { return m_msgsnd; }
437 long msgrcv () const { return m_msgrcv; }
438 long nsignals () const { return m_nsignals; }
439 long nvcsw () const { return m_nvcsw; }
440 long nivcsw () const { return m_nivcsw; }
441
442private:
443
444 cpu_time m_cpu;
445
446 long m_maxrss;
447 long m_ixrss;
448 long m_idrss;
449 long m_isrss;
450 long m_minflt;
451 long m_majflt;
452 long m_nswap;
453 long m_inblock;
454 long m_oublock;
455 long m_msgsnd;
456 long m_msgrcv;
457 long m_nsignals;
458 long m_nvcsw;
459 long m_nivcsw;
460};
461
462// class to handle file time efficiently on different platforms
463
465{
466public:
467
468 file_time ();
469
470 file_time (OCTAVE_TIME_T t)
471 : m_time (t)
472 { }
473
474 file_time (const std::string& filename);
475
477 {
478 m_time = ot.time ();
479 }
480
481 file_time& operator = (const file_time& ot)
482 {
483 if (this != &ot)
484 m_time = ot.time ();
485
486 return *this;
487 }
488
489 ~file_time () = default;
490
491 inline static file_time time_resolution ()
492 {
493#if defined (OCTAVE_USE_WINDOWS_API)
494 // FAT file systems have 2 seconds resolution for the modification time.
495 static OCTAVE_TIME_T time_resolution = 20000000;
496#else
497 // Assume 1 second (see file_stat)
498 static OCTAVE_TIME_T time_resolution = 1;
499#endif
500 return time_resolution;
501 }
502
503 inline bool
504 operator == (const file_time& t2) const
505 {
506 return time () == t2.time ();
507 }
508
509 inline bool
510 operator != (const file_time& t2) const
511 {
512 return ! (*this == t2);
513 }
514
515 inline bool
516 operator < (const file_time& t2) const
517 {
518 return time () < t2.time ();
519 }
520
521 inline bool
522 operator <= (const file_time& t2) const
523 {
524 return (*this < t2 || *this == t2);
525 }
526
527 inline bool
528 operator > (const file_time& t2) const
529 {
530 return time () > t2.time ();
531 }
532
533 inline bool
534 operator >= (const file_time& t2) const
535 {
536 return (*this > t2 || *this == t2);
537 }
538
539 inline file_time
540 operator + (const file_time& t2) const
541 {
542 return file_time (time () + t2.time ());
543 }
544
545 inline file_time
546 operator + (const OCTAVE_TIME_T t2) const
547 {
548 return file_time (time () + t2);
549 }
550
551 OCTAVE_TIME_T time () const { return m_time; }
552
553private:
554
555 // The native file time type differs per platform.
556 // On POSIX, this is the number of 1 second intervals since the epoch.
557 // On Windows, this is the number of 100 ns intervals since a different epoch.
558 OCTAVE_TIME_T m_time;
559};
560
561OCTAVE_END_NAMESPACE(sys)
562OCTAVE_END_NAMESPACE(octave)
563
564#endif
ComplexColumnVector operator+(const ComplexColumnVector &x)
Definition CColVector.h:156
int mday() const
Definition oct-time.h:228
int yday() const
Definition oct-time.h:232
int m_sec
Definition oct-time.h:261
int m_isdst
Definition oct-time.h:285
int m_year
Definition oct-time.h:276
int hour() const
Definition oct-time.h:227
int m_usec
Definition oct-time.h:258
int m_mday
Definition oct-time.h:270
int wday() const
Definition oct-time.h:231
int m_min
Definition oct-time.h:264
std::string m_zone
Definition oct-time.h:291
base_tm & operator=(const base_tm &tm)
Definition oct-time.h:201
int mon() const
Definition oct-time.h:229
void init(void *p)
Definition oct-time.cc:231
int min() const
Definition oct-time.h:226
std::string asctime() const
Definition oct-time.h:252
long m_gmtoff
Definition oct-time.h:288
int isdst() const
Definition oct-time.h:233
int m_hour
Definition oct-time.h:267
int sec() const
Definition oct-time.h:225
long gmtoff() const
Definition oct-time.h:234
virtual ~base_tm()=default
std::string zone() const
Definition oct-time.h:235
int usec() const
Definition oct-time.h:224
int m_wday
Definition oct-time.h:279
int year() const
Definition oct-time.h:230
std::string strftime(const std::string &fmt) const
Definition oct-time.cc:173
base_tm(const base_tm &tm)
Definition oct-time.h:194
int m_yday
Definition oct-time.h:282
int m_mon
Definition oct-time.h:273
base_tm()
Definition oct-time.h:188
double system() const
Definition oct-time.h:383
long user_usec() const
Definition oct-time.h:389
double user() const
Definition oct-time.h:378
OCTAVE_TIME_T user_sec() const
Definition oct-time.h:388
OCTAVE_TIME_T system_sec() const
Definition oct-time.h:391
void stamp()
Definition oct-time.cc:351
long system_usec() const
Definition oct-time.h:392
static file_time time_resolution()
Definition oct-time.h:491
OCTAVE_TIME_T time() const
Definition oct-time.h:551
file_time(const file_time &ot)
Definition oct-time.h:476
~file_time()=default
file_time(OCTAVE_TIME_T t)
Definition oct-time.h:470
gmtime(const time &ot)
Definition oct-time.h:329
gmtime()
Definition oct-time.h:326
localtime(const localtime &t)
Definition oct-time.h:306
localtime & operator=(const localtime &t)
Definition oct-time.h:309
localtime(const time &ot)
Definition oct-time.h:303
~localtime()=default
long maxrss() const
Definition oct-time.h:427
long nvcsw() const
Definition oct-time.h:439
long msgsnd() const
Definition oct-time.h:436
long nsignals() const
Definition oct-time.h:438
cpu_time cpu() const
Definition oct-time.h:425
long minflt() const
Definition oct-time.h:431
long inblock() const
Definition oct-time.h:434
long idrss() const
Definition oct-time.h:429
long nivcsw() const
Definition oct-time.h:440
long isrss() const
Definition oct-time.h:430
long ixrss() const
Definition oct-time.h:428
long oublock() const
Definition oct-time.h:435
long majflt() const
Definition oct-time.h:432
long nswap() const
Definition oct-time.h:433
long msgrcv() const
Definition oct-time.h:437
strptime()=delete
int characters_converted() const
Definition oct-time.h:353
strptime(const std::string &str, const std::string &fmt)
Definition oct-time.h:345
time()
Definition oct-time.h:55
time(OCTAVE_TIME_T t, long us)
Definition oct-time.h:61
void stamp()
Definition oct-time.cc:116
time & operator=(const time &ot)
Definition oct-time.h:90
~time()=default
friend std::ostream & operator<<(std::ostream &os, const time &ot)
Definition oct-time.cc:105
std::string ctime() const
Definition oct-time.cc:99
long usec() const
Definition oct-time.h:112
time(const time &ot)
Definition oct-time.h:87
OCTAVE_TIME_T unix_time() const
Definition oct-time.h:110
double double_value() const
Definition oct-time.h:105
time(OCTAVE_TIME_T t)
Definition oct-time.h:58
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
bool operator!=(const dim_vector &a, const dim_vector &b)
Definition dim-vector.h:532
bool operator==(const dim_vector &a, const dim_vector &b)
Definition dim-vector.h:516
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.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
time operator+(const time &t1, const time &t2)
Definition oct-time.h:178
bool operator>=(const time &t1, const time &t2)
Definition oct-time.h:172
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:153
bool operator>(const time &t1, const time &t2)
Definition oct-time.h:159