WFMath 1.0.2
timestamp.h
1// timestamp.h (time functions, taken from Eris)
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., 675 Mass Ave, Cambridge, MA 02139, 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#ifndef WFMATH_TIMESTAMP_H
27#define WFMATH_TIMESTAMP_H
28
29#include <wfmath/const.h>
30#include <algorithm> // For std::pair
31#include <iosfwd>
32
36#ifdef _MSC_VER
37#include <sys/timeb.h>
38#else
39#include <sys/time.h>
40#endif
41
42namespace WFMath {
43
44class TimeStamp;
45
47
53{
54 TimeDiff(long sec, long usec, bool is_valid);
55 public:
57 TimeDiff() : m_isvalid(false) {}
59 TimeDiff(long msec);
60 // default copy constructor is fine
61
63
67 long milliseconds() const;
69 std::pair<long,long> full_time() const {return std::make_pair(m_sec,m_usec);}
70
71 bool isValid() const {return m_isvalid;}
72
78 TimeDiff operator-() const {return TimeDiff(-m_sec, -m_usec, m_isvalid);}
79
81 friend TimeDiff operator+(const TimeDiff &a, const TimeDiff &b);
83 friend TimeDiff operator-(const TimeDiff &a, const TimeDiff &b);
84
89
91 friend TimeStamp operator+(const TimeStamp &a, const TimeDiff &msec);
93 friend TimeStamp operator-(const TimeStamp &a, const TimeDiff &msec);
94
96 friend TimeDiff operator-(const TimeStamp &a, const TimeStamp &b);
97
98 friend bool operator<(const TimeDiff&, const TimeDiff&);
99 friend bool operator==(const TimeDiff&, const TimeDiff&);
100
101 private:
102 bool m_isvalid;
103 long m_sec, m_usec;
104};
105
106inline bool operator>(const TimeDiff &a, const TimeDiff &b) {return b < a;}
107inline bool operator<=(const TimeDiff &a, const TimeDiff &b) {return !(b < a);}
108inline bool operator>=(const TimeDiff &a, const TimeDiff &b) {return !(a < b);}
109inline bool operator!=(const TimeDiff &a, const TimeDiff &b) {return !(b == a);}
110
112
118 private:
119#ifdef __WIN32__
120 // We roll our own timeval... may only need to be done for mingw32.
121 struct {
122 long tv_sec; /* seconds */
123 long tv_usec; /* microseconds */
124 } _val;
125#else
126 // POSIX, BeOS, ....
127 struct timeval _val;
128#endif
129 bool _isvalid;
130 TimeStamp(long sec, long usec, bool isvalid);
131 public:
133 TimeStamp() : _isvalid(false) {}
134 // default copy constructor is fine
135
136 friend bool operator<(const TimeStamp &a, const TimeStamp &b);
137 friend bool operator==(const TimeStamp &a, const TimeStamp &b);
138
139 friend std::ostream& operator<<(std::ostream& os, const TimeStamp&);
140 friend std::istream& operator>>(std::istream& is, TimeStamp&);
141
142 bool isValid() const {return _isvalid;}
144 friend TimeStamp& operator+=(TimeStamp&, const TimeDiff&);
146 friend TimeStamp& operator-=(TimeStamp&, const TimeDiff&);
147
149 friend TimeStamp operator+(const TimeStamp &a, const TimeDiff &msec);
151 friend TimeStamp operator-(const TimeStamp &a, const TimeDiff &msec);
152
154 friend TimeDiff operator-(const TimeStamp &a, const TimeStamp &b);
155
157 static TimeStamp now();
160};
161
163inline TimeStamp operator+(TimeDiff msec, const TimeStamp &a) {return a + msec;}
164
165inline bool operator>(const TimeStamp &a, const TimeStamp &b) {return b < a;}
166inline bool operator<=(const TimeStamp &a, const TimeStamp &b) {return !(b < a);}
167inline bool operator>=(const TimeStamp &a, const TimeStamp &b) {return !(a < b);}
168inline bool operator!=(const TimeStamp &a, const TimeStamp &b) {return !(b == a);}
169
170} // namespace WFMath
171
172#endif // WFMATH_TIMESTAMP_H
The difference between two timestamps.
Definition timestamp.h:53
TimeDiff(long msec)
construct a TimeDiff of a given number of milliseconds
friend TimeDiff & operator-=(TimeDiff &, const TimeDiff &)
decrement a TimeDiff
friend TimeStamp & operator+=(TimeStamp &, const TimeDiff &)
advance a TimeStamp by a TimeDiff
std::pair< long, long > full_time() const
Get the value of a TimeDiff in (seconds, microseconds)
Definition timestamp.h:69
friend TimeDiff operator-(const TimeDiff &a, const TimeDiff &b)
subtract two TimeDiff instances
friend TimeStamp & operator-=(TimeStamp &, const TimeDiff &)
regress a TimeStamp by a TimeDiff
TimeDiff()
construct an uninitialized TimeDiff
Definition timestamp.h:57
TimeDiff operator-() const
negate a TimeDiff
Definition timestamp.h:78
friend TimeDiff operator+(const TimeDiff &a, const TimeDiff &b)
add two TimeDiff instances
friend TimeDiff & operator+=(TimeDiff &, const TimeDiff &)
increment a TimeDiff
friend TimeDiff operator-(const TimeStamp &a, const TimeStamp &b)
find the time difference between two time stamps
friend TimeStamp operator-(const TimeStamp &a, const TimeDiff &msec)
find the result of regressing a TimeStamp
long milliseconds() const
Get the value of a TimeDiff in milliseconds.
friend TimeStamp operator+(const TimeStamp &a, const TimeDiff &msec)
find the result of advancing a TimeStamp
A time stamp.
Definition timestamp.h:117
static TimeStamp epochStart()
set a TimeStamp to Jan 1, 1970
TimeStamp()
Construct an uninitialized TimeStamp.
Definition timestamp.h:133
static TimeStamp now()
set a TimeStamp to the current time
Generic library namespace.
Definition atlasconv.h:45