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