source: libcfa/src/time.hfa @ 89c2a77b

arm-ehjacob/cs343-translation
Last change on this file since 89c2a77b was 89c2a77b, checked in by Peter A. Buhr <pabuhr@…>, 4 months ago

add conversion of timeval and timespec to Duration

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