Time.icpp
Go to the documentation of this file.
1 #ifndef GAUDIKERNEL_TIME_ICPP
2 #define GAUDIKERNEL_TIME_ICPP 1
3 
4 // Implementation of inline function for classes Gaudi::Time and Gaudi::TimeSpan
5 
6 namespace Gaudi {
7 
9  inline Time::Time (void): m_nsecs(0) {}
10 
13  inline Time::Time (ValueType nsecs): m_nsecs(nsecs) {
14  TimeAssert( m_nsecs >= 0, "cannot create a negative time");
15  }
16 
19  inline Time::Time (TimeSpan ts) : m_nsecs(ts.m_nsecs) {
20  TimeAssert( m_nsecs >= 0, "cannot create a negative time");
21  }
22 
25  inline Time::Time (ValueType secs, int nsecs)
26  : m_nsecs(secs * Time::SEC_NSECS + nsecs) {
27  TimeAssert( m_nsecs >= 0, "cannot create a negative time");
28  }
29 
32  inline Time::ValueType Time::ns (void) const {
33  return m_nsecs;
34  }
35 
38  inline Time &Time::operator+= (const TimeSpan &x) {
39  TimeAssert( m_nsecs >= -x.m_nsecs, "time operation lead to negative time");
40  m_nsecs += x.m_nsecs;
41  return *this;
42  }
43 
46  inline Time &Time::operator-= (const TimeSpan &x) {
47  TimeAssert( m_nsecs >= x.m_nsecs, "time operation lead to negative time");
48  m_nsecs -= x.m_nsecs;
49  return *this;
50  }
51 
53  inline Time Time::epoch (void) {
54  return 0LL;
55  }
56 
58  inline Time Time::max (void) {
59  return 0x7fffffffffffffffLL;
60  }
61 
63  inline bool Time::isLeap (int year) {
64  return ((year % 4) == 0
65  && ((year % 100) != 0
66  || (year % 400) == 0));
67  }
68 
72 
73  inline TimeSpan::TimeSpan (void): m_nsecs(0) {}
74 
76  inline TimeSpan::TimeSpan (Time t): m_nsecs (t.m_nsecs) {}
77 
79  inline TimeSpan::TimeSpan (ValueType nsecs): m_nsecs (nsecs) {}
80 
89  inline TimeSpan::TimeSpan (ValueType secs, int nsecs)
90  : m_nsecs(secs * Time::SEC_NSECS + nsecs) {}
91 
103  inline TimeSpan::TimeSpan (int days, int hours, int mins, int secs, int nsecs) {
104  m_nsecs = (secs + 60 * (mins + 60 * (hours + 24*days)))*Time::SEC_NSECS + nsecs;
105  }
106 
108  inline int TimeSpan::days (void) const {
110  }
111 
113  inline int TimeSpan::hours (void) const {
115  }
116 
118  inline int TimeSpan::minutes (void) const {
119  return int(m_nsecs / Time::SEC_NSECS / 60);
120  }
121 
123  inline TimeSpan::ValueType TimeSpan::seconds (void) const {
124  return m_nsecs / Time::SEC_NSECS;
125  }
126 
128  inline TimeSpan::ValueType TimeSpan::ns (void) const {
129  return m_nsecs;
130  }
131 
134  inline int TimeSpan::lastHours (void) const {
135  return hours () - days () * 24;
136  }
137 
140  inline int TimeSpan::lastMinutes (void) const {
141  return minutes () - hours () * 60;
142  }
143 
146  inline int TimeSpan::lastSeconds (void) const {
147  return int(seconds() - ( (ValueType)minutes() * (ValueType)60 ));
148  }
149 
152  inline int TimeSpan::lastNSeconds (void) const {
153  return int(m_nsecs % Time::SEC_NSECS);
154  }
155 
157  inline TimeSpan &TimeSpan::operator+= (const TimeSpan &x) {
158  m_nsecs += x.m_nsecs;
159  return *this;
160  }
161 
163  inline TimeSpan & TimeSpan::operator-= (const TimeSpan &x) {
164  m_nsecs -= x.m_nsecs;
165  return *this;
166  }
167 
169  inline TimeSpan & TimeSpan::operator*= (const TimeSpan &x) {
170  m_nsecs *= x.m_nsecs;
171  return *this;
172  }
173 
175  inline TimeSpan & TimeSpan::operator/= (const TimeSpan &x) {
176  m_nsecs /= x.m_nsecs; return *this;
177  }
178 
180  inline TimeSpan & TimeSpan::operator%= (const TimeSpan &x) {
181  m_nsecs %= x.m_nsecs; return *this;
182  }
183 
185  inline std::ostream& operator<< (std::ostream &out, const Gaudi::Time &time) {
186  return out << Gaudi::TimeSpan(time).seconds() << '.' << time.nanoformat();
187  }
188 
190  inline std::ostream& operator<< (std::ostream &out, const Gaudi::TimeSpan &time) {
191  return out << time.seconds() << '.' << Gaudi::Time(time).nanoformat();
192  }
193 }
194 
195 //<<<<<< INLINE PUBLIC FUNCTIONS >>>>>>
196 inline Gaudi::Time operator+ (const Gaudi::Time &t, const Gaudi::TimeSpan &ts) {
197  return Gaudi::Time (t.ns() + ts.ns());
198 }
199 
200 inline Gaudi::Time operator+ (const Gaudi::TimeSpan &ts, const Gaudi::Time &t) {
201  return Gaudi::Time (t.ns() + ts.ns());
202 }
203 
204 inline Gaudi::TimeSpan operator- (const Gaudi::Time &t1, const Gaudi::Time &t2) {
205  return Gaudi::TimeSpan (t1.ns() - t2.ns());
206 }
207 
208 inline Gaudi::Time operator- (const Gaudi::Time &t, const Gaudi::TimeSpan &ts) {
209  return Gaudi::Time (t.ns() - ts.ns());
210 }
211 
212 //inline Gaudi::TimeSpan operator* (const Gaudi::Time &t, const Gaudi::TimeSpan &ts) {
213 // return Gaudi::TimeSpan (t.ns() * ts.ns());
214 //}
215 
216 //inline Gaudi::TimeSpan operator/ (const Gaudi::Time &t, const Gaudi::TimeSpan &ts) {
217 // return Gaudi::TimeSpan (t.ns() / ts.ns());
218 //}
219 
220 //inline Gaudi::TimeSpan operator% (const Gaudi::Time &t, const Gaudi::TimeSpan &ts) {
221 // return Gaudi::TimeSpan (t.ns() % ts.ns());
222 //}
223 
224 inline bool operator== (const Gaudi::Time &t1, const Gaudi::Time &t2) {
225  return t1.ns() == t2.ns();
226 }
227 
228 inline bool operator!= (const Gaudi::Time &t1, const Gaudi::Time &t2) {
229  return t1.ns() != t2.ns();
230 }
231 
232 inline bool operator< (const Gaudi::Time &t1, const Gaudi::Time &t2) {
233  return t1.ns() < t2.ns();
234 }
235 
236 inline bool operator<= (const Gaudi::Time &t1, const Gaudi::Time &t2) {
237  return t1.ns() <= t2.ns();
238 }
239 
240 inline bool operator> (const Gaudi::Time &t1, const Gaudi::Time &t2) {
241  return t1.ns() > t2.ns();
242 }
243 
244 inline bool operator>= (const Gaudi::Time &t1, const Gaudi::Time &t2) {
245  return t1.ns() >= t2.ns();
246 }
247 
248 inline bool operator! (const Gaudi::Time &t) {
249  return ! t.ns();
250 }
251 
255 
256 inline Gaudi::TimeSpan operator+ (const Gaudi::TimeSpan &ts) {
257  return ts;
258 }
259 
260 inline Gaudi::TimeSpan operator- (const Gaudi::TimeSpan &ts) {
261  return Gaudi::TimeSpan (-ts.ns());
262 }
263 
264 inline Gaudi::TimeSpan operator+ (const Gaudi::TimeSpan &ts1, const Gaudi::TimeSpan &ts2) {
265  return Gaudi::TimeSpan (ts1.ns() + ts2.ns());
266 }
267 
268 inline Gaudi::TimeSpan operator- (const Gaudi::TimeSpan &ts1, const Gaudi::TimeSpan &ts2) {
269  return Gaudi::TimeSpan (ts1.ns() - ts2.ns());
270 }
271 
272 //inline Gaudi::TimeSpan operator* (const Gaudi::TimeSpan &ts1, const Gaudi::TimeSpan &ts2) {
273 // return Gaudi::TimeSpan (ts1.ns() * ts2.ns());
274 //}
275 
276 //inline Gaudi::TimeSpan operator/ (const Gaudi::TimeSpan &ts1, const Gaudi::TimeSpan &ts2) {
277 // return Gaudi::TimeSpan (ts1.ns() / ts2.ns());
278 //}
279 
280 //inline Gaudi::TimeSpan operator% (const Gaudi::TimeSpan &ts1, const Gaudi::TimeSpan &ts2) {
281 // return Gaudi::TimeSpan (ts1.ns() % ts2.ns());
282 //}
283 
284 inline bool operator== (const Gaudi::TimeSpan &t1, const Gaudi::TimeSpan &t2) {
285  return t1.ns() == t2.ns();
286 }
287 
288 inline bool operator!= (const Gaudi::TimeSpan &t1, const Gaudi::TimeSpan &t2) {
289  return t1.ns() != t2.ns();
290 }
291 
292 inline bool operator< (const Gaudi::TimeSpan &t1, const Gaudi::TimeSpan &t2) {
293  return t1.ns() < t2.ns();
294 }
295 
296 inline bool operator<= (const Gaudi::TimeSpan &t1, const Gaudi::TimeSpan &t2) {
297  return t1.ns() <= t2.ns();
298 }
299 
300 inline bool operator> (const Gaudi::TimeSpan &t1, const Gaudi::TimeSpan &t2) {
301  return t1.ns() > t2.ns();
302 }
303 
304 inline bool operator>= (const Gaudi::TimeSpan &t1, const Gaudi::TimeSpan &t2) {
305  return t1.ns() >= t2.ns();
306 }
307 
308 inline bool operator! (const Gaudi::TimeSpan &ts) {
309  return ! ts.ns();
310 }
311 
312 // --- operators for serialization ---
313 
314 // Output serialization
315 inline StreamBuffer& operator<<(StreamBuffer &s, const Gaudi::Time &t) {
316  return s << t.ns();
317 }
318 // Input serialization
321  s >> tmp;
322  t = Gaudi::Time(tmp);
323  return s;
324 }
325 
326 #endif
longlong ValueType
Definition: Time.h:216
ValueType seconds(void) const
Get the number of complete seconds in the span.
Definition: Time.icpp:123
ValueType ns(void) const
Return the time span as nanoseconds.
Definition: Time.icpp:128
GAUDI_API std::ostream & operator<<(std::ostream &o, const Gaudi::Histo1DDef &histo)
the streamer operator for class Gaudi::Histo1DDef
Definition: HistoDef.cpp:109
bool operator!=(const Gaudi::Time &t1, const Gaudi::Time &t2)
Definition: Time.icpp:228
static const ValueType SEC_NSECS
Nanoseconds in one second.
Definition: Time.h:241
TimeSpan & operator/=(const TimeSpan &n)
Divide a time span.
Definition: Time.icpp:175
longlong ValueType
Definition: Time.h:59
The stream buffer is a small object collecting object data.
Definition: StreamBuffer.h:41
StreamBuffer & operator>>(StreamBuffer &s, Gaudi::Time &t)
Definition: Time.icpp:319
int days(void) const
Get the number of complete days in the span.
Definition: Time.icpp:108
ValueType m_nsecs
Definition: Time.h:86
int lastMinutes(void) const
Get the number of complete minutes in the last incomplete hour of the span.
Definition: Time.icpp:140
bool operator==(const Gaudi::Time &t1, const Gaudi::Time &t2)
Definition: Time.icpp:224
int lastNSeconds(void) const
Get the number of nanoseconds in the last incomplete second of the span.
Definition: Time.icpp:152
static const int SECS_PER_DAY
Seconds in 24 hours.
Definition: Time.h:235
void TimeAssert(bool cond, const std::string &msg="time assertion failed") const
Definition: Time.h:298
TimeSpan & operator*=(const TimeSpan &n)
Multiply a time span.
Definition: Time.icpp:169
StreamBuffer & operator<<(StreamBuffer &s, const Gaudi::Time &t)
Definition: Time.icpp:315
int lastSeconds(void) const
Get the number of complete seconds in the last incomplete minute of the span.
Definition: Time.icpp:146
Gaudi::Time operator+(const Gaudi::Time &t, const Gaudi::TimeSpan &ts)
Definition: Time.icpp:196
ValueType ns(void) const
Return the time as nanoseconds since 00:00:00 on January 1, 1970 in UTC.
Definition: Time.icpp:32
static const int SECS_PER_HOUR
Seconds in one hour hour.
Definition: Time.h:238
Gaudi::TimeSpan operator-(const Gaudi::Time &t1, const Gaudi::Time &t2)
Definition: Time.icpp:204
TimeSpan & operator+=(const TimeSpan &x)
Add to a time span.
Definition: Time.icpp:157
Based on seal::Time.
Definition: Time.h:213
TimeSpan & operator-=(const TimeSpan &x)
Subtract from a time span.
Definition: Time.icpp:163
bool operator>(const Gaudi::Time &t1, const Gaudi::Time &t2)
Definition: Time.icpp:240
int minutes(void) const
Get the number of complete minutes in the span.
Definition: Time.icpp:118
std::string nanoformat(size_t minwidth=1, size_t maxwidth=9) const
Format the nanosecond fractional part of the time as a string.
Definition: Time.cpp:322
TimeSpan & operator%=(const TimeSpan &n)
Compute a modulo of a time span.
Definition: Time.icpp:180
Time & operator+=(const TimeSpan &x)
Add the specified amount to the time.
Definition: Time.icpp:38
static bool isLeap(int year)
Check if the year is a leap-year.
Definition: Time.icpp:63
static Time epoch(void)
Returns the minimum time.
Definition: Time.icpp:53
ValueType m_nsecs
Definition: Time.h:296
bool operator>=(const Gaudi::Time &t1, const Gaudi::Time &t2)
Definition: Time.icpp:244
bool operator!(const Gaudi::Time &t)
Definition: Time.icpp:248
int lastHours(void) const
Get the number of complete hours in the last incomplete day of the span.
Definition: Time.icpp:134
string s
Definition: gaudirun.py:245
bool operator<=(const Gaudi::Time &t1, const Gaudi::Time &t2)
Definition: Time.icpp:236
Time(void)
Initialize an empty (zero) time value.
Definition: Time.icpp:9
TimeSpan(void)
Initialize an empty (zero) time difference.
Definition: Time.icpp:73
Time & operator-=(const TimeSpan &x)
Subtract the specified amount from the time.
Definition: Time.icpp:46
Based on seal::TimeSpan.
Definition: Time.h:56
static Time max(void)
Returns the maximum time.
Definition: Time.icpp:58
Helper functions to set/get the application return code.
Definition: __init__.py:1
bool operator<(const Gaudi::Time &t1, const Gaudi::Time &t2)
Definition: Time.icpp:232
int hours(void) const
Get the number of complete hours in the span.
Definition: Time.icpp:113