Changeset 10a97adb for src/libcfa/time


Ignore:
Timestamp:
Apr 12, 2018, 8:53:12 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:
07b8001
Parents:
2ae8507a
Message:

reduce size of "time"

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/libcfa/time

    r2ae8507a r10a97adb  
    1010// Created On       : Wed Mar 14 23:18:57 2018
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Tue Apr 10 17:25:34 2018
    13 // Update Count     : 622
     12// Last Modified On : Thu Apr 12 16:53:35 2018
     13// Update Count     : 629
    1414//
    1515
     
    2323#include <sys/time.h>                                                                   // timeval
    2424}
    25 #include <iostream>                                                                             // istype/ostype
     25#include <time_t.h>                                                                             // Duration/Time types
    2626
    2727enum { TIMEGRAN = 1_000_000_000LL };                                    // nanosecond granularity, except for timeval
    2828
    29 #include <time_t.h>                                                                             // Duration (constructors) / Time (constructors)
    3029
    3130//######################### Duration #########################
     
    6867
    6968static inline Duration abs( Duration rhs ) { return rhs.tv >= 0 ? rhs : -rhs; }
    70 
    71 forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype & os, Duration dur );
    7269
    7370static inline Duration ?`ns( int64_t nsec ) { return (Duration)@{ nsec }; }
     
    136133
    137134
    138 //######################### C time #########################
    139 
    140 static inline char * ctime( time_t tp ) { char * buf = ctime( &tp ); buf[24] = '\0'; return buf; }
    141 static inline char * ctime_r( time_t tp, char * buf ) { ctime_r( &tp, buf ); buf[24] = '\0'; return buf; }
    142 static inline tm * gmtime( time_t tp ) { return gmtime( &tp ); }
    143 static inline tm * gmtime_r( time_t tp, tm * result ) { return gmtime_r( &tp, result ); }
    144 static inline tm * localtime( time_t tp ) { return localtime( &tp ); }
    145 static inline tm * localtime_r( time_t tp, tm * result ) { return localtime_r( &tp, result ); }
    146 
    147 
    148135//######################### Time #########################
    149136
     
    199186size_t strftime( char * buf, size_t size, const char * fmt, Time time );
    200187
    201 forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype & os, Time time );
    202 
    203188//------------------------- timeval (cont) -------------------------
    204189
     
    215200} // ?{}
    216201
    217 
    218 //######################### Clock #########################
    219 
    220 struct Clock {                                                                                  // private
    221         Duration offset;                                                                        // for virtual clock: contains offset from real-time
    222         int clocktype;                                                                          // implementation only -1 (virtual), CLOCK_REALTIME
    223 };
    224 
    225 static inline void resetClock( Clock & clk ) with( clk ) {
    226         clocktype = CLOCK_REALTIME_COARSE;
    227 } // Clock::resetClock
    228 
    229 static inline void resetClock( Clock & clk, Duration adj ) with( clk ) {
    230         clocktype = -1;
    231         offset = adj + timezone`s;                                                      // timezone (global) is (UTC - local time) in seconds
    232 } // resetClock
    233 
    234 static inline void ?{}( Clock & clk ) { resetClock( clk ); }
    235 static inline void ?{}( Clock & clk, Duration adj ) { resetClock( clk, adj ); }
    236 
    237 static inline Duration getRes() {
    238         struct timespec res;
    239         clock_getres( CLOCK_REALTIME_COARSE, &res );
    240         return ((int64_t)res.tv_sec * TIMEGRAN + res.tv_nsec)`ns;
    241 } // getRes
    242 
    243 static inline Time getTimeNsec() {                                              // with nanoseconds
    244         timespec curr;
    245         clock_gettime( CLOCK_REALTIME_COARSE, &curr );
    246         return (Time){ curr };
    247 } // getTime
    248 
    249 static inline Time getTime() {                                                  // without nanoseconds
    250         timespec curr;
    251         clock_gettime( CLOCK_REALTIME_COARSE, &curr );
    252         curr.tv_nsec = 0;
    253         return (Time){ curr };
    254 } // getTime
    255 
    256 static inline Time getTime( Clock & clk ) with( clk ) {
    257         return getTime() + offset;
    258 } // getTime
    259 
    260 static inline Time ?()( Clock & clk ) with( clk ) {             // alternative syntax
    261         return getTime() + offset;
    262 } // getTime
    263 
    264 static inline timeval getTime( Clock & clk ) {
    265         return (timeval){ clk() };
    266 } // getTime
    267 
    268 static inline tm getTime( Clock & clk ) with( clk ) {
    269         tm ret;
    270         localtime_r( getTime( clk ).tv_sec, &ret );
    271         return ret;
    272 } // getTime
    273 
    274202// Local Variables: //
    275203// mode: c //
Note: See TracChangeset for help on using the changeset viewer.