Scarab  v1.6.0
Project 8 C++ Utility Library
time.hh
Go to the documentation of this file.
1 #ifndef SCARAB_TIME_HH_
2 #define SCARAB_TIME_HH_
3 
4 #include "scarab_api.hh"
5 
6 #include <cstdint>
7 #include <string>
8 
9 #ifndef _WIN32
10 #include <sys/time.h>
11 #else
12 #include <Windows.h>
13 #endif
14 #include <time.h>
15 
16 #ifdef __MACH__
17 #include <mach/mach_time.h>
18 #endif
19 
20 namespace scarab
21 {
22  typedef uint64_t time_nsec_type; // the maximum number of nanoseconds representable by a uint64_t is equivalent to 584.6 years
23 }
24 
25 #ifndef NSEC_PER_SEC
26 #define NSEC_PER_SEC 1000000000
27 #endif
28 
29 #ifndef SEC_PER_NSEC
30 #define SEC_PER_NSEC 1.e-9
31 #endif
32 
33 #ifdef __MACH__
34 #ifndef MACNANO
35 #define MACNANO (+1.0E-9)
36 #define MACGIGA UINT64_C(1000000000)
37 #endif // MACNANO
38  extern double thorax_timebase;
39  extern scarab::time_nsec_type thorax_timestart;
40 #endif // __MACH__
41 
42 
43 #ifdef _WIN32
44 struct timespec
45 {
46  time_t tv_sec;
47  long tv_nsec;
48  //inline SCARAB_API bool operator==( const timespec& rhs ) const { return tv_nsec==rhs.tv_nsec && tv_sec==rhs.tv_sec; }
49  //inline SCARAB_API bool operator<( const timespec& rhs ) const { return tv_sec != rhs.tv_sec ? tv_sec < rhs.tv_sec : tv_nsec < rhs.tv_nsec; }
50 };
51 #endif
52 //#else
53 inline SCARAB_API bool operator==( const timespec& lhs, const timespec& rhs ) { return lhs.tv_nsec==rhs.tv_nsec && lhs.tv_sec==rhs.tv_sec; }
54 inline SCARAB_API bool operator<( const timespec& lhs, const timespec& rhs ) { return lhs.tv_sec != rhs.tv_sec ? lhs.tv_sec < rhs.tv_sec : lhs.tv_nsec < rhs.tv_nsec; }
55 //#endif
56 
57 namespace scarab
58 {
59 #ifdef _WIN32
60  SCARAB_API LARGE_INTEGER getFILETIMEoffset();
61  SCARAB_API int clock_gettime( int X, struct timespec* tv );
62 #endif
63 
64  extern SCARAB_API char date_time_format[];
65 
66  SCARAB_API int get_time_monotonic( struct timespec* time );
67 
68  SCARAB_API int get_time_current( struct timespec* time );
69 
70  SCARAB_API time_nsec_type time_to_nsec( struct timespec time );
71 
72  SCARAB_API double time_to_sec( struct timespec time );
73 
74  SCARAB_API void time_diff( struct timespec start, struct timespec end, struct timespec* diff );
75 
76  SCARAB_API size_t get_time_absolute_str( char* ptr );
77 
79 
80 } /* namespace scarab */
81 
82 #endif // SCARAB_TIME_HH_
#define SCARAB_API
Definition: scarab_api.hh:24
std::string get_absolute_time_string()
Definition: time.cc:105
size_t get_time_absolute_str(char *ptr)
Definition: time.cc:95
uint64_t time_nsec_type
Definition: time.hh:22
void time_diff(struct timespec start, struct timespec end, struct timespec *diff)
Definition: time.cc:80
int get_time_current(struct timespec *time)
Definition: time.cc:49
bool operator<(const timespec &lhs, const timespec &rhs)
Definition: time.hh:54
time_nsec_type time_to_nsec(struct timespec time)
Definition: time.cc:70
double time_to_sec(struct timespec time)
Definition: time.cc:75
int get_time_monotonic(struct timespec *time)
Definition: time.cc:29
bool operator==(const timespec &lhs, const timespec &rhs)
Definition: time.hh:53
char date_time_format[]
Definition: time.cc:17