Debian Codegraph @

protolib/src/common/protoTime.cpp

raw
1 /**
2 * @file protoTime.cpp
3 *
4 * @brief Provides parsing and comparison time operations.
5
6 */
7 #include "protoTime.h"
8 #include "protoDebug.h"
9
10 #ifndef SIMULATE
11 static const ProtoTime PROTO_TIME_INIT = ProtoTime().GetCurrentTime();
12 #else
13 static const ProtoTime PROTO_TIME_INIT(0);
14 #endif
15
16 ProtoTime::ProtoTime()
17 {
18 tval.tv_sec = tval.tv_usec = 0;
19 }
20
21 ProtoTime::ProtoTime(const struct timeval& timeVal)
22 : tval(timeVal)
23 {
24 }
25
26 ProtoTime::ProtoTime(double seconds)
27 {
28 tval.tv_sec = (time_t)seconds;
29 tval.tv_usec = (suseconds_t)(1.0e+06 * (seconds - ((double)tval.tv_sec)));
30 }
31
32 ProtoTime::ProtoTime(unsigned long sec, unsigned long usec)
33 {
34 tval.tv_sec = (time_t)sec;
35 tval.tv_usec = (suseconds_t)usec;
36 }
37
38 void ProtoTime::operator+=(const ProtoTime& t)
39 {
40 tval.tv_sec += t.sec();
41 tval.tv_usec += t.usec();
42 if (tval.tv_usec >= 1000000)
43 {
44 tval.tv_sec++;
45 tval.tv_usec -= 1000000;
46 }
47 } // end ProtoTime::operator+=()
48
49 void ProtoTime::operator+=(double value)
50 {
51 if (value >= 0.0)
52 {
53 time_t sec = (time_t)value;
54 suseconds_t usec = (suseconds_t)(1.0e+06*(value - (double)sec) + 0.5);
55 tval.tv_sec += sec;
56 tval.tv_usec += usec;
57 if (tval.tv_usec >= 1000000)
58 {
59 tval.tv_sec += 1;
60 tval.tv_usec -= 1000000;
61 }
62 }
63 else
64 {
65 value = -value;
66 time_t sec = (time_t)value;
67 if (tval.tv_sec >= sec)
68 {
69 tval.tv_sec -= sec;
70 suseconds_t usec = (suseconds_t)(1.0e+06*(value - (double)sec) + 0.5);
71 if (tval.tv_usec >= usec)
72 {
73 tval.tv_usec -= usec;
74 return;
75 }
76 else if (tval.tv_sec > 0)
77 {
78 tval.tv_sec -= 1;
79 tval.tv_usec = usec - tval.tv_usec;
80 return;
81 }
82 }
83 tval.tv_sec = 0;
84 tval.tv_usec = 0;
85 }
86 } // end ProtoTime::operator+=()
87
88 double ProtoTime::GetOffsetValue() const
89 {
90 return ProtoTime::Delta(*this, PROTO_TIME_INIT);
91 }
92
93 double ProtoTime::Delta(const ProtoTime& t1, const ProtoTime& t2)
94 {
95 // (TBD) perhaps we should have "long" instead "unsigned long"
96 // sec()/usec() methods to simplify this?
97 double delta = (t1.sec() >= t2.sec()) ?
98 (double)(t1.sec() - t2.sec()) :
99 -(double)(t2.sec() - t1.sec());
100 if (t1.usec() > t2.usec())
101 delta += 1.0e-06 * (double)(t1.usec() - t2.usec());
102 else
103 delta -= 1.0e-06 * (double)(t2.usec() - t1.usec());
104 return delta;
105 } // end ProtoTime::Delta()
106
107
108 #ifdef WIN32
109 // These are state variables that are needed to manage
110 // use of the WIN32 High Performance Counter to get more
111 // precise timing (needed for WinCE)
112 bool proto_performance_counter_init = false;
113 LARGE_INTEGER proto_performance_counter_frequency = {0, 0};
114 #ifdef USE_PERFORMANCE_COUNTER
115 //#ifdef _WIN32_WCE
116 long proto_performance_counter_offset = 0;
117 long proto_system_time_last_sec = 0;
118 unsigned long proto_system_count_roll_sec = 0;
119 LARGE_INTEGER proto_system_count_last = {0, 0};
120 #endif // USE_PERFORMANCE_COUNTER
121 //#endif // _WIN32_WCE
122 #endif // WIN32
123
124
125