source: src/libcfa/clock @ 1d7b0a8

new-env
Last change on this file since 1d7b0a8 was 10a97adb, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

reduce size of "time"

  • Property mode set to 100644
File size: 2.7 KB
RevLine 
[10a97adb]1//
2// Cforall Version 1.0.0 Copyright (C) 2018 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// clock --
8//
9// Author           : Peter A. Buhr
10// Created On       : Thu Apr 12 14:36:06 2018
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Thu Apr 12 16:53:31 2018
13// Update Count     : 3
14//
15
16#include <time>
17
18
19//######################### C time #########################
20
21static inline char * ctime( time_t tp ) { char * buf = ctime( &tp ); buf[24] = '\0'; return buf; }
22static inline char * ctime_r( time_t tp, char * buf ) { ctime_r( &tp, buf ); buf[24] = '\0'; return buf; }
23static inline tm * gmtime( time_t tp ) { return gmtime( &tp ); }
24static inline tm * gmtime_r( time_t tp, tm * result ) { return gmtime_r( &tp, result ); }
25static inline tm * localtime( time_t tp ) { return localtime( &tp ); }
26static inline tm * localtime_r( time_t tp, tm * result ) { return localtime_r( &tp, result ); }
27
28
29//######################### Clock #########################
30
31struct Clock {                                                                                  // private
32        Duration offset;                                                                        // for virtual clock: contains offset from real-time
33        int clocktype;                                                                          // implementation only -1 (virtual), CLOCK_REALTIME
34};
35
36static inline void resetClock( Clock & clk ) with( clk ) {
37        clocktype = CLOCK_REALTIME_COARSE;
38} // Clock::resetClock
39
40static inline void resetClock( Clock & clk, Duration adj ) with( clk ) {
41        clocktype = -1;
42        offset = adj + timezone`s;                                                      // timezone (global) is (UTC - local time) in seconds
43} // resetClock
44
45static inline void ?{}( Clock & clk ) { resetClock( clk ); }
46static inline void ?{}( Clock & clk, Duration adj ) { resetClock( clk, adj ); }
47
48static inline Duration getRes() {
49        struct timespec res;
50        clock_getres( CLOCK_REALTIME_COARSE, &res );
51        return ((int64_t)res.tv_sec * TIMEGRAN + res.tv_nsec)`ns;
52} // getRes
53
54static inline Time getTimeNsec() {                                              // with nanoseconds
55        timespec curr;
56        clock_gettime( CLOCK_REALTIME_COARSE, &curr );
57        return (Time){ curr };
58} // getTime
59
60static inline Time getTime() {                                                  // without nanoseconds
61        timespec curr;
62        clock_gettime( CLOCK_REALTIME_COARSE, &curr );
63        curr.tv_nsec = 0;
64        return (Time){ curr };
65} // getTime
66
67static inline Time getTime( Clock & clk ) with( clk ) {
68        return getTime() + offset;
69} // getTime
70
71static inline Time ?()( Clock & clk ) with( clk ) {             // alternative syntax
72        return getTime() + offset;
73} // getTime
74
75static inline timeval getTime( Clock & clk ) {
76        return (timeval){ clk() };
77} // getTime
78
79static inline tm getTime( Clock & clk ) with( clk ) {
80        tm ret;
81        localtime_r( getTime( clk ).tv_sec, &ret );
82        return ret;
83} // getTime
84
85// Local Variables: //
86// mode: c //
87// tab-width: 4 //
88// End: //
Note: See TracBrowser for help on using the repository browser.