source: libcfa/src/time.hfa @ 6c5d92f

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 6c5d92f was 89c2a77b, checked in by Peter A. Buhr <pabuhr@…>, 3 years ago

add conversion of timeval and timespec to Duration

  • Property mode set to 100644
File size: 11.0 KB
RevLine 
[b1a4300]1//
[2a84d06d]2// Cforall Version 1.0.0 Copyright (C) 2018 University of Waterloo
[6ecc079]3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
[b1a4300]6//
7// time --
8//
[6ecc079]9// Author           : Peter A. Buhr
10// Created On       : Wed Mar 14 23:18:57 2018
11// Last Modified By : Peter A. Buhr
[89c2a77b]12// Last Modified On : Wed Apr 14 09:30:30 2021
13// Update Count     : 664
[b1a4300]14//
[6ecc079]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
[2a84d06d]21#include <time.h>                                                                               // timespec
22#include <sys/time.h>                                                                   // timeval
[461eed2]23#include <time_t.hfa>                                                                   // Duration/Time types
[6ecc079]24
[273cde6]25enum { TIMEGRAN = 1_000_000_000LL };                                    // nanosecond granularity, except for timeval
[6ecc079]26
27//######################### Duration #########################
28
[bbe1a87]29static inline {
[ffe2fad]30        Duration ?=?( Duration & dur, __attribute__((unused)) zero_t ) { return dur{ 0 }; }
[bbe1a87]31
[89c2a77b]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
[e0c235c]44        Duration +?( Duration rhs ) with( rhs ) { return (Duration)@{ +tn }; }
45        Duration ?+?( Duration & lhs, Duration rhs ) { return (Duration)@{ lhs.tn + rhs.tn }; }
[bbe1a87]46        Duration ?+=?( Duration & lhs, Duration rhs ) { lhs = lhs + rhs; return lhs; }
47
[e0c235c]48        Duration -?( Duration rhs ) with( rhs ) { return (Duration)@{ -tn }; }
49        Duration ?-?( Duration & lhs, Duration rhs ) { return (Duration)@{ lhs.tn - rhs.tn }; }
[bbe1a87]50        Duration ?-=?( Duration & lhs, Duration rhs ) { lhs = lhs - rhs; return lhs; }
51
[e0c235c]52        Duration ?*?( Duration lhs, int64_t rhs ) { return (Duration)@{ lhs.tn * rhs }; }
53        Duration ?*?( int64_t lhs, Duration rhs ) { return (Duration)@{ lhs * rhs.tn }; }
[bbe1a87]54        Duration ?*=?( Duration & lhs, int64_t rhs ) { lhs = lhs * rhs; return lhs; }
55
[e0c235c]56        int64_t ?/?( Duration lhs, Duration rhs ) { return lhs.tn / rhs.tn; }
57        Duration ?/?( Duration lhs, int64_t rhs ) { return (Duration)@{ lhs.tn / rhs }; }
[bbe1a87]58        Duration ?/=?( Duration & lhs, int64_t rhs ) { lhs = lhs / rhs; return lhs; }
[e0c235c]59        double div( Duration lhs, Duration rhs ) { return (double)lhs.tn / (double)rhs.tn; }
[bbe1a87]60
[e0c235c]61        Duration ?%?( Duration lhs, Duration rhs ) { return (Duration)@{ lhs.tn % rhs.tn }; }
[bbe1a87]62        Duration ?%=?( Duration & lhs, Duration rhs ) { lhs = lhs % rhs; return lhs; }
63
[e0c235c]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; }
[bbe1a87]70
[e0c235c]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; }
[bbe1a87]77
[e0c235c]78        Duration abs( Duration rhs ) { return rhs.tn >= 0 ? rhs : -rhs; }
[bbe1a87]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
[e0c235c]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
[030653a]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
[e0c235c]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;}
[bbe1a87]114} // distribution
[643c6b9]115
116//######################### C timeval #########################
[2a84d06d]117
[bbe1a87]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 }; }
[ffe2fad]122        void ?{}( timeval & t, __attribute__((unused)) zero_t ) { t{ 0, 0 }; }
[6ecc079]123
[ffe2fad]124        timeval ?=?( timeval & t, __attribute__((unused)) zero_t ) { return t{ 0 }; }
[461eed2]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 }; }
[93c2e0a]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; }
[bbe1a87]129} // distribution
[6ecc079]130
[643c6b9]131//######################### C timespec #########################
[6ecc079]132
[bbe1a87]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}; }
[ffe2fad]137        void ?{}( timespec & t, __attribute__((unused)) zero_t ) { t{ 0, 0 }; }
[6ecc079]138
[ffe2fad]139        timespec ?=?( timespec & t, __attribute__((unused)) zero_t ) { return t{ 0 }; }
[461eed2]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 }; }
[93c2e0a]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; }
[bbe1a87]144} // distribution
[6ecc079]145
[8ad6533]146//######################### C itimerval #########################
147
[bbe1a87]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
[8ad6533]154
[bbe1a87]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
[8ad6533]161
[643c6b9]162//######################### Time #########################
[6ecc079]163
[e0c235c]164void ?{}( Time & time, int year, int month = 1, int day = 1, int hour = 0, int min = 0, int sec = 0, int64_t nsec = 0 );
[bbe1a87]165static inline {
[ffe2fad]166        Time ?=?( Time & time, __attribute__((unused)) zero_t ) { return time{ 0 }; }
[bbe1a87]167
[e0c235c]168        void ?{}( Time & time, timeval t ) with( time ) { tn = (int64_t)t.tv_sec * TIMEGRAN + t.tv_usec * 1000; }
[bbe1a87]169        Time ?=?( Time & time, timeval t ) with( time ) {
[e0c235c]170                tn = (int64_t)t.tv_sec * TIMEGRAN + t.tv_usec * (TIMEGRAN / 1_000_000LL);
[bbe1a87]171                return time;
172        } // ?=?
173
[e0c235c]174        void ?{}( Time & time, timespec t ) with( time ) { tn = (int64_t)t.tv_sec * TIMEGRAN + t.tv_nsec; }
[bbe1a87]175        Time ?=?( Time & time, timespec t ) with( time ) {
[e0c235c]176                tn = (int64_t)t.tv_sec * TIMEGRAN + t.tv_nsec;
[bbe1a87]177                return time;
178        } // ?=?
179
[e0c235c]180        Time ?+?( Time & lhs, Duration rhs ) { return (Time)@{ lhs.tn + rhs.tn }; }
[bbe1a87]181        Time ?+?( Duration lhs, Time rhs ) { return rhs + lhs; }
182        Time ?+=?( Time & lhs, Duration rhs ) { lhs = lhs + rhs; return lhs; }
183
[e0c235c]184        Duration ?-?( Time lhs, Time rhs ) { return (Duration)@{ lhs.tn - rhs.tn }; }
185        Time ?-?( Time lhs, Duration rhs ) { return (Time)@{ lhs.tn - rhs.tn }; }
[bbe1a87]186        Time ?-=?( Time & lhs, Duration rhs ) { lhs = lhs - rhs; return lhs; }
[e0c235c]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; }
[bbe1a87]195} // distribution
[6ecc079]196
[2a84d06d]197char * yy_mm_dd( Time time, char * buf );
[643c6b9]198static inline char * ?`ymd( Time time, char * buf ) {   // short form
[2a84d06d]199        return yy_mm_dd( time, buf );
200} // ymd
201
202char * mm_dd_yy( Time time, char * buf );
[643c6b9]203static inline char * ?`mdy( Time time, char * buf ) {   // short form
[2a84d06d]204        return mm_dd_yy( time, buf );
205} // mdy
206
207char * dd_mm_yy( Time time, char * buf );
[643c6b9]208static inline char * ?`dmy( Time time, char * buf ) {   // short form
[2a84d06d]209        return dd_mm_yy( time, buf );;
210} // dmy
211
[e3fea42]212size_t strftime( char buf[], size_t size, const char fmt[], Time time );
[2a84d06d]213
[273cde6]214//------------------------- timeval (cont) -------------------------
215
216static inline void ?{}( timeval & t, Time time ) with( t, time ) {
[e0c235c]217        tv_sec = tn / TIMEGRAN;                                                         // seconds
218        tv_usec = tn % TIMEGRAN / (TIMEGRAN / 1_000_000LL);     // microseconds
[273cde6]219} // ?{}
220
221//------------------------- timespec (cont) -------------------------
222
223static inline void ?{}( timespec & t, Time time ) with( t, time ) {
[e0c235c]224        tv_sec = tn / TIMEGRAN;                                                         // seconds
225        tv_nsec = tn % TIMEGRAN;                                                        // nanoseconds
[273cde6]226} // ?{}
227
[6ecc079]228// Local Variables: //
229// mode: c //
230// tab-width: 4 //
231// End: //
Note: See TracBrowser for help on using the repository browser.