source: libcfa/src/time.hfa @ a8a3485

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since a8a3485 was 030653a, checked in by Peter A. Buhr <pabuhr@…>, 4 years ago

add unit conversions from duration to double

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