Changeset 2a84d06d for src/libcfa/time


Ignore:
Timestamp:
Mar 27, 2018, 5:22:58 PM (4 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
aaron-thesis, arm-eh, 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, with_gc
Children:
43725bd
Parents:
af1ed1ad
Message:

second draft of time package and incorporation into runtime kernel

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/libcfa/time

    raf1ed1ad r2a84d06d  
    11//
    2 // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
     2// Cforall Version 1.0.0 Copyright (C) 2018 University of Waterloo
    33//
    44// The contents of this file are covered under the licence agreement in the
    55// file "LICENCE" distributed with Cforall.
    66//
    7 // calendar --
     7// time --
    88//
    99// Author           : Peter A. Buhr
    1010// Created On       : Wed Mar 14 23:18:57 2018
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Mar 22 17:11:19 2018
    13 // Update Count     : 495
     12// Last Modified On : Tue Mar 27 16:37:37 2018
     13// Update Count     : 564
    1414//
    1515
     
    1919// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0355r5.html#refcc
    2020
    21 #include <time.h>
     21#include <time.h>                                                                               // timespec
    2222extern "C" {
    23 #include <sys/time.h>
    24 int snprintf( char * buf, size_t size, const char * fmt, ... );
     23#include <sys/time.h>                                                                   // timeval
    2524}
    26 #include <fstream>
    27 
    28 enum {
    29         CLOCKGRAN = 15_000_000L,                                                        // ALWAYS in nanoseconds, MUST BE less than 1 second
    30         TIMEGRAN = 1_000_000_000L                                                       // nanosecond granularity, except for timeval
    31 };
    32 
    33 
    34 #if defined( REALTIME_POSIX )
    35 #define tv_XSEC tv_nsec
    36 #else
    37 #define tv_XSEC tv_usec
    38 #endif
    39 
    40 
    41 #if defined( __linux__ )
    42 // fake a few things
    43 #define CLOCK_REALTIME  0                                                               // real (clock on the wall) time
    44 #endif
    45 
    46 // conversions for existing time types
     25#include <iostream>                                                                             // istype/ostype
     26
     27enum { TIMEGRAN = 1_000_000_000L };                                             // nanosecond granularity, except for timeval
     28
     29
     30//######################### timeval #########################
     31
    4732static inline void ?{}( timeval & t ) {}
    4833static inline void ?{}( timeval & t, time_t sec ) { t.tv_sec = sec; t.tv_usec = 0; }
    4934static inline void ?{}( timeval & t, time_t sec, suseconds_t usec ) { t.tv_sec = sec; t.tv_usec = usec; }
     35static inline void ?{}( timeval & t, zero_t ) { t.tv_sec = 0; t.tv_usec = 0; }
     36static inline timeval ?=?( timeval & t, zero_t ) { return t{ 0 }; }
     37static inline timeval ?+?( timeval & lhs, timeval rhs ) { return (timeval)@{ lhs.tv_sec + rhs.tv_sec, lhs.tv_usec + rhs.tv_usec }; }
     38static inline timeval ?-?( timeval & lhs, timeval rhs ) { return (timeval)@{ lhs.tv_sec - rhs.tv_sec, lhs.tv_usec - rhs.tv_usec }; }
     39static inline _Bool ?==?( timeval lhs, timeval rhs ) { return lhs.tv_sec == rhs.tv_sec && lhs.tv_usec == rhs.tv_usec; }
     40static inline _Bool ?!=?( timeval lhs, timeval rhs ) { return lhs.tv_sec != rhs.tv_sec || lhs.tv_usec != rhs.tv_usec; }
     41
     42
     43//######################### timespec #########################
    5044
    5145static inline void ?{}( timespec & t ) {}
    5246static inline void ?{}( timespec & t, time_t sec ) { t.tv_sec = sec; t.tv_nsec = 0; }
    5347static inline void ?{}( timespec & t, time_t sec, __syscall_slong_t nsec ) { t.tv_sec = sec; t.tv_nsec = nsec; }
     48static inline void ?{}( timespec & t, zero_t ) { t.tv_sec = 0; t.tv_nsec = 0; }
     49static inline timespec ?=?( timespec & t, zero_t ) { return t{ 0 }; }
     50static inline timespec ?+?( timespec & lhs, timespec rhs ) { return (timespec)@{ lhs.tv_sec + rhs.tv_sec, lhs.tv_nsec + rhs.tv_nsec }; }
     51static inline timespec ?-?( timespec & lhs, timespec rhs ) { return (timespec)@{ lhs.tv_sec - rhs.tv_sec, lhs.tv_nsec - rhs.tv_nsec }; }
     52static inline _Bool ?==?( timespec lhs, timespec rhs ) { return lhs.tv_sec == rhs.tv_sec && lhs.tv_nsec == rhs.tv_nsec; }
     53static inline _Bool ?!=?( timespec lhs, timespec rhs ) { return lhs.tv_sec != rhs.tv_sec || lhs.tv_nsec != rhs.tv_nsec; }
     54
     55
     56//######################### C time #########################
    5457
    5558static inline char * ctime( time_t tp ) { char * buf = ctime( &tp ); buf[24] = '\0'; return buf; }
     
    6063static inline tm * localtime_r( time_t tp, tm * result ) { return localtime_r( &tp, result ); }
    6164
    62 
    6365//######################### Duration #########################
    6466
     
    6971static inline void ?{}( Duration & dur ) with( dur ) { tv = 0; }
    7072static inline void ?{}( Duration & dur, Duration d ) with( dur ) { tv = d.tv; }
     73static inline void ?{}( Duration & dur, zero_t ) with( dur ) { tv = 0; }
     74static inline Duration ?=?( Duration & dur, zero_t ) { return dur{ 0 }; }
    7175
    7276static inline void ?{}( Duration & dur, timeval t ) with( dur ) {
     
    8892} // ?=? timespec
    8993
     94static inline int64_t nsecs( Duration dur ) with( dur ) { return tv; }
     95
     96static inline Duration +?( Duration rhs ) with( rhs ) { return (Duration)@{ +tv }; }
     97static inline Duration ?+?( Duration & lhs, Duration rhs ) { return (Duration)@{ lhs.tv + rhs.tv }; }
     98static inline Duration ?+=?( Duration & lhs, Duration rhs ) { lhs = lhs + rhs; return lhs; }
     99
     100static inline Duration -?( Duration rhs ) with( rhs ) { return (Duration)@{ -tv }; }
     101static inline Duration ?-?( Duration & lhs, Duration rhs ) { return (Duration)@{ lhs.tv - rhs.tv }; }
     102static inline Duration ?-=?( Duration & lhs, Duration rhs ) { lhs = lhs - rhs; return lhs; }
     103
     104static inline Duration ?*?( Duration lhs, int64_t rhs ) { return (Duration)@{ lhs.tv * rhs }; }
     105static inline Duration ?*?( int64_t lhs, Duration rhs ) { return (Duration)@{ lhs * rhs.tv }; }
     106static inline Duration ?*=?( Duration & lhs, int64_t rhs ) { lhs = lhs * rhs; return lhs; }
     107
     108static inline Duration ?/?( Duration lhs, int64_t rhs ) { return (Duration)@{ lhs.tv / rhs }; }
     109static inline int64_t ?/?( Duration lhs, Duration rhs ) { return lhs.tv / rhs.tv; }
     110static inline Duration ?/=?( Duration & lhs, int64_t rhs ) { lhs = lhs / rhs; return lhs; }
     111
     112static inline Duration ?%?( Duration lhs, Duration rhs ) { return (Duration)@{ lhs.tv % rhs.tv }; }
     113
     114static inline _Bool ?==?( Duration lhs, Duration rhs ) { return lhs.tv == rhs.tv; }
     115static inline _Bool ?!=?( Duration lhs, Duration rhs ) { return lhs.tv != rhs.tv; }
     116static inline _Bool ?<? ( Duration lhs, Duration rhs ) { return lhs.tv <  rhs.tv; }
     117static inline _Bool ?<=?( Duration lhs, Duration rhs ) { return lhs.tv <= rhs.tv; }
     118static inline _Bool ?>? ( Duration lhs, Duration rhs ) { return lhs.tv >  rhs.tv; }
     119static inline _Bool ?>=?( Duration lhs, Duration rhs ) { return lhs.tv >= rhs.tv; }
     120
     121static inline _Bool ?==?( Duration lhs, zero_t ) { return lhs.tv == 0; }
     122static inline _Bool ?!=?( Duration lhs, zero_t ) { return lhs.tv != 0; }
     123static inline _Bool ?<? ( Duration lhs, zero_t ) { return lhs.tv <  0; }
     124static inline _Bool ?<=?( Duration lhs, zero_t ) { return lhs.tv <= 0; }
     125static inline _Bool ?>? ( Duration lhs, zero_t ) { return lhs.tv >  0; }
     126static inline _Bool ?>=?( Duration lhs, zero_t ) { return lhs.tv >= 0; }
     127
     128static inline Duration abs( Duration lhs ) { return lhs.tv >= 0 ? lhs : -lhs; }
     129
     130forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype & os, Duration dur );
     131
     132static inline Duration ?`ns( int64_t nsec  ) { return (Duration)@{ nsec }; }
     133static inline Duration ?`us( int64_t usec  ) { return (Duration)@{ usec * (TIMEGRAN / 1_000_000L) }; }
     134static inline Duration ?`ms( int64_t msec  ) { return (Duration)@{ msec * (TIMEGRAN / 1_000L) }; }
     135static inline Duration ?`s ( int64_t sec   ) { return (Duration)@{ sec * TIMEGRAN }; }
     136static inline Duration ?`s ( double  sec   ) { return (Duration)@{ sec * TIMEGRAN }; }
     137static inline Duration ?`m ( int64_t min   ) { return (Duration)@{ min * (60L * TIMEGRAN) }; }
     138static inline Duration ?`m ( double  min   ) { return (Duration)@{ min * (60L * TIMEGRAN) }; }
     139static inline Duration ?`h ( int64_t hours ) { return (Duration)@{ hours * (3600L * TIMEGRAN) }; }
     140static inline Duration ?`h ( double  hours ) { return (Duration)@{ hours * (3600L * TIMEGRAN) }; }
     141static inline Duration ?`d ( int64_t days  ) { return (Duration)@{ days * (24L * 3600L * TIMEGRAN) }; }
     142static inline Duration ?`d ( double  days  ) { return (Duration)@{ days * (24L * 3600L * TIMEGRAN) }; }
     143static inline Duration ?`w ( int64_t weeks ) { return (Duration)@{ weeks * (7L * 24L * 3600L * TIMEGRAN) }; }
     144static inline Duration ?`f ( int64_t fortnight ) { return (Duration)@{ fortnight * (14L * 24L * 3600L * TIMEGRAN) }; }
     145
     146static inline int64_t ?`ns ( Duration dur ) { return dur.tv; }
     147static inline int64_t ?`us ( Duration dur ) { return dur.tv / (TIMEGRAN / 1_000_000L); }
     148static inline int64_t ?`ms ( Duration dur ) { return dur.tv / (TIMEGRAN / 1_000L); }
     149static inline int64_t ?`s  ( Duration dur ) { return dur.tv / TIMEGRAN; }
     150static inline int64_t ?`m  ( Duration dur ) { return dur.tv / (60L * TIMEGRAN); }
     151static inline int64_t ?`h  ( Duration dur ) { return dur.tv / (3600L * TIMEGRAN); }
     152static inline int64_t ?`d  ( Duration dur ) { return dur.tv / (24L * 3600L * TIMEGRAN); }
     153static inline int64_t ?`w  ( Duration dur ) { return dur.tv / (7L * 24L * 3600L * TIMEGRAN); }
     154static inline int64_t ?`f  ( Duration dur ) { return dur.tv / (14L * 24L * 3600L * TIMEGRAN); }
     155
     156//------------------------- timeval (cont) -------------------------
     157
    90158static inline void ?{}( timeval & t, Duration dur ) with( dur ) {
    91159        t.tv_sec = tv / TIMEGRAN;                                                       // seconds
    92         t.tv_usec = tv % TIMEGRAN / ( TIMEGRAN / 1000000L ); // microseconds
     160        t.tv_usec = tv % TIMEGRAN / (TIMEGRAN / 1_000_000L); // microseconds
    93161} // ?{}
     162
     163//------------------------- timespec (cont) -------------------------
    94164
    95165static inline void ?{}( timespec & t, Duration dur ) with( dur ) {
     
    98168} // Timespec
    99169
    100 static inline int64_t nsecs( Duration dur ) with( dur ) { return tv; }
    101 
    102 static inline Duration +?( Duration rhs ) with( rhs ) { return (Duration)@{ +tv }; }
    103 static inline Duration ?+?( Duration & lhs, Duration rhs ) { return (Duration)@{ lhs.tv + rhs.tv }; }
    104 static inline Duration ?+=?( Duration & lhs, Duration rhs ) { lhs = lhs + rhs; return lhs; }
    105 
    106 static inline Duration -?( Duration rhs ) with( rhs ) { return (Duration)@{ -tv }; }
    107 static inline Duration ?-?( Duration & lhs, Duration rhs ) { return (Duration)@{ lhs.tv - rhs.tv }; }
    108 static inline Duration ?-=?( Duration & lhs, Duration rhs ) { lhs = lhs - rhs; return lhs; }
    109 
    110 static inline Duration ?*?( Duration lhs, int64_t rhs ) { return (Duration)@{ lhs.tv * rhs }; }
    111 static inline Duration ?*?( int64_t lhs, Duration rhs ) { return (Duration)@{ lhs * rhs.tv }; }
    112 static inline Duration ?*=?( Duration & lhs, int64_t rhs ) { lhs = lhs * rhs; return lhs; }
    113 
    114 static inline Duration ?/?( Duration lhs, int64_t rhs ) { return (Duration)@{ lhs.tv / rhs }; }
    115 static inline int64_t ?/?( Duration lhs, Duration rhs ) { return lhs.tv / rhs.tv; }
    116 static inline Duration ?/=?( Duration & lhs, int64_t rhs ) { lhs = lhs / rhs; return lhs; }
    117 
    118 static inline Duration ?%?( Duration lhs, int64_t rhs ) { return (Duration)@{ lhs.tv % rhs }; }
    119 static inline int64_t ?%?( int64_t lhs, Duration rhs ) { return lhs % (rhs.tv / TIMEGRAN); }
    120 static inline int64_t ?%?( Duration lhs, Duration rhs ) { return lhs.tv % rhs.tv; }
    121 
    122 static inline _Bool ?==?( Duration lhs, Duration rhs ) { return lhs.tv == rhs.tv; }
    123 static inline _Bool ?!=?( Duration lhs, Duration rhs ) { return lhs.tv != rhs.tv; }
    124 static inline _Bool ?<?( Duration lhs, Duration rhs ) { return lhs.tv < rhs.tv; }
    125 static inline _Bool ?<=?( Duration lhs, Duration rhs ) { return lhs.tv <= rhs.tv; }
    126 static inline _Bool ?>?( Duration lhs, Duration rhs ) { return lhs.tv > rhs.tv; }
    127 static inline _Bool ?>=?( Duration lhs, Duration rhs ) { return lhs.tv >= rhs.tv; }
    128 
    129 static inline Duration abs( Duration lhs ) {
    130         return lhs.tv >= 0 ? lhs : -lhs;
    131 } // abs
    132 
    133 static inline forall( dtype ostype | ostream( ostype ) )
    134 ostype & ?|?( ostype & os, Duration dur ) with( dur ) {
    135         os | tv / TIMEGRAN;
    136         char buf[16];
    137         snprintf( buf, 16, "%09ld", ((tv < 0 ? -tv : tv) % TIMEGRAN) );
    138         int i;
    139         for ( i = 8; i >= 0 && buf[i] == '0' ; i -= 1 );        // find least significant digit
    140         if ( i != -1 ) { buf[i + 1] = '\0';     os | '.' | buf; }
    141         return os;
    142 }
    143 
    144 static inline Duration ?`ns( int64_t nsec ) { return (Duration)@{ nsec }; }
    145 static inline Duration ?`us( int64_t usec ) { return (Duration)@{ usec * (TIMEGRAN / 1_000l) }; }
    146 static inline Duration ?`ms( int64_t msec ) { return (Duration)@{ msec * (TIMEGRAN / 1_000_000l) }; }
    147 static inline Duration ?`s ( int64_t sec ) { return (Duration)@{ sec * TIMEGRAN }; }
    148 static inline Duration ?`s ( double  sec ) { return (Duration)@{ sec * TIMEGRAN }; }
    149 static inline Duration ?`m ( int64_t min ) { return (Duration)@{ min * (60L * TIMEGRAN) }; }
    150 static inline Duration ?`m ( double  min ) { return (Duration)@{ min * (60L * TIMEGRAN) }; }
    151 static inline Duration ?`h ( int64_t hours ) { return (Duration)@{ hours * (3600L * TIMEGRAN) }; }
    152 static inline Duration ?`h ( double  hours ) { return (Duration)@{ hours * (3600L * TIMEGRAN) }; }
    153 static inline Duration ?`d ( int64_t days ) { return (Duration)@{ days * (24L * 3600L * TIMEGRAN) }; }
    154 static inline Duration ?`d ( double  days ) { return (Duration)@{ days * (24L * 3600L * TIMEGRAN) }; }
    155 static inline Duration ?`w ( int64_t weeks ) { return (Duration)@{ weeks * (7L * 24L * 3600L * TIMEGRAN) }; }
    156 static inline Duration ?`f ( int64_t fortnight ) { return (Duration)@{ fortnight * (14L * 24L * 3600L * TIMEGRAN) }; }
    157 
    158 static inline int64_t ?`s ( Duration dur ) { return dur.tv / TIMEGRAN; }
    159 static inline int64_t ?`m ( Duration dur ) { return dur.tv / (60L * TIMEGRAN); }
    160 static inline int64_t ?`h ( Duration dur ) { return dur.tv / (3600L * TIMEGRAN); }
    161 static inline int64_t ?`d ( Duration dur ) { return dur.tv / (24L * 3600L * TIMEGRAN); }
    162 static inline int64_t ?`w ( Duration dur ) { return dur.tv / (7L * 24L * 3600L * TIMEGRAN); }
    163 static inline int64_t ?`f ( Duration dur ) { return dur.tv / (14L * 24L * 3600L * TIMEGRAN); }
    164 
    165170
    166171//######################### Time #########################
     
    171176};
    172177
    173 #ifdef __CFA_DEBUG__
    174 #define CreateFmt "Attempt to create Time( year=%d, month=%d, day=%d, hour=%d, min=%d, sec=%d, nsec=%d ), " \
    175         "which exceeds range 00:00:00 UTC, January 1, 1970 to 03:14:07 UTC, January 19, 2038."
    176 #endif // __CFA_DEBUG__
    177 
    178 void mktime( Time & time, int year, int month, int day, int hour, int min, int sec, int nsec ) with( time ) {
    179         tm tm;
    180 
    181 //      tzset();                                                                                        // initialize time global variables
    182         tm.tm_isdst = -1;                                                                       // let mktime determine if alternate timezone is in effect
    183         tm.tm_year = year - 1900;                                                       // mktime uses 1900 as its starting point
    184         tm.tm_mon = month - 1;
    185         tm.tm_mday = day;                                                                       // mktime uses range 1-31
    186         tm.tm_hour = hour;
    187         tm.tm_min = min;
    188         tm.tm_sec = sec;
    189         time_t epochsec = mktime( &tm );
    190 #ifdef __CFA_DEBUG__
    191         if ( epochsec == (time_t)-1 ) {
    192                 abort( CreateFmt, year, month, day, hour, min, sec, nsec );
    193         } // if
    194 #endif // __CFA_DEBUG__
    195         tv = (int64_t)(epochsec) * TIMEGRAN + nsec;                     // convert to nanoseconds
    196 #ifdef __CFA_DEBUG__
    197         if ( tv > 2147483647LL * TIMEGRAN ) {                           // between 00:00:00 UTC, January 1, 1970 and 03:14:07 UTC, January 19, 2038.
    198                 abort( CreateFmt, year, month, day, hour, min, sec, nsec );
    199         } // if
    200 #endif // __CFA_DEBUG__
    201 } // mktime
     178void mktime( Time & time, int year, int month, int day, int hour, int min, int sec, int nsec );
    202179
    203180static inline void ?{}( Time & t ) with( t ) {
     
    205182} // Time
    206183
    207 // These two constructors must not call mktime because it calls malloc. The malloc calls lead to recursion problems
    208 // because Time values are created from the sigalrm handler in composing the next context switch event.
    209 
    210 static inline void ?{}( Time & t, int sec ) with( t ) {
    211 #ifdef __CFA_DEBUG__
    212         if ( tv < 0 || tv > 2147483647LL ) {                            // between 00:00:00 UTC, January 1, 1970 and 03:14:07 UTC, January 19, 2038.
    213             abort( CreateFmt, 1970, 0, 0, 0, 0, sec, 0 );
    214         } // if
    215 #endif // __CFA_DEBUG__
    216         tv = (int64_t)sec * TIMEGRAN;
    217 } // Time
    218 
    219 static inline void ?{}( Time & t, int sec, int nsec ) with( t ) {
    220 #ifdef __U_DEBUG__
    221         if ( tv < 0 || tv > 2147483647LL || nsec < 0 ) {        // between 00:00:00 UTC, January 1, 1970 and 03:14:07 UTC, January 19, 2038.
    222             abort( CreateFmt, 1970, 0, 0, 0, 0, sec, nsec );
    223         } // if
    224 #endif // __U_DEBUG__
    225         tv = (int64_t)sec * TIMEGRAN + nsec;
    226 } // Time
    227 
    228 static inline void ?{}( Time & time, int min, int sec, long int nsec ) {
    229         mktime( time, 1970, 1, 1, 0, min, sec, nsec );
    230 } // Time
    231 
    232 static inline void ?{}( Time & time, int hour, int min, int sec, long int nsec ) {
    233         mktime( time, 1970, 1, 1, hour, min, sec, nsec );
    234 } // Time
    235 
    236 static inline void ?{}( Time & time, int day, int hour, int min, int sec, long int nsec ) {
    237         mktime( time, 1970, 1, day, hour, min, sec, nsec );
    238 } // Time
    239 
    240 static inline void ?{}( Time & time, int month, int day, int hour, int min, int sec, long int nsec ) {
    241         mktime( time, 1970, month, day, hour, min, sec, nsec );
    242 } // Time
    243 
    244 static inline void ?{}( Time & time, int year, int month, int day, int hour, int min, int sec, long int nsec ) {
     184static inline void ?{}( Time & time, int year, int month, int day, int hour, int min, int sec, int nsec ) {
    245185        mktime( time, year, month, day, hour, min, sec, nsec );
     186} // Time
     187
     188static inline void ?{}( Time & time, int year, int month, int day, int hour, int min, int sec ) {
     189        mktime( time, year, month, day, hour, min, sec, 0 );
     190} // Time
     191
     192static inline void ?{}( Time & time, int year, int month, int day, int hour, int min ) {
     193        mktime( time, year, month, day, hour, min, 0, 0 );
     194} // Time
     195
     196static inline void ?{}( Time & time, int year, int month, int day, int hour ) {
     197        mktime( time, year, month, day, hour, 0, 0, 0 );
     198} // Time
     199
     200static inline void ?{}( Time & time, int year, int month, int day ) {
     201        mktime( time, year, month, day, 0, 0, 0, 0 );
     202} // Time
     203
     204static inline void ?{}( Time & time, int year, int month ) {
     205        mktime( time, year, month, 0, 0, 0, 0, 0 );
     206} // Time
     207
     208static inline void ?{}( Time & time, int year ) {
     209        mktime( time, year, 0, 0, 0, 0, 0, 0 );
    246210} // Time
    247211
     
    254218} // Time
    255219
     220static inline void ?{}( Time & t, zero_t ) { t.tv = 0; }
     221static inline Time ?=?( Time & t, zero_t ) { return t{ 0 }; }
     222
    256223static inline Time ?=?( Time & time, timeval t ) with( time ) {
    257         tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_usec * 1000;
     224        tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_usec * (TIMEGRAN / 1_000_000L);
    258225        return time;
    259226} // ?=?
     
    273240        t.tv_nsec = tv % TIMEGRAN;                                                      // nanoseconds
    274241} // ?{}
    275 
    276 static inline int64_t nsec( Time time ) with( time ) { return tv; }
    277242
    278243static inline Time ?+?( Time & lhs, Duration rhs ) { return (Time)@{ lhs.tv + rhs.tv }; }
     
    290255static inline _Bool ?>=?( Time lhs, Time rhs ) { return lhs.tv >= rhs.tv; }
    291256
    292 static inline char * yymmd( Time time, char * buf ) with( time ) {
    293         tm tm;
    294         time_t s = tv / TIMEGRAN;
    295         gmtime_r( &s, &tm );
    296         snprintf( buf, 9, "%02d/%02d/%02d", tm.tm_year % 99, tm.tm_mon, tm.tm_mday );
    297         return buf;
    298 } // yymmd
    299 
    300 static inline char * mmyyd( Time time, char * buf ) with( time ) {
    301         tm tm;
    302         time_t s = tv / TIMEGRAN;
    303         gmtime_r( &s, &tm );
    304         snprintf( buf, 9, "%02d/%02d/%02d", tm.tm_mon, tm.tm_year % 99, tm.tm_mday );
    305         return buf;
    306 } // yymmd
    307 
    308 static inline char * dmmyy( Time time, char * buf ) with( time ) {
    309         tm tm;
    310         time_t s = tv / TIMEGRAN;
    311         gmtime_r( &s, &tm );
    312         snprintf( buf, 9, "%02d/%02d/%02d", tm.tm_mday, tm.tm_mon, tm.tm_year % 99 );
    313         return buf;
    314 } // yymmd
    315 
    316 static inline forall( dtype ostype | ostream( ostype ) )
    317 ostype & ?|?( ostype & os, Time time ) with( time ) {
    318         char buf[32];                                                                           // at least 26
    319         time_t s = tv / TIMEGRAN;
    320         tm tm;
    321         gmtime_r( &s, &tm );                                                            // ctime_r adjusts for timezone
    322         asctime_r( &tm, (char *)&buf );
    323         buf[24] = '\0';                                                                         // remove trailing '\n'
    324         long int ns = (tv < 0 ? -tv : tv) % TIMEGRAN;
    325         if ( ns == 0 ) {
    326                 os | buf;
    327         } else {
    328                 buf[19] = '\0';
    329                 os | buf;
    330                 char buf2[16];
    331                 snprintf( buf2, 16, "%09ld", ns );
    332                 int i;
    333                 for ( i = 8; i >= 0 && buf2[i] == '0' ; i -= 1 ); // find least significant digit
    334                 if ( i != -1 ) { buf2[i + 1] = '\0'; os | '.' | buf2; }
    335                 os | ' ' | &buf[20];
    336         } // if
    337         return os;
    338 } // ?|?
     257char * yy_mm_dd( Time time, char * buf );
     258static inline char * ?`ymd( Time time, char * buf ) {
     259        return yy_mm_dd( time, buf );
     260} // ymd
     261
     262char * mm_dd_yy( Time time, char * buf );
     263static inline char * ?`mdy( Time time, char * buf ) {
     264        return mm_dd_yy( time, buf );
     265} // mdy
     266
     267char * dd_mm_yy( Time time, char * buf );
     268static inline char * ?`dmy( Time time, char * buf ) {
     269        return dd_mm_yy( time, buf );;
     270} // dmy
     271
     272size_t strftime( char * buf, size_t size, const char * fmt, Time time );
     273
     274forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype & os, Time time );
     275
    339276
    340277//######################### Clock #########################
     
    346283};
    347284
    348 void resetClock( Clock & clk ) with( clk ) {
    349         clocktype = CLOCK_REALTIME;
     285static inline void resetClock( Clock & clk ) with( clk ) {
     286        clocktype = CLOCK_REALTIME_COARSE;
    350287} // Clock::resetClock
    351288
    352 void resetClock( Clock & clk, Duration adj ) with( clk ) {
     289static inline void resetClock( Clock & clk, Duration adj ) with( clk ) {
    353290        clocktype = -1;
    354291        Duration tz = (timeval){ timezone, 0 };
     
    356293} // resetClock
    357294
    358 void ?{}( Clock & clk ) {
     295static inline void ?{}( Clock & clk ) {
    359296        resetClock( clk );
    360297} // Clock
    361298
    362 void ?{}( Clock & clk, Duration adj ) {
     299static inline void ?{}( Clock & clk, Duration adj ) {
    363300        resetClock( clk, adj );
    364301} // Clock
    365302
    366 Duration getRes() {
     303static inline Duration getRes() {
    367304        struct timespec res;
    368         clock_getres( CLOCK_REALTIME, &res );
     305        clock_getres( CLOCK_REALTIME_COARSE, &res );
    369306        return (Duration){ res };
    370307} // getRes
    371308
    372 Time getTime() {
     309static inline Time getTimeNsec() {                                              // with nanoseconds
    373310        timespec curr;
    374311        clock_gettime( CLOCK_REALTIME_COARSE, &curr );
     
    376313} // getTime
    377314
    378 Time getTime( Clock & clk ) with( clk ) {
     315static inline Time getTime() {                                                  // without nanoseconds
     316        timespec curr;
     317        clock_gettime( CLOCK_REALTIME_COARSE, &curr );
     318        curr.tv_nsec = 0;
     319        return (Time){ curr };
     320} // getTime
     321
     322static inline Time getTime( Clock & clk ) with( clk ) {
    379323        return getTime() + offset;
    380324} // getTime
    381325
    382 Time ?()( Clock & clk ) with( clk ) {                                   // alternative syntax
     326static inline Time ?()( Clock & clk ) with( clk ) {             // alternative syntax
    383327        return getTime() + offset;
    384328} // getTime
    385329
    386 timeval getTime( Clock & clk ) {
     330static inline timeval getTime( Clock & clk ) {
    387331        return (timeval){ clk() };
    388332} // getTime
    389333
    390 tm getTime( Clock & clk ) with( clk ) {
     334static inline tm getTime( Clock & clk ) with( clk ) {
    391335        tm ret;
    392336        localtime_r( getTime( clk ).tv_sec, &ret );
     
    398342// tab-width: 4 //
    399343// End: //
    400 
Note: See TracChangeset for help on using the changeset viewer.