source: src/libcfa/time @ 7ca8441c

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumwith_gc
Last change on this file since 7ca8441c was 82df430, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

formatting

  • Property mode set to 100644
File size: 10.4 KB
Line 
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 Apr 14 17:48:23 2018
13// Update Count     : 636
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
22extern "C" {
23#include <sys/time.h>                                                                   // timeval
24}
25#include <time_t.h>                                                                             // Duration/Time types
26
27enum { TIMEGRAN = 1_000_000_000LL };                                    // nanosecond granularity, except for timeval
28
29
30//######################### Duration #########################
31
32static inline Duration ?=?( Duration & dur, zero_t ) { return dur{ 0 }; }
33
34static inline Duration +?( Duration rhs ) with( rhs ) { return (Duration)@{ +tv }; }
35static inline Duration ?+?( Duration & lhs, Duration rhs ) { return (Duration)@{ lhs.tv + rhs.tv }; }
36static inline Duration ?+=?( Duration & lhs, Duration rhs ) { lhs = lhs + rhs; return lhs; }
37
38static inline Duration -?( Duration rhs ) with( rhs ) { return (Duration)@{ -tv }; }
39static inline Duration ?-?( Duration & lhs, Duration rhs ) { return (Duration)@{ lhs.tv - rhs.tv }; }
40static inline Duration ?-=?( Duration & lhs, Duration rhs ) { lhs = lhs - rhs; return lhs; }
41
42static inline Duration ?*?( Duration lhs, int64_t rhs ) { return (Duration)@{ lhs.tv * rhs }; }
43static inline Duration ?*?( int64_t lhs, Duration rhs ) { return (Duration)@{ lhs * rhs.tv }; }
44static inline Duration ?*=?( Duration & lhs, int64_t rhs ) { lhs = lhs * rhs; return lhs; }
45
46static inline int64_t ?/?( Duration lhs, Duration rhs ) { return lhs.tv / rhs.tv; }
47static inline Duration ?/?( Duration lhs, int64_t rhs ) { return (Duration)@{ lhs.tv / rhs }; }
48static inline Duration ?/=?( Duration & lhs, int64_t rhs ) { lhs = lhs / rhs; return lhs; }
49static inline double div( Duration lhs, Duration rhs ) { return (double)lhs.tv / (double)rhs.tv; }
50
51static inline Duration ?%?( Duration lhs, Duration rhs ) { return (Duration)@{ lhs.tv % rhs.tv }; }
52static inline Duration ?%=?( Duration & lhs, Duration rhs ) { lhs = lhs % rhs; return lhs; }
53
54static inline _Bool ?==?( Duration lhs, Duration rhs ) { return lhs.tv == rhs.tv; }
55static inline _Bool ?!=?( Duration lhs, Duration rhs ) { return lhs.tv != rhs.tv; }
56static inline _Bool ?<? ( Duration lhs, Duration rhs ) { return lhs.tv <  rhs.tv; }
57static inline _Bool ?<=?( Duration lhs, Duration rhs ) { return lhs.tv <= rhs.tv; }
58static inline _Bool ?>? ( Duration lhs, Duration rhs ) { return lhs.tv >  rhs.tv; }
59static inline _Bool ?>=?( Duration lhs, Duration rhs ) { return lhs.tv >= rhs.tv; }
60
61static inline _Bool ?==?( Duration lhs, zero_t ) { return lhs.tv == 0; }
62static inline _Bool ?!=?( Duration lhs, zero_t ) { return lhs.tv != 0; }
63static inline _Bool ?<? ( Duration lhs, zero_t ) { return lhs.tv <  0; }
64static inline _Bool ?<=?( Duration lhs, zero_t ) { return lhs.tv <= 0; }
65static inline _Bool ?>? ( Duration lhs, zero_t ) { return lhs.tv >  0; }
66static inline _Bool ?>=?( Duration lhs, zero_t ) { return lhs.tv >= 0; }
67
68static inline Duration abs( Duration rhs ) { return rhs.tv >= 0 ? rhs : -rhs; }
69
70static inline Duration ?`ns( int64_t nsec ) { return (Duration)@{ nsec }; }
71static inline Duration ?`us( int64_t usec ) { return (Duration)@{ usec * (TIMEGRAN / 1_000_000LL) }; }
72static inline Duration ?`ms( int64_t msec ) { return (Duration)@{ msec * (TIMEGRAN / 1_000LL) }; }
73static inline Duration ?`s( int64_t sec ) { return (Duration)@{ sec * TIMEGRAN }; }
74static inline Duration ?`s( double sec ) { return (Duration)@{ sec * TIMEGRAN }; }
75static inline Duration ?`m( int64_t min ) { return (Duration)@{ min * (60LL * TIMEGRAN) }; }
76static inline Duration ?`m( double min ) { return (Duration)@{ min * (60LL * TIMEGRAN) }; }
77static inline Duration ?`h( int64_t hours ) { return (Duration)@{ hours * (60LL * 60LL * TIMEGRAN) }; }
78static inline Duration ?`h( double hours ) { return (Duration)@{ hours * (60LL * 60LL * TIMEGRAN) }; }
79static inline Duration ?`d( int64_t days ) { return (Duration)@{ days * (24LL * 60LL * 60LL * TIMEGRAN) }; }
80static inline Duration ?`d( double days ) { return (Duration)@{ days * (24LL * 60LL * 60LL * TIMEGRAN) }; }
81static inline Duration ?`w( int64_t weeks ) { return (Duration)@{ weeks * (7LL * 24LL * 60LL * 60LL * TIMEGRAN) }; }
82static inline Duration ?`w( double weeks ) { return (Duration)@{ weeks * (7LL * 24LL * 60LL * 60LL * TIMEGRAN) }; }
83
84static inline int64_t ?`ns( Duration dur ) { return dur.tv; }
85static inline int64_t ?`us( Duration dur ) { return dur.tv / (TIMEGRAN / 1_000_000LL); }
86static inline int64_t ?`ms( Duration dur ) { return dur.tv / (TIMEGRAN / 1_000LL); }
87static inline int64_t ?`s( Duration dur ) { return dur.tv / TIMEGRAN; }
88static inline int64_t ?`m( Duration dur ) { return dur.tv / (60LL * TIMEGRAN); }
89static inline int64_t ?`h( Duration dur ) { return dur.tv / (60LL * 60LL * TIMEGRAN); }
90static inline int64_t ?`d( Duration dur ) { return dur.tv / (24LL * 60LL * 60LL * TIMEGRAN); }
91static inline int64_t ?`w( Duration dur ) { return dur.tv / (7LL * 24LL * 60LL * 60LL * TIMEGRAN); }
92
93
94//######################### C timeval #########################
95
96static inline void ?{}( timeval & t ) {}
97static inline void ?{}( timeval & t, time_t sec, suseconds_t usec ) { t.tv_sec = sec; t.tv_usec = usec; }
98static inline void ?{}( timeval & t, time_t sec ) { t{ sec, 0 }; }
99static inline void ?{}( timeval & t, zero_t ) { t{ 0, 0 }; }
100static inline timeval ?=?( timeval & t, zero_t ) { return t{ 0 }; }
101static inline timeval ?+?( timeval & lhs, timeval rhs ) { return (timeval)@{ lhs.tv_sec + rhs.tv_sec, lhs.tv_usec + rhs.tv_usec }; }
102static inline timeval ?-?( timeval & lhs, timeval rhs ) { return (timeval)@{ lhs.tv_sec - rhs.tv_sec, lhs.tv_usec - rhs.tv_usec }; }
103static inline _Bool ?==?( timeval lhs, timeval rhs ) { return lhs.tv_sec == rhs.tv_sec && lhs.tv_usec == rhs.tv_usec; }
104static inline _Bool ?!=?( timeval lhs, timeval rhs ) { return lhs.tv_sec != rhs.tv_sec || lhs.tv_usec != rhs.tv_usec; }
105
106
107//######################### C timespec #########################
108
109static inline void ?{}( timespec & t ) {}
110static inline void ?{}( timespec & t, time_t sec, __syscall_slong_t nsec ) { t.tv_sec = sec; t.tv_nsec = nsec; }
111static inline void ?{}( timespec & t, time_t sec ) { t{ sec, 0}; }
112static inline void ?{}( timespec & t, zero_t ) { t{ 0, 0 }; }
113static inline timespec ?=?( timespec & t, zero_t ) { return t{ 0 }; }
114static inline timespec ?+?( timespec & lhs, timespec rhs ) { return (timespec)@{ lhs.tv_sec + rhs.tv_sec, lhs.tv_nsec + rhs.tv_nsec }; }
115static inline timespec ?-?( timespec & lhs, timespec rhs ) { return (timespec)@{ lhs.tv_sec - rhs.tv_sec, lhs.tv_nsec - rhs.tv_nsec }; }
116static inline _Bool ?==?( timespec lhs, timespec rhs ) { return lhs.tv_sec == rhs.tv_sec && lhs.tv_nsec == rhs.tv_nsec; }
117static inline _Bool ?!=?( timespec lhs, timespec rhs ) { return lhs.tv_sec != rhs.tv_sec || lhs.tv_nsec != rhs.tv_nsec; }
118
119
120//######################### C itimerval #########################
121
122static inline void ?{}( itimerval & itv, Duration alarm ) with( itv ) {
123        // itimerval contains durations but but uses time data-structure timeval.
124        it_value{ alarm`s, (alarm % 1`s)`us };                          // seconds, microseconds
125        it_interval{ 0 };                                                                       // 0 seconds, 0 microseconds
126} // itimerval
127
128static inline void ?{}( itimerval & itv, Duration alarm, Duration interval ) with( itv ) {
129        // itimerval contains durations but but uses time data-structure timeval.
130        it_value{ alarm`s, (alarm % 1`s)`us };                          // seconds, microseconds
131        it_interval{ interval`s, interval`us };                         // seconds, microseconds
132} // itimerval
133
134
135//######################### Time #########################
136
137void ?{}( Time & time, int year, int month = 0, int day = 0, int hour = 0, int min = 0, int sec = 0, int nsec = 0 );
138static inline Time ?=?( Time & time, zero_t ) { return time{ 0 }; }
139
140static inline void ?{}( Time & time, timeval t ) with( time ) { tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_usec * 1000; }
141static inline Time ?=?( Time & time, timeval t ) with( time ) {
142        tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_usec * (TIMEGRAN / 1_000_000LL);
143        return time;
144} // ?=?
145
146static inline void ?{}( Time & time, timespec t ) with( time ) { tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_nsec; }
147static inline Time ?=?( Time & time, timespec t ) with( time ) {
148        tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_nsec;
149        return time;
150} // ?=?
151
152static inline Time ?+?( Time & lhs, Duration rhs ) { return (Time)@{ lhs.tv + rhs.tv }; }
153static inline Time ?+?( Duration lhs, Time rhs ) { return rhs + lhs; }
154static inline Time ?+=?( Time & lhs, Duration rhs ) { lhs = lhs + rhs; return lhs; }
155
156static inline Duration ?-?( Time lhs, Time rhs ) { return (Duration)@{ lhs.tv - rhs.tv }; }
157static inline Time ?-?( Time lhs, Duration rhs ) { return (Time)@{ lhs.tv - rhs.tv }; }
158static inline Time ?-=?( Time & lhs, Duration rhs ) { lhs = lhs - rhs; return lhs; }
159static inline _Bool ?==?( Time lhs, Time rhs ) { return lhs.tv == rhs.tv; }
160static inline _Bool ?!=?( Time lhs, Time rhs ) { return lhs.tv != rhs.tv; }
161static inline _Bool ?<?( Time lhs, Time rhs ) { return lhs.tv < rhs.tv; }
162static inline _Bool ?<=?( Time lhs, Time rhs ) { return lhs.tv <= rhs.tv; }
163static inline _Bool ?>?( Time lhs, Time rhs ) { return lhs.tv > rhs.tv; }
164static inline _Bool ?>=?( Time lhs, Time rhs ) { return lhs.tv >= rhs.tv; }
165
166char * yy_mm_dd( Time time, char * buf );
167static inline char * ?`ymd( Time time, char * buf ) {   // short form
168        return yy_mm_dd( time, buf );
169} // ymd
170
171char * mm_dd_yy( Time time, char * buf );
172static inline char * ?`mdy( Time time, char * buf ) {   // short form
173        return mm_dd_yy( time, buf );
174} // mdy
175
176char * dd_mm_yy( Time time, char * buf );
177static inline char * ?`dmy( Time time, char * buf ) {   // short form
178        return dd_mm_yy( time, buf );;
179} // dmy
180
181size_t strftime( char * buf, size_t size, const char * fmt, Time time );
182
183//------------------------- timeval (cont) -------------------------
184
185static inline void ?{}( timeval & t, Time time ) with( t, time ) {
186        tv_sec = tv / TIMEGRAN;                                                         // seconds
187        tv_usec = tv % TIMEGRAN / (TIMEGRAN / 1_000_000LL);     // microseconds
188} // ?{}
189
190//------------------------- timespec (cont) -------------------------
191
192static inline void ?{}( timespec & t, Time time ) with( t, time ) {
193        tv_sec = tv / TIMEGRAN;                                                         // seconds
194        tv_nsec = tv % TIMEGRAN;                                                        // nanoseconds
195} // ?{}
196
197// Local Variables: //
198// mode: c //
199// tab-width: 4 //
200// End: //
Note: See TracBrowser for help on using the repository browser.