Changeset 643c6b9 for src/libcfa/time


Ignore:
Timestamp:
Apr 6, 2018, 1:24:31 PM (7 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, with_gc
Children:
d35abc2
Parents:
bc03be3
Message:

cleanup, add "div" for Duration

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/libcfa/time

    rbc03be3 r643c6b9  
    1010// Created On       : Wed Mar 14 23:18:57 2018
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Wed Apr  4 16:28:48 2018
    13 // Update Count     : 595
     12// Last Modified On : Fri Apr  6 11:15:49 2018
     13// Update Count     : 610
    1414//
    1515
     
    2828
    2929
    30 //######################### timeval #########################
     30//######################### Duration #########################
     31
     32struct Duration {                                                                               // private
     33        int64_t tv;                                                                                     // nanoseconds
     34}; // Duration
     35
     36static inline void ?{}( Duration & dur ) with( dur ) { tv = 0; }
     37static inline void ?{}( Duration & dur, Duration d ) with( dur ) { tv = d.tv; }
     38
     39static inline void ?{}( Duration & dur, zero_t ) with( dur ) { tv = 0; }
     40static inline Duration ?=?( Duration & dur, zero_t ) { return dur{ 0 }; }
     41
     42static inline Duration +?( Duration rhs ) with( rhs ) { return (Duration)@{ +tv }; }
     43static inline Duration ?+?( Duration & lhs, Duration rhs ) { return (Duration)@{ lhs.tv + rhs.tv }; }
     44static inline Duration ?+=?( Duration & lhs, Duration rhs ) { lhs = lhs + rhs; return lhs; }
     45
     46static inline Duration -?( Duration rhs ) with( rhs ) { return (Duration)@{ -tv }; }
     47static inline Duration ?-?( Duration & lhs, Duration rhs ) { return (Duration)@{ lhs.tv - rhs.tv }; }
     48static inline Duration ?-=?( Duration & lhs, Duration rhs ) { lhs = lhs - rhs; return lhs; }
     49
     50static inline Duration ?*?( Duration lhs, int64_t rhs ) { return (Duration)@{ lhs.tv * rhs }; }
     51static inline Duration ?*?( int64_t lhs, Duration rhs ) { return (Duration)@{ lhs * rhs.tv }; }
     52static inline Duration ?*=?( Duration & lhs, int64_t rhs ) { lhs = lhs * rhs; return lhs; }
     53
     54static inline int64_t ?/?( Duration lhs, Duration rhs ) { return lhs.tv / rhs.tv; }
     55static inline Duration ?/?( Duration lhs, int64_t rhs ) { return (Duration)@{ lhs.tv / rhs }; }
     56static inline Duration ?/=?( Duration & lhs, int64_t rhs ) { lhs = lhs / rhs; return lhs; }
     57static inline double div( Duration lhs, Duration rhs ) { return (double)lhs.tv / (double)rhs.tv; }
     58
     59static inline Duration ?%?( Duration lhs, Duration rhs ) { return (Duration)@{ lhs.tv % rhs.tv }; }
     60static inline Duration ?%=?( Duration & lhs, Duration rhs ) { lhs = lhs % rhs; return lhs; }
     61
     62static inline _Bool ?==?( Duration lhs, Duration rhs ) { return lhs.tv == rhs.tv; }
     63static inline _Bool ?!=?( Duration lhs, Duration rhs ) { return lhs.tv != rhs.tv; }
     64static inline _Bool ?<? ( Duration lhs, Duration rhs ) { return lhs.tv <  rhs.tv; }
     65static inline _Bool ?<=?( Duration lhs, Duration rhs ) { return lhs.tv <= rhs.tv; }
     66static inline _Bool ?>? ( Duration lhs, Duration rhs ) { return lhs.tv >  rhs.tv; }
     67static inline _Bool ?>=?( Duration lhs, Duration rhs ) { return lhs.tv >= rhs.tv; }
     68
     69static inline _Bool ?==?( Duration lhs, zero_t ) { return lhs.tv == 0; }
     70static inline _Bool ?!=?( Duration lhs, zero_t ) { return lhs.tv != 0; }
     71static inline _Bool ?<? ( Duration lhs, zero_t ) { return lhs.tv <  0; }
     72static inline _Bool ?<=?( Duration lhs, zero_t ) { return lhs.tv <= 0; }
     73static inline _Bool ?>? ( Duration lhs, zero_t ) { return lhs.tv >  0; }
     74static inline _Bool ?>=?( Duration lhs, zero_t ) { return lhs.tv >= 0; }
     75
     76static inline Duration abs( Duration rhs ) { return rhs.tv >= 0 ? rhs : -rhs; }
     77
     78forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype & os, Duration dur );
     79
     80static inline Duration ?`ns( int64_t nsec ) { return (Duration)@{ nsec }; }
     81static inline Duration ?`us( int64_t usec ) { return (Duration)@{ usec * (TIMEGRAN / 1_000_000LL) }; }
     82static inline Duration ?`ms( int64_t msec ) { return (Duration)@{ msec * (TIMEGRAN / 1_000LL) }; }
     83static inline Duration ?`s( int64_t sec ) { return (Duration)@{ sec * TIMEGRAN }; }
     84static inline Duration ?`s( double sec ) { return (Duration)@{ sec * TIMEGRAN }; }
     85static inline Duration ?`m( int64_t min ) { return (Duration)@{ min * (60LL * TIMEGRAN) }; }
     86static inline Duration ?`m( double min ) { return (Duration)@{ min * (60LL * TIMEGRAN) }; }
     87static inline Duration ?`h( int64_t hours ) { return (Duration)@{ hours * (60LL * 60LL * TIMEGRAN) }; }
     88static inline Duration ?`h( double hours ) { return (Duration)@{ hours * (60LL * 60LL * TIMEGRAN) }; }
     89static inline Duration ?`d( int64_t days ) { return (Duration)@{ days * (24LL * 60LL * 60LL * TIMEGRAN) }; }
     90static inline Duration ?`d( double days ) { return (Duration)@{ days * (24LL * 60LL * 60LL * TIMEGRAN) }; }
     91static inline Duration ?`w( int64_t weeks ) { return (Duration)@{ weeks * (7LL * 24LL * 60LL * 60LL * TIMEGRAN) }; }
     92static inline Duration ?`w( double weeks ) { return (Duration)@{ weeks * (7LL * 24LL * 60LL * 60LL * TIMEGRAN) }; }
     93
     94static inline int64_t ?`ns( Duration dur ) { return dur.tv; }
     95static inline int64_t ?`us( Duration dur ) { return dur.tv / (TIMEGRAN / 1_000_000LL); }
     96static inline int64_t ?`ms( Duration dur ) { return dur.tv / (TIMEGRAN / 1_000LL); }
     97static inline int64_t ?`s( Duration dur ) { return dur.tv / TIMEGRAN; }
     98static inline int64_t ?`m( Duration dur ) { return dur.tv / (60LL * TIMEGRAN); }
     99static inline int64_t ?`h( Duration dur ) { return dur.tv / (60LL * 60LL * TIMEGRAN); }
     100static inline int64_t ?`d( Duration dur ) { return dur.tv / (24LL * 60LL * 60LL * TIMEGRAN); }
     101static inline int64_t ?`w( Duration dur ) { return dur.tv / (7LL * 24LL * 60LL * 60LL * TIMEGRAN); }
     102
     103
     104//######################### C timeval #########################
    31105
    32106static inline void ?{}( timeval & t ) {}
     
    41115
    42116
    43 //######################### timespec #########################
     117//######################### C timespec #########################
    44118
    45119static inline void ?{}( timespec & t ) {}
     
    64138
    65139
    66 //######################### Duration #########################
    67 
    68 struct Duration {
    69         int64_t tv;                                                                                     // nanoseconds
    70 }; // Duration
    71 
    72 static inline void ?{}( Duration & dur ) with( dur ) { tv = 0; }
    73 static inline void ?{}( Duration & dur, Duration d ) with( dur ) { tv = d.tv; }
    74 static inline void ?{}( Duration & dur, zero_t ) with( dur ) { tv = 0; }
    75 static inline Duration ?=?( Duration & dur, zero_t ) { return dur{ 0 }; }
    76 
    77 #if 0
    78 static inline void ?{}( Duration & dur, timeval t ) with( dur ) {
    79         tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_usec * 1000;
    80 } // Duration
    81 
    82 static inline void ?{}( Duration & dur, timespec t ) with( dur ) {
    83         tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_nsec;
    84 } // Duration
    85 
    86 static inline Duration ?=?( Duration & dur, timeval t ) with( dur ) {
    87         tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_usec * 1000;
    88         return dur;
    89 } // ?=?
    90 
    91 static inline Duration ?=?( Duration & dur, timespec t ) with( dur ) {
    92         tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_nsec;
    93         return dur;
    94 } // ?=? timespec
    95 
    96 //------------------------- timeval (cont) -------------------------
    97 
    98 static inline void ?{}( timeval & t, Duration dur ) with( dur ) {
    99         t.tv_sec = tv / TIMEGRAN;                                                       // seconds
    100         t.tv_usec = tv % TIMEGRAN / (TIMEGRAN / 1_000_000LL); // microseconds
    101 } // ?{}
    102 
    103 //------------------------- timespec (cont) -------------------------
    104 
    105 static inline void ?{}( timespec & t, Duration dur ) with( dur ) {
    106         t.tv_sec = tv / TIMEGRAN;                                                       // seconds
    107         t.tv_nsec = tv % TIMEGRAN;                                                      // nanoseconds
    108 } // Timespec
    109 
    110 static inline int64_t nsecs( Duration dur ) with( dur ) { return tv; }
    111 #endif
    112 
    113 static inline Duration +?( Duration rhs ) with( rhs ) { return (Duration)@{ +tv }; }
    114 static inline Duration ?+?( Duration & lhs, Duration rhs ) { return (Duration)@{ lhs.tv + rhs.tv }; }
    115 static inline Duration ?+=?( Duration & lhs, Duration rhs ) { lhs = lhs + rhs; return lhs; }
    116 
    117 static inline Duration -?( Duration rhs ) with( rhs ) { return (Duration)@{ -tv }; }
    118 static inline Duration ?-?( Duration & lhs, Duration rhs ) { return (Duration)@{ lhs.tv - rhs.tv }; }
    119 static inline Duration ?-=?( Duration & lhs, Duration rhs ) { lhs = lhs - rhs; return lhs; }
    120 
    121 static inline Duration ?*?( Duration lhs, int64_t rhs ) { return (Duration)@{ lhs.tv * rhs }; }
    122 static inline Duration ?*?( int64_t lhs, Duration rhs ) { return (Duration)@{ lhs * rhs.tv }; }
    123 static inline Duration ?*=?( Duration & lhs, int64_t rhs ) { lhs = lhs * rhs; return lhs; }
    124 
    125 static inline int64_t ?/?( Duration lhs, Duration rhs ) { return lhs.tv / rhs.tv; }
    126 static inline Duration ?/?( Duration lhs, int64_t rhs ) { return (Duration)@{ lhs.tv / rhs }; }
    127 static inline Duration ?/=?( Duration & lhs, int64_t rhs ) { lhs = lhs / rhs; return lhs; }
    128 
    129 static inline Duration ?%?( Duration lhs, Duration rhs ) { return (Duration)@{ lhs.tv % rhs.tv }; }
    130 static inline Duration ?%=?( Duration & lhs, Duration rhs ) { lhs = lhs % rhs; return lhs; }
    131 
    132 static inline _Bool ?==?( Duration lhs, Duration rhs ) { return lhs.tv == rhs.tv; }
    133 static inline _Bool ?!=?( Duration lhs, Duration rhs ) { return lhs.tv != rhs.tv; }
    134 static inline _Bool ?<? ( Duration lhs, Duration rhs ) { return lhs.tv <  rhs.tv; }
    135 static inline _Bool ?<=?( Duration lhs, Duration rhs ) { return lhs.tv <= rhs.tv; }
    136 static inline _Bool ?>? ( Duration lhs, Duration rhs ) { return lhs.tv >  rhs.tv; }
    137 static inline _Bool ?>=?( Duration lhs, Duration rhs ) { return lhs.tv >= rhs.tv; }
    138 
    139 static inline _Bool ?==?( Duration lhs, zero_t ) { return lhs.tv == 0; }
    140 static inline _Bool ?!=?( Duration lhs, zero_t ) { return lhs.tv != 0; }
    141 static inline _Bool ?<? ( Duration lhs, zero_t ) { return lhs.tv <  0; }
    142 static inline _Bool ?<=?( Duration lhs, zero_t ) { return lhs.tv <= 0; }
    143 static inline _Bool ?>? ( Duration lhs, zero_t ) { return lhs.tv >  0; }
    144 static inline _Bool ?>=?( Duration lhs, zero_t ) { return lhs.tv >= 0; }
    145 
    146 static inline Duration abs( Duration rhs ) { return rhs.tv >= 0 ? rhs : -rhs; }
    147 
    148 forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype & os, Duration dur );
    149 
    150 static inline Duration ?`ns( int64_t nsec ) { return (Duration)@{ nsec }; }
    151 static inline Duration ?`us( int64_t usec ) { return (Duration)@{ usec * (TIMEGRAN / 1_000_000LL) }; }
    152 static inline Duration ?`ms( int64_t msec ) { return (Duration)@{ msec * (TIMEGRAN / 1_000LL) }; }
    153 static inline Duration ?`s ( int64_t sec ) { return (Duration)@{ sec * TIMEGRAN }; }
    154 static inline Duration ?`s ( double sec ) { return (Duration)@{ sec * TIMEGRAN }; }
    155 static inline Duration ?`m ( int64_t min ) { return (Duration)@{ min * (60LL * TIMEGRAN) }; }
    156 static inline Duration ?`m ( double min ) { return (Duration)@{ min * (60LL * TIMEGRAN) }; }
    157 static inline Duration ?`h ( int64_t hours ) { return (Duration)@{ hours * (60LL * 60LL * TIMEGRAN) }; }
    158 static inline Duration ?`h ( double hours ) { return (Duration)@{ hours * (60LL * 60LL * TIMEGRAN) }; }
    159 static inline Duration ?`d ( int64_t days ) { return (Duration)@{ days * (24LL * 60LL * 60LL * TIMEGRAN) }; }
    160 static inline Duration ?`d ( double days ) { return (Duration)@{ days * (24LL * 60LL * 60LL * TIMEGRAN) }; }
    161 static inline Duration ?`w ( int64_t weeks ) { return (Duration)@{ weeks * (7LL * 24LL * 60LL * 60LL * TIMEGRAN) }; }
    162 static inline Duration ?`w ( double weeks ) { return (Duration)@{ weeks * (7LL * 24LL * 60LL * 60LL * TIMEGRAN) }; }
    163 //static inline Duration ?`f ( int64_t fortnight ) { return (Duration)@{ fortnight * (14LL * 24LL * 60LL * 60LL * TIMEGRAN) }; }
    164 //static inline Duration ?`f ( double  fortnight ) { return (Duration)@{ fortnight * (14LL * 24LL * 60LL * 60LL * TIMEGRAN) }; }
    165 
    166 static inline int64_t ?`ns ( Duration dur ) { return dur.tv; }
    167 static inline int64_t ?`us ( Duration dur ) { return dur.tv / (TIMEGRAN / 1_000_000LL); }
    168 static inline int64_t ?`ms ( Duration dur ) { return dur.tv / (TIMEGRAN / 1_000LL); }
    169 static inline int64_t ?`s  ( Duration dur ) { return dur.tv / TIMEGRAN; }
    170 static inline int64_t ?`m  ( Duration dur ) { return dur.tv / (60LL * TIMEGRAN); }
    171 static inline int64_t ?`h  ( Duration dur ) { return dur.tv / (60LL * 60LL * TIMEGRAN); }
    172 static inline int64_t ?`d  ( Duration dur ) { return dur.tv / (24LL * 60LL * 60LL * TIMEGRAN); }
    173 static inline int64_t ?`w  ( Duration dur ) { return dur.tv / (7LL * 24LL * 60LL * 60LL * TIMEGRAN); }
    174 static inline int64_t ?`f  ( Duration dur ) { return dur.tv / (14LL * 24LL * 60LL * 60LL * TIMEGRAN); }
    175 
    176 
    177140//######################### Time #########################
    178141
    179 struct Time {
     142struct Time {                                                                                   // private
    180143        uint64_t tv;                                                                            // nanoseconds since UNIX epoch
    181144};
    182145
    183 static inline void ?{}( Time & t ) with( t ) {
    184         tv = 0;
    185 } // Time
    186 
    187 void ?{}( Time & time, int year, int month, int day, int hour, int min, int sec, int nsec );
    188 
    189 static inline void ?{}( Time & time, int year, int month, int day, int hour, int min, int sec ) {
    190         time{ year, month, day, hour, min, sec, 0 };
    191 } // Time
    192 
    193 static inline void ?{}( Time & time, int year, int month, int day, int hour, int min ) {
    194         time{ year, month, day, hour, min, 0, 0 };
    195 } // Time
    196 
    197 static inline void ?{}( Time & time, int year, int month, int day, int hour ) {
    198         time{ year, month, day, hour, 0, 0, 0 };
    199 } // Time
    200 
    201 static inline void ?{}( Time & time, int year, int month, int day ) {
    202         time{ year, month, day, 0, 0, 0, 0 };
    203 } // Time
    204 
    205 static inline void ?{}( Time & time, int year, int month ) {
    206         time{ year, month, 0, 0, 0, 0, 0 };
    207 } // Time
    208 
    209 static inline void ?{}( Time & time, int year ) {
    210         time{ year, 0, 0, 0, 0, 0, 0 };
    211 } // Time
     146static inline void ?{}( Time & t ) with( t ) { tv = 0; } // fast
     147void ?{}( Time & time, int year, int month = 0, int day = 0, int hour = 0, int min = 0, int sec = 0, int nsec = 0 ); // slow
    212148
    213149static inline void ?{}( Time & t, zero_t ) { t.tv = 0; }
     
    247183
    248184char * yy_mm_dd( Time time, char * buf );
    249 static inline char * ?`ymd( Time time, char * buf ) {
     185static inline char * ?`ymd( Time time, char * buf ) {   // short form
    250186        return yy_mm_dd( time, buf );
    251187} // ymd
    252188
    253189char * mm_dd_yy( Time time, char * buf );
    254 static inline char * ?`mdy( Time time, char * buf ) {
     190static inline char * ?`mdy( Time time, char * buf ) {   // short form
    255191        return mm_dd_yy( time, buf );
    256192} // mdy
    257193
    258194char * dd_mm_yy( Time time, char * buf );
    259 static inline char * ?`dmy( Time time, char * buf ) {
     195static inline char * ?`dmy( Time time, char * buf ) {   // short form
    260196        return dd_mm_yy( time, buf );;
    261197} // dmy
     
    282218//######################### Clock #########################
    283219
    284 struct Clock {
     220struct Clock {                                                                                  // private
    285221        Duration offset;                                                                        // for virtual clock: contains offset from real-time
    286222        int clocktype;                                                                          // implementation only -1 (virtual), CLOCK_REALTIME
     
    293229static inline void resetClock( Clock & clk, Duration adj ) with( clk ) {
    294230        clocktype = -1;
    295         offset = adj + timezone`s;                                                      // timezone is (UTC - local time) in seconds
     231        offset = adj + timezone`s;                                                      // timezone (global) is (UTC - local time) in seconds
    296232} // resetClock
    297233
Note: See TracChangeset for help on using the changeset viewer.