wfmath  1.0.3
A math library for the Worldforge system.
timestamp.cpp
1 // timestamp.cpp (time and random number implementation)
2 //
3 // The WorldForge Project
4 // Copyright (C) 2002 The WorldForge Project
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 //
20 // For information about WorldForge and its authors, please contact
21 // the Worldforge Web Site at http://www.worldforge.org.
22 
23 // Author: Ron Steinke
24 // Created: 2002-5-23
25 
26 #if defined _WIN32
27 #include <sys/timeb.h>
28 #else
29 #include <unistd.h>
30 #include <sys/time.h>
31 #endif
32 
33 #include "timestamp.h"
34 
35 #ifdef _WIN32
36 #include <winsock2.h>
37 #endif
38 
39 #ifdef HAVE_CONFIG_H
40  #include "config.h"
41 #endif
42 
43 static const long Million = 1000000;
44 
45 // apparently, some architectures use int instead of long
46 // in struct timeval
47 template<class T1, class T2>
48 static void regularize(T1 &sec, T2 &usec)
49 {
50  if(usec >= Million) {
51  usec -= Million;
52  ++sec;
53  }
54  else if(usec < 0) {
55  usec += Million;
56  --sec;
57  }
58 }
59 
60 namespace WFMath {
61 
62 TimeDiff::TimeDiff(long sec, long usec, bool is_valid) : m_isvalid(is_valid),
63  m_sec(sec), m_usec(usec)
64 {
65  if (m_isvalid)
66  regularize(m_sec, m_usec);
67 }
68 
69 TimeDiff::TimeDiff(long msec) :
70  m_isvalid(true),
71  m_sec(msec / 1000),
72  m_usec(msec % 1000)
73 {
74  if(msec < 0) {
75  --m_sec;
76  if(m_usec < 0) // behavior of % is machine dependent
77  m_usec += Million;
78  }
79 }
80 
82 {
83  return m_sec * 1000 + m_usec / 1000;
84 }
85 
86 TimeDiff& operator+=(TimeDiff &val, const TimeDiff &d)
87 {
88  val.m_sec += d.m_sec;
89  val.m_usec += d.m_usec;
90  val.m_isvalid = val.m_isvalid && d.m_isvalid;
91 
92  if (val.m_isvalid)
93  regularize(val.m_sec, val.m_usec);
94 
95  return val;
96 }
97 
98 TimeDiff& operator-=(TimeDiff &val, const TimeDiff &d)
99 {
100  val.m_sec -= d.m_sec;
101  val.m_usec -= d.m_usec;
102  val.m_isvalid = val.m_isvalid && d.m_isvalid;
103 
104  if (val.m_isvalid)
105  regularize(val.m_sec, val.m_usec);
106 
107  return val;
108 }
109 
110 TimeDiff operator+(const TimeDiff &a, const TimeDiff &b)
111 {
112  return TimeDiff(a.m_sec + b.m_sec, a.m_usec + b.m_usec, a.m_isvalid && b.m_isvalid);
113 }
114 
115 TimeDiff operator-(const TimeDiff &a, const TimeDiff &b)
116 {
117  return TimeDiff(a.m_sec - b.m_sec, a.m_usec - b.m_usec, a.m_isvalid && b.m_isvalid);
118 }
119 
120 bool operator<(const TimeDiff &a, const TimeDiff &b)
121 {
122  return (a.m_sec < b.m_sec) || ((a.m_sec == b.m_sec) && (a.m_usec < b.m_usec));
123 }
124 
125 bool operator==(const TimeDiff &a, const TimeDiff &b)
126 {
127  return (a.m_sec == b.m_sec) && (a.m_usec == b.m_usec);
128 }
129 
131 {
132  TimeStamp ret;
133 #ifndef _WIN32
134  gettimeofday(&ret._val, nullptr);
135 #else
136  SYSTEMTIME sysTime;
137  FILETIME fileTime = {0}; /* 100ns == 1 */
138  LARGE_INTEGER i;
139 
140  GetSystemTime(&sysTime);
141  SystemTimeToFileTime(&sysTime, &fileTime);
142  /* Documented as the way to get a 64 bit from a
143  * FILETIME. */
144  memcpy(&i, &fileTime, sizeof(LARGE_INTEGER));
145 
146  ret._val.tv_sec = i.QuadPart / 10000000; /*10e7*/
147  ret._val.tv_usec = (i.QuadPart / 10) % 1000000; /*10e6*/
148 
149 #endif
150  ret._isvalid = true;
151  return ret;
152 }
153 
155 {
156  TimeStamp ret;
157 
158  ret._val.tv_sec = 0;
159  ret._val.tv_usec = 0;
160  ret._isvalid = true;
161 
162  return ret;
163 }
164 
165 // FIXME C++0x supports initialising _val this way
166 // _val({sec, usec}),
167 TimeStamp::TimeStamp(long sec, long usec, bool isvalid) : _isvalid(isvalid)
168 {
169  _val.tv_sec = sec;
170  _val.tv_usec = usec;
171 
172  if (_isvalid)
173  regularize(_val.tv_sec, _val.tv_usec);
174 }
175 
176 bool operator<(const TimeStamp &a, const TimeStamp &b)
177 {
178  if (a._val.tv_sec == b._val.tv_sec)
179  return (a._val.tv_usec < b._val.tv_usec);
180  else
181  return a._val.tv_sec < b._val.tv_sec;
182 }
183 
184 bool operator==(const TimeStamp &a, const TimeStamp &b)
185 {
186  return (a._val.tv_sec == b._val.tv_sec)
187  && (a._val.tv_usec == b._val.tv_usec);
188 }
189 
190 TimeStamp& operator+=(TimeStamp &a, const TimeDiff &d)
191 {
192  a._val.tv_sec += d.m_sec;
193  a._val.tv_usec += d.m_usec;
194  a._isvalid = a._isvalid && d.m_isvalid;
195 
196  if (a._isvalid)
197  regularize(a._val.tv_sec, a._val.tv_usec);
198  return a;
199 }
200 
201 TimeStamp& operator-=(TimeStamp &a, const TimeDiff &d)
202 {
203  a._val.tv_sec -= d.m_sec;
204  a._val.tv_usec -= d.m_usec;
205  a._isvalid = a._isvalid && d.m_isvalid;
206 
207  if (a._isvalid)
208  regularize(a._val.tv_sec, a._val.tv_usec);
209  return a;
210 }
211 
212 TimeStamp operator+(const TimeStamp &a, const TimeDiff &d)
213 {
214  return TimeStamp(a._val.tv_sec + d.m_sec, a._val.tv_usec + d.m_usec,
215  a._isvalid && d.m_isvalid);
216 }
217 
218 TimeStamp operator-(const TimeStamp &a, const TimeDiff &d)
219 {
220  return TimeStamp(a._val.tv_sec - d.m_sec, a._val.tv_usec - d.m_usec,
221  a._isvalid && d.m_isvalid);
222 }
223 
224 TimeDiff operator-(const TimeStamp &a, const TimeStamp &b)
225 {
226  return TimeDiff(a._val.tv_sec - b._val.tv_sec,
227  a._val.tv_usec - b._val.tv_usec, a._isvalid && b._isvalid);
228 }
229 
230 }
The difference between two timestamps.
Definition: timestamp.h:53
TimeDiff()
construct an uninitialized TimeDiff
Definition: timestamp.h:57
long milliseconds() const
Get the value of a TimeDiff in milliseconds.
Definition: timestamp.cpp:81
A time stamp.
Definition: timestamp.h:117
static TimeStamp epochStart()
set a TimeStamp to Jan 1, 1970
Definition: timestamp.cpp:154
static TimeStamp now()
set a TimeStamp to the current time
Definition: timestamp.cpp:130
TimeStamp()
Construct an uninitialized TimeStamp.
Definition: timestamp.h:133
Generic library namespace.
Definition: shape.h:41