1 | //
|
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2018 University of Waterloo
|
---|
3 | //
|
---|
4 | // The contents of this file are covered under the licence agreement in the
|
---|
5 | // file "LICENCE" distributed with Cforall.
|
---|
6 | //
|
---|
7 | // time --
|
---|
8 | //
|
---|
9 | // Author : Peter A. Buhr
|
---|
10 | // Created On : Wed Mar 14 23:18:57 2018
|
---|
11 | // Last Modified By : Peter A. Buhr
|
---|
12 | // Last Modified On : Sat Oct 8 09:07:48 2022
|
---|
13 | // Update Count : 668
|
---|
14 | //
|
---|
15 |
|
---|
16 | #pragma once
|
---|
17 |
|
---|
18 | // http://en.cppreference.com/w/cpp/header/chrono
|
---|
19 | // http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0355r5.html#refcc
|
---|
20 |
|
---|
21 | #include <time.h> // timespec
|
---|
22 | #include <sys/time.h> // timeval
|
---|
23 | #include <time_t.hfa> // Duration/Time types
|
---|
24 |
|
---|
25 | enum { TIMEGRAN = 1_000_000_000LL }; // nanosecond granularity, except for timeval
|
---|
26 |
|
---|
27 | //######################### Duration #########################
|
---|
28 |
|
---|
29 | static inline __attribute__((always_inline)) {
|
---|
30 | void ?{}( Duration & dur, timeval t ) with( dur ) { tn = (int64_t)t.tv_sec * TIMEGRAN + t.tv_usec * 1000; }
|
---|
31 | void ?{}( Duration & dur, timespec t ) with( dur ) { tn = (int64_t)t.tv_sec * TIMEGRAN + t.tv_nsec; }
|
---|
32 |
|
---|
33 | Duration ?=?( Duration & dur, __attribute__((unused)) zero_t ) { return dur{ 0 }; }
|
---|
34 | Duration ?=?( Duration & dur, timeval t ) with( dur ) {
|
---|
35 | tn = (int64_t)t.tv_sec * TIMEGRAN + t.tv_usec * (TIMEGRAN / 1_000_000LL);
|
---|
36 | return dur;
|
---|
37 | } // ?=?
|
---|
38 | Duration ?=?( Duration & dur, timespec t ) with( dur ) {
|
---|
39 | tn = (int64_t)t.tv_sec * TIMEGRAN + t.tv_nsec;
|
---|
40 | return dur;
|
---|
41 | } // ?=?
|
---|
42 |
|
---|
43 | Duration +?( Duration rhs ) with( rhs ) { return (Duration)@{ +tn }; }
|
---|
44 | Duration ?+?( Duration & lhs, Duration rhs ) { return (Duration)@{ lhs.tn + rhs.tn }; }
|
---|
45 | Duration ?+=?( Duration & lhs, Duration rhs ) { lhs = lhs + rhs; return lhs; }
|
---|
46 |
|
---|
47 | Duration -?( Duration rhs ) with( rhs ) { return (Duration)@{ -tn }; }
|
---|
48 | Duration ?-?( Duration & lhs, Duration rhs ) { return (Duration)@{ lhs.tn - rhs.tn }; }
|
---|
49 | Duration ?-=?( Duration & lhs, Duration rhs ) { lhs = lhs - rhs; return lhs; }
|
---|
50 |
|
---|
51 | Duration ?*?( Duration lhs, int64_t rhs ) { return (Duration)@{ lhs.tn * rhs }; }
|
---|
52 | Duration ?*?( int64_t lhs, Duration rhs ) { return (Duration)@{ lhs * rhs.tn }; }
|
---|
53 | Duration ?*=?( Duration & lhs, int64_t rhs ) { lhs = lhs * rhs; return lhs; }
|
---|
54 |
|
---|
55 | int64_t ?/?( Duration lhs, Duration rhs ) { return lhs.tn / rhs.tn; }
|
---|
56 | Duration ?/?( Duration lhs, int64_t rhs ) { return (Duration)@{ lhs.tn / rhs }; }
|
---|
57 | Duration ?/=?( Duration & lhs, int64_t rhs ) { lhs = lhs / rhs; return lhs; }
|
---|
58 | double div( Duration lhs, Duration rhs ) { return (double)lhs.tn / (double)rhs.tn; }
|
---|
59 |
|
---|
60 | Duration ?%?( Duration lhs, Duration rhs ) { return (Duration)@{ lhs.tn % rhs.tn }; }
|
---|
61 | Duration ?%=?( Duration & lhs, Duration rhs ) { lhs = lhs % rhs; return lhs; }
|
---|
62 |
|
---|
63 | bool ?==?( Duration lhs, __attribute__((unused)) zero_t ) { return lhs.tn == 0; }
|
---|
64 | bool ?!=?( Duration lhs, __attribute__((unused)) zero_t ) { return lhs.tn != 0; }
|
---|
65 | bool ?<? ( Duration lhs, __attribute__((unused)) zero_t ) { return lhs.tn < 0; }
|
---|
66 | bool ?<=?( Duration lhs, __attribute__((unused)) zero_t ) { return lhs.tn <= 0; }
|
---|
67 | bool ?>? ( Duration lhs, __attribute__((unused)) zero_t ) { return lhs.tn > 0; }
|
---|
68 | bool ?>=?( Duration lhs, __attribute__((unused)) zero_t ) { return lhs.tn >= 0; }
|
---|
69 |
|
---|
70 | bool ?==?( Duration lhs, Duration rhs ) { return lhs.tn == rhs.tn; }
|
---|
71 | bool ?!=?( Duration lhs, Duration rhs ) { return lhs.tn != rhs.tn; }
|
---|
72 | bool ?<? ( Duration lhs, Duration rhs ) { return lhs.tn < rhs.tn; }
|
---|
73 | bool ?<=?( Duration lhs, Duration rhs ) { return lhs.tn <= rhs.tn; }
|
---|
74 | bool ?>? ( Duration lhs, Duration rhs ) { return lhs.tn > rhs.tn; }
|
---|
75 | bool ?>=?( Duration lhs, Duration rhs ) { return lhs.tn >= rhs.tn; }
|
---|
76 |
|
---|
77 | Duration abs( Duration rhs ) { return rhs.tn >= 0 ? rhs : -rhs; }
|
---|
78 |
|
---|
79 | Duration ?`ns( int64_t nsec ) { return (Duration)@{ nsec }; }
|
---|
80 | Duration ?`us( int64_t usec ) { return (Duration)@{ usec * (TIMEGRAN / 1_000_000LL) }; }
|
---|
81 | Duration ?`ms( int64_t msec ) { return (Duration)@{ msec * (TIMEGRAN / 1_000LL) }; }
|
---|
82 | Duration ?`s( int64_t sec ) { return (Duration)@{ sec * TIMEGRAN }; }
|
---|
83 | Duration ?`s( double sec ) { return (Duration)@{ sec * TIMEGRAN }; }
|
---|
84 | Duration ?`m( int64_t min ) { return (Duration)@{ min * (60LL * TIMEGRAN) }; }
|
---|
85 | Duration ?`m( double min ) { return (Duration)@{ min * (60LL * TIMEGRAN) }; }
|
---|
86 | Duration ?`h( int64_t hours ) { return (Duration)@{ hours * (60LL * 60LL * TIMEGRAN) }; }
|
---|
87 | Duration ?`h( double hours ) { return (Duration)@{ hours * (60LL * 60LL * TIMEGRAN) }; }
|
---|
88 | Duration ?`d( int64_t days ) { return (Duration)@{ days * (24LL * 60LL * 60LL * TIMEGRAN) }; }
|
---|
89 | Duration ?`d( double days ) { return (Duration)@{ days * (24LL * 60LL * 60LL * TIMEGRAN) }; }
|
---|
90 | Duration ?`w( int64_t weeks ) { return (Duration)@{ weeks * (7LL * 24LL * 60LL * 60LL * TIMEGRAN) }; }
|
---|
91 | Duration ?`w( double weeks ) { return (Duration)@{ weeks * (7LL * 24LL * 60LL * 60LL * TIMEGRAN) }; }
|
---|
92 |
|
---|
93 | int64_t ?`ns( Duration dur ) { return dur.tn; }
|
---|
94 | int64_t ?`us( Duration dur ) { return dur.tn / (TIMEGRAN / 1_000_000LL); }
|
---|
95 | int64_t ?`ms( Duration dur ) { return dur.tn / (TIMEGRAN / 1_000LL); }
|
---|
96 | int64_t ?`s( Duration dur ) { return dur.tn / TIMEGRAN; }
|
---|
97 | int64_t ?`m( Duration dur ) { return dur.tn / (60LL * TIMEGRAN); }
|
---|
98 | int64_t ?`h( Duration dur ) { return dur.tn / (60LL * 60LL * TIMEGRAN); }
|
---|
99 | int64_t ?`d( Duration dur ) { return dur.tn / (24LL * 60LL * 60LL * TIMEGRAN); }
|
---|
100 | int64_t ?`w( Duration dur ) { return dur.tn / (7LL * 24LL * 60LL * 60LL * TIMEGRAN); }
|
---|
101 |
|
---|
102 | double ?`dns( Duration dur ) { return dur.tn; }
|
---|
103 | double ?`dus( Duration dur ) { return dur.tn / ((double)TIMEGRAN / 1_000_000.); }
|
---|
104 | double ?`dms( Duration dur ) { return dur.tn / ((double)TIMEGRAN / 1_000.); }
|
---|
105 | double ?`ds( Duration dur ) { return dur.tn / (double)TIMEGRAN; }
|
---|
106 | double ?`dm( Duration dur ) { return dur.tn / (60. * TIMEGRAN); }
|
---|
107 | double ?`dh( Duration dur ) { return dur.tn / (60. * 60. * (double)TIMEGRAN); }
|
---|
108 | double ?`dd( Duration dur ) { return dur.tn / (24. * 60. * 60. * (double)TIMEGRAN); }
|
---|
109 | double ?`dw( Duration dur ) { return dur.tn / (7. * 24. * 60. * 60. * (double)TIMEGRAN); }
|
---|
110 |
|
---|
111 | Duration max( Duration lhs, Duration rhs ) { return (lhs.tn < rhs.tn) ? rhs : lhs;}
|
---|
112 | Duration min( Duration lhs, Duration rhs ) { return !(rhs.tn < lhs.tn) ? lhs : rhs;}
|
---|
113 | } // distribution
|
---|
114 |
|
---|
115 | //######################### C timeval #########################
|
---|
116 |
|
---|
117 | static inline __attribute__((always_inline)) {
|
---|
118 | void ?{}( timeval & t ) {}
|
---|
119 | void ?{}( timeval & t, time_t sec, suseconds_t usec ) { t.tv_sec = sec; t.tv_usec = usec; }
|
---|
120 | void ?{}( timeval & t, time_t sec ) { t{ sec, 0 }; }
|
---|
121 | void ?{}( timeval & t, __attribute__((unused)) zero_t ) { t{ 0, 0 }; }
|
---|
122 |
|
---|
123 | timeval ?=?( timeval & t, __attribute__((unused)) zero_t ) { return t{ 0 }; }
|
---|
124 | timeval ?+?( timeval lhs, timeval rhs ) { return (timeval)@{ lhs.tv_sec + rhs.tv_sec, lhs.tv_usec + rhs.tv_usec }; }
|
---|
125 | timeval ?-?( timeval lhs, timeval rhs ) { return (timeval)@{ lhs.tv_sec - rhs.tv_sec, lhs.tv_usec - rhs.tv_usec }; }
|
---|
126 | bool ?==?( timeval lhs, timeval rhs ) { return lhs.tv_sec == rhs.tv_sec && lhs.tv_usec == rhs.tv_usec; }
|
---|
127 | bool ?!=?( timeval lhs, timeval rhs ) { return lhs.tv_sec != rhs.tv_sec || lhs.tv_usec != rhs.tv_usec; }
|
---|
128 | } // distribution
|
---|
129 |
|
---|
130 | //######################### C timespec #########################
|
---|
131 |
|
---|
132 | static inline __attribute__((always_inline)) {
|
---|
133 | void ?{}( timespec & t ) {}
|
---|
134 | void ?{}( timespec & t, time_t sec, __syscall_slong_t nsec ) { t.tv_sec = sec; t.tv_nsec = nsec; }
|
---|
135 | void ?{}( timespec & t, time_t sec ) { t{ sec, 0}; }
|
---|
136 | void ?{}( timespec & t, __attribute__((unused)) zero_t ) { t{ 0, 0 }; }
|
---|
137 |
|
---|
138 | timespec ?=?( timespec & t, __attribute__((unused)) zero_t ) { return t{ 0 }; }
|
---|
139 | timespec ?+?( timespec lhs, timespec rhs ) { return (timespec)@{ lhs.tv_sec + rhs.tv_sec, lhs.tv_nsec + rhs.tv_nsec }; }
|
---|
140 | timespec ?-?( timespec lhs, timespec rhs ) { return (timespec)@{ lhs.tv_sec - rhs.tv_sec, lhs.tv_nsec - rhs.tv_nsec }; }
|
---|
141 | bool ?==?( timespec lhs, timespec rhs ) { return lhs.tv_sec == rhs.tv_sec && lhs.tv_nsec == rhs.tv_nsec; }
|
---|
142 | bool ?!=?( timespec lhs, timespec rhs ) { return lhs.tv_sec != rhs.tv_sec || lhs.tv_nsec != rhs.tv_nsec; }
|
---|
143 | } // distribution
|
---|
144 |
|
---|
145 | //######################### C itimerval #########################
|
---|
146 |
|
---|
147 | static inline __attribute__((always_inline)) {
|
---|
148 | void ?{}( itimerval & itv, Duration alarm ) with( itv ) {
|
---|
149 | // itimerval contains durations but but uses time data-structure timeval.
|
---|
150 | it_value{ alarm`s, (alarm % 1`s)`us }; // seconds, microseconds
|
---|
151 | it_interval{ 0 }; // 0 seconds, 0 microseconds
|
---|
152 | } // itimerval
|
---|
153 |
|
---|
154 | void ?{}( itimerval & itv, Duration alarm, Duration interval ) with( itv ) {
|
---|
155 | // itimerval contains durations but but uses time data-structure timeval.
|
---|
156 | it_value{ alarm`s, (alarm % 1`s)`us }; // seconds, microseconds
|
---|
157 | it_interval{ interval`s, interval`us }; // seconds, microseconds
|
---|
158 | } // itimerval
|
---|
159 | } // distribution
|
---|
160 |
|
---|
161 | //######################### Time #########################
|
---|
162 |
|
---|
163 | void ?{}( Time & time, int year, int month = 1, int day = 1, int hour = 0, int min = 0, int sec = 0, int64_t nsec = 0 );
|
---|
164 | static inline __attribute__((always_inline)) {
|
---|
165 | void ?{}( Time & time, timeval t ) with( time ) { tn = (int64_t)t.tv_sec * TIMEGRAN + t.tv_usec * 1000; }
|
---|
166 | void ?{}( Time & time, timespec t ) with( time ) { tn = (int64_t)t.tv_sec * TIMEGRAN + t.tv_nsec; }
|
---|
167 |
|
---|
168 | Time ?=?( Time & time, __attribute__((unused)) zero_t ) { return time{ 0 }; }
|
---|
169 | Time ?=?( Time & time, timeval t ) with( time ) {
|
---|
170 | tn = (int64_t)t.tv_sec * TIMEGRAN + t.tv_usec * (TIMEGRAN / 1_000_000LL);
|
---|
171 | return time;
|
---|
172 | } // ?=?
|
---|
173 | Time ?=?( Time & time, timespec t ) with( time ) {
|
---|
174 | tn = (int64_t)t.tv_sec * TIMEGRAN + t.tv_nsec;
|
---|
175 | return time;
|
---|
176 | } // ?=?
|
---|
177 |
|
---|
178 | Time ?+?( Time & lhs, Duration rhs ) { return (Time)@{ lhs.tn + rhs.tn }; }
|
---|
179 | Time ?+?( Duration lhs, Time rhs ) { return rhs + lhs; }
|
---|
180 | Time ?+=?( Time & lhs, Duration rhs ) { lhs = lhs + rhs; return lhs; }
|
---|
181 |
|
---|
182 | Duration ?-?( Time lhs, Time rhs ) { return (Duration)@{ lhs.tn - rhs.tn }; }
|
---|
183 | Time ?-?( Time lhs, Duration rhs ) { return (Time)@{ lhs.tn - rhs.tn }; }
|
---|
184 | Time ?-=?( Time & lhs, Duration rhs ) { lhs = lhs - rhs; return lhs; }
|
---|
185 | bool ?==?( Time lhs, Time rhs ) { return lhs.tn == rhs.tn; }
|
---|
186 | bool ?!=?( Time lhs, Time rhs ) { return lhs.tn != rhs.tn; }
|
---|
187 | bool ?<?( Time lhs, Time rhs ) { return lhs.tn < rhs.tn; }
|
---|
188 | bool ?<=?( Time lhs, Time rhs ) { return lhs.tn <= rhs.tn; }
|
---|
189 | bool ?>?( Time lhs, Time rhs ) { return lhs.tn > rhs.tn; }
|
---|
190 | bool ?>=?( Time lhs, Time rhs ) { return lhs.tn >= rhs.tn; }
|
---|
191 |
|
---|
192 | int64_t ?`ns( Time t ) { return t.tn; }
|
---|
193 | } // distribution
|
---|
194 |
|
---|
195 | char * yy_mm_dd( Time time, char * buf );
|
---|
196 | static inline __attribute__((always_inline)) char * ?`ymd( Time time, char * buf ) { // short form
|
---|
197 | return yy_mm_dd( time, buf );
|
---|
198 | } // ymd
|
---|
199 |
|
---|
200 | char * mm_dd_yy( Time time, char * buf );
|
---|
201 | static inline __attribute__((always_inline)) char * ?`mdy( Time time, char * buf ) { // short form
|
---|
202 | return mm_dd_yy( time, buf );
|
---|
203 | } // mdy
|
---|
204 |
|
---|
205 | char * dd_mm_yy( Time time, char * buf );
|
---|
206 | static inline __attribute__((always_inline)) char * ?`dmy( Time time, char * buf ) { // short form
|
---|
207 | return dd_mm_yy( time, buf );;
|
---|
208 | } // dmy
|
---|
209 |
|
---|
210 | size_t strftime( char buf[], size_t size, const char fmt[], Time time );
|
---|
211 |
|
---|
212 | //------------------------- timeval (cont) -------------------------
|
---|
213 |
|
---|
214 | static inline __attribute__((always_inline)) void ?{}( timeval & t, Time time ) with( t, time ) {
|
---|
215 | tv_sec = tn / TIMEGRAN; // seconds
|
---|
216 | tv_usec = tn % TIMEGRAN / (TIMEGRAN / 1_000_000LL); // microseconds
|
---|
217 | } // ?{}
|
---|
218 |
|
---|
219 | //------------------------- timespec (cont) -------------------------
|
---|
220 |
|
---|
221 | static inline __attribute__((always_inline)) void ?{}( timespec & t, Time time ) with( t, time ) {
|
---|
222 | tv_sec = tn / TIMEGRAN; // seconds
|
---|
223 | tv_nsec = tn % TIMEGRAN; // nanoseconds
|
---|
224 | } // ?{}
|
---|
225 |
|
---|
226 | // Local Variables: //
|
---|
227 | // mode: c //
|
---|
228 | // tab-width: 4 //
|
---|
229 | // End: //
|
---|