Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/libcfa/time

    r07b8001 r8ad6533  
    1010// Created On       : Wed Mar 14 23:18:57 2018
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Apr 13 07:51:52 2018
    13 // Update Count     : 634
     12// Last Modified On : Mon Apr  9 13:10:23 2018
     13// Update Count     : 616
    1414//
    1515
     
    2323#include <sys/time.h>                                                                   // timeval
    2424}
    25 #include <time_t.h>                                                                             // Duration/Time types
     25#include <iostream>                                                                             // istype/ostype
    2626
    2727enum { TIMEGRAN = 1_000_000_000LL };                                    // nanosecond granularity, except for timeval
     
    3030//######################### Duration #########################
    3131
     32struct Duration {                                                                               // private
     33        int64_t tv;                                                                                     // nanoseconds
     34}; // Duration
     35
     36static inline void ?{}( Duration & dur ) with( dur ) { tv = 0; }
    3237static inline void ?{}( Duration & dur, Duration d ) with( dur ) { tv = d.tv; }
    3338
     39static inline void ?{}( Duration & dur, zero_t ) with( dur ) { tv = 0; }
    3440static inline Duration ?=?( Duration & dur, zero_t ) { return dur{ 0 }; }
    3541
     
    6975
    7076static inline Duration abs( Duration rhs ) { return rhs.tv >= 0 ? rhs : -rhs; }
     77
     78forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype & os, Duration dur );
    7179
    7280static inline Duration ?`ns( int64_t nsec ) { return (Duration)@{ nsec }; }
     
    135143
    136144
     145//######################### C time #########################
     146
     147static inline char * ctime( time_t tp ) { char * buf = ctime( &tp ); buf[24] = '\0'; return buf; }
     148static inline char * ctime_r( time_t tp, char * buf ) { ctime_r( &tp, buf ); buf[24] = '\0'; return buf; }
     149static inline tm * gmtime( time_t tp ) { return gmtime( &tp ); }
     150static inline tm * gmtime_r( time_t tp, tm * result ) { return gmtime_r( &tp, result ); }
     151static inline tm * localtime( time_t tp ) { return localtime( &tp ); }
     152static inline tm * localtime_r( time_t tp, tm * result ) { return localtime_r( &tp, result ); }
     153
     154
    137155//######################### Time #########################
    138156
    139 static inline void ?{}( Time & time, Time t ) with( time ) { tv = t.tv; }
    140 void ?{}( Time & time, int year, int month = 0, int day = 0, int hour = 0, int min = 0, int sec = 0, int nsec = 0 );
    141 static inline void ?{}( Time & time, timeval t ) with( time ) { tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_usec * 1000; }
    142 
    143 static inline Time ?=?( Time & time, zero_t ) { return time{ 0 }; }
     157struct Time {                                                                                   // private
     158        uint64_t tv;                                                                            // nanoseconds since UNIX epoch
     159}; // Time
     160
     161static inline void ?{}( Time & t ) with( t ) { tv = 0; } // fast
     162void ?{}( Time & time, int year, int month = 0, int day = 0, int hour = 0, int min = 0, int sec = 0, int nsec = 0 ); // slow
     163
     164static inline void ?{}( Time & t, zero_t ) { t.tv = 0; }
     165static inline Time ?=?( Time & t, zero_t ) { return t{ 0 }; }
     166
     167static inline void ?{}( Time & time, timeval t ) with( time ) {
     168        tv = (int64_t)t.tv_sec * TIMEGRAN + t.tv_usec * 1000;
     169} // Time
    144170
    145171static inline Time ?=?( Time & time, timeval t ) with( time ) {
     
    188214size_t strftime( char * buf, size_t size, const char * fmt, Time time );
    189215
     216forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype & os, Time time );
     217
    190218//------------------------- timeval (cont) -------------------------
    191219
     
    202230} // ?{}
    203231
     232
     233//######################### Clock #########################
     234
     235struct Clock {                                                                                  // private
     236        Duration offset;                                                                        // for virtual clock: contains offset from real-time
     237        int clocktype;                                                                          // implementation only -1 (virtual), CLOCK_REALTIME
     238};
     239
     240static inline void resetClock( Clock & clk ) with( clk ) {
     241        clocktype = CLOCK_REALTIME_COARSE;
     242} // Clock::resetClock
     243
     244static inline void resetClock( Clock & clk, Duration adj ) with( clk ) {
     245        clocktype = -1;
     246        offset = adj + timezone`s;                                                      // timezone (global) is (UTC - local time) in seconds
     247} // resetClock
     248
     249static inline void ?{}( Clock & clk ) {
     250        resetClock( clk );
     251} // Clock
     252
     253static inline void ?{}( Clock & clk, Duration adj ) {
     254        resetClock( clk, adj );
     255} // Clock
     256
     257static inline Duration getRes() {
     258        struct timespec res;
     259        clock_getres( CLOCK_REALTIME_COARSE, &res );
     260        return ((int64_t)res.tv_sec * TIMEGRAN + res.tv_nsec)`ns;
     261} // getRes
     262
     263static inline Time getTimeNsec() {                                              // with nanoseconds
     264        timespec curr;
     265        clock_gettime( CLOCK_REALTIME_COARSE, &curr );
     266        return (Time){ curr };
     267} // getTime
     268
     269static inline Time getTime() {                                                  // without nanoseconds
     270        timespec curr;
     271        clock_gettime( CLOCK_REALTIME_COARSE, &curr );
     272        curr.tv_nsec = 0;
     273        return (Time){ curr };
     274} // getTime
     275
     276static inline Time getTime( Clock & clk ) with( clk ) {
     277        return getTime() + offset;
     278} // getTime
     279
     280static inline Time ?()( Clock & clk ) with( clk ) {             // alternative syntax
     281        return getTime() + offset;
     282} // getTime
     283
     284static inline timeval getTime( Clock & clk ) {
     285        return (timeval){ clk() };
     286} // getTime
     287
     288static inline tm getTime( Clock & clk ) with( clk ) {
     289        tm ret;
     290        localtime_r( getTime( clk ).tv_sec, &ret );
     291        return ret;
     292} // getTime
     293
    204294// Local Variables: //
    205295// mode: c //
Note: See TracChangeset for help on using the changeset viewer.