source: libcfa/src/time.hfa @ aec68b6

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

fix declaration ordering

  • 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
[e638266]12// Last Modified On : Wed Apr 21 06:32:31 2021
13// Update Count     : 667
[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 {
[89c2a77b]30        void ?{}( Duration & dur, timeval t ) with( dur ) { tn = (int64_t)t.tv_sec * TIMEGRAN + t.tv_usec * 1000; }
[6f6b844]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 }; }
[89c2a77b]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
[e0c235c]43        Duration +?( Duration rhs ) with( rhs ) { return (Duration)@{ +tn }; }
44        Duration ?+?( Duration & lhs, Duration rhs ) { return (Duration)@{ lhs.tn + rhs.tn }; }
[bbe1a87]45        Duration ?+=?( Duration & lhs, Duration rhs ) { lhs = lhs + rhs; return lhs; }
46
[e0c235c]47        Duration -?( Duration rhs ) with( rhs ) { return (Duration)@{ -tn }; }
48        Duration ?-?( Duration & lhs, Duration rhs ) { return (Duration)@{ lhs.tn - rhs.tn }; }
[bbe1a87]49        Duration ?-=?( Duration & lhs, Duration rhs ) { lhs = lhs - rhs; return lhs; }
50
[e0c235c]51        Duration ?*?( Duration lhs, int64_t rhs ) { return (Duration)@{ lhs.tn * rhs }; }
52        Duration ?*?( int64_t lhs, Duration rhs ) { return (Duration)@{ lhs * rhs.tn }; }
[bbe1a87]53        Duration ?*=?( Duration & lhs, int64_t rhs ) { lhs = lhs * rhs; return lhs; }
54
[e0c235c]55        int64_t ?/?( Duration lhs, Duration rhs ) { return lhs.tn / rhs.tn; }
56        Duration ?/?( Duration lhs, int64_t rhs ) { return (Duration)@{ lhs.tn / rhs }; }
[bbe1a87]57        Duration ?/=?( Duration & lhs, int64_t rhs ) { lhs = lhs / rhs; return lhs; }
[e0c235c]58        double div( Duration lhs, Duration rhs ) { return (double)lhs.tn / (double)rhs.tn; }
[bbe1a87]59
[e0c235c]60        Duration ?%?( Duration lhs, Duration rhs ) { return (Duration)@{ lhs.tn % rhs.tn }; }
[bbe1a87]61        Duration ?%=?( Duration & lhs, Duration rhs ) { lhs = lhs % rhs; return lhs; }
62
[e0c235c]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; }
[bbe1a87]69
[6f6b844]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
[e0c235c]77        Duration abs( Duration rhs ) { return rhs.tn >= 0 ? rhs : -rhs; }
[bbe1a87]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
[e0c235c]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
[030653a]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
[e0c235c]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;}
[bbe1a87]113} // distribution
[643c6b9]114
115//######################### C timeval #########################
[2a84d06d]116
[bbe1a87]117static 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 }; }
[e638266]121        void ?{}( timeval & t, __attribute__((unused)) zero_t ) { t{ 0, 0 }; }
[6ecc079]122
[ffe2fad]123        timeval ?=?( timeval & t, __attribute__((unused)) zero_t ) { return t{ 0 }; }
[461eed2]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 }; }
[93c2e0a]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; }
[bbe1a87]128} // distribution
[6ecc079]129
[643c6b9]130//######################### C timespec #########################
[6ecc079]131
[bbe1a87]132static 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}; }
[e638266]136        void ?{}( timespec & t, __attribute__((unused)) zero_t ) { t{ 0, 0 }; }
[6ecc079]137
[ffe2fad]138        timespec ?=?( timespec & t, __attribute__((unused)) zero_t ) { return t{ 0 }; }
[461eed2]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 }; }
[93c2e0a]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; }
[bbe1a87]143} // distribution
[6ecc079]144
[8ad6533]145//######################### C itimerval #########################
146
[bbe1a87]147static 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
[8ad6533]153
[bbe1a87]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
[8ad6533]160
[643c6b9]161//######################### Time #########################
[6ecc079]162
[e0c235c]163void ?{}( Time & time, int year, int month = 1, int day = 1, int hour = 0, int min = 0, int sec = 0, int64_t nsec = 0 );
[bbe1a87]164static inline {
[e0c235c]165        void ?{}( Time & time, timeval t ) with( time ) { tn = (int64_t)t.tv_sec * TIMEGRAN + t.tv_usec * 1000; }
[6f6b844]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 }; }
[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        Time ?=?( Time & time, timespec t ) with( time ) {
[e0c235c]174                tn = (int64_t)t.tv_sec * TIMEGRAN + t.tv_nsec;
[bbe1a87]175                return time;
176        } // ?=?
177
[e0c235c]178        Time ?+?( Time & lhs, Duration rhs ) { return (Time)@{ lhs.tn + rhs.tn }; }
[bbe1a87]179        Time ?+?( Duration lhs, Time rhs ) { return rhs + lhs; }
180        Time ?+=?( Time & lhs, Duration rhs ) { lhs = lhs + rhs; return lhs; }
181
[e0c235c]182        Duration ?-?( Time lhs, Time rhs ) { return (Duration)@{ lhs.tn - rhs.tn }; }
183        Time ?-?( Time lhs, Duration rhs ) { return (Time)@{ lhs.tn - rhs.tn }; }
[bbe1a87]184        Time ?-=?( Time & lhs, Duration rhs ) { lhs = lhs - rhs; return lhs; }
[e0c235c]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; }
[bbe1a87]193} // distribution
[6ecc079]194
[2a84d06d]195char * yy_mm_dd( Time time, char * buf );
[643c6b9]196static inline char * ?`ymd( Time time, char * buf ) {   // short form
[2a84d06d]197        return yy_mm_dd( time, buf );
198} // ymd
199
200char * mm_dd_yy( Time time, char * buf );
[643c6b9]201static inline char * ?`mdy( Time time, char * buf ) {   // short form
[2a84d06d]202        return mm_dd_yy( time, buf );
203} // mdy
204
205char * dd_mm_yy( Time time, char * buf );
[643c6b9]206static inline char * ?`dmy( Time time, char * buf ) {   // short form
[2a84d06d]207        return dd_mm_yy( time, buf );;
208} // dmy
209
[e3fea42]210size_t strftime( char buf[], size_t size, const char fmt[], Time time );
[2a84d06d]211
[273cde6]212//------------------------- timeval (cont) -------------------------
213
214static inline void ?{}( timeval & t, Time time ) with( t, time ) {
[e0c235c]215        tv_sec = tn / TIMEGRAN;                                                         // seconds
216        tv_usec = tn % TIMEGRAN / (TIMEGRAN / 1_000_000LL);     // microseconds
[273cde6]217} // ?{}
218
219//------------------------- timespec (cont) -------------------------
220
221static inline void ?{}( timespec & t, Time time ) with( t, time ) {
[e0c235c]222        tv_sec = tn / TIMEGRAN;                                                         // seconds
223        tv_nsec = tn % TIMEGRAN;                                                        // nanoseconds
[273cde6]224} // ?{}
225
[6ecc079]226// Local Variables: //
227// mode: c //
228// tab-width: 4 //
229// End: //
Note: See TracBrowser for help on using the repository browser.