The Gaudi Framework  v28r3 (cc1cf868)
Time.h
Go to the documentation of this file.
1 #ifndef GAUDIKERNEL_TIME_H
2 #define GAUDIKERNEL_TIME_H 1
3 
4 // Include files
5 // for the architecture independent int64 definition (longlong)
6 #include "GaudiKernel/Kernel.h"
9 
21  // Standard constructor
22  TimeException( std::string Message = "unspecified exception",
23  std::string Tag = "*Gaudi::Time*",
25  GaudiException(std::move(Message),std::move(Tag),std::move(Code)) {}
26 };
27 
28 struct tm;
29 # ifdef WIN32
30 typedef struct _FILETIME FILETIME;
31 # endif
32 
33 namespace Gaudi {
34 
35  class Time;
36  class TimeSpan;
37 
54  friend class Time;
55  public:
57 
59  TimeSpan() = default;
60 
61  TimeSpan(Time t);
62  TimeSpan(ValueType nsecs);
63  TimeSpan(ValueType secs, int nsecs);
64  TimeSpan(int days, int hours, int mins, int secs, int nsecs);
65 
66  int days() const;
67  int hours() const;
68  int minutes() const;
69  ValueType seconds() const;
70 
71  int lastHours() const;
72  int lastMinutes() const;
73  int lastSeconds() const;
74  int lastNSeconds() const;
75 
76  TimeSpan & operator+= (const TimeSpan &x);
77  TimeSpan & operator-= (const TimeSpan &x);
78  TimeSpan & operator*= (const TimeSpan &n);
79  TimeSpan & operator/= (const TimeSpan &n);
80  TimeSpan & operator%= (const TimeSpan &n);
81 
82  ValueType ns() const;
83 
84  friend bool operator== (const Gaudi::TimeSpan &t1, const Gaudi::TimeSpan &t2)
85  { return t1.ns() == t2.ns(); }
86 
87  friend bool operator!= (const Gaudi::TimeSpan &t1, const Gaudi::TimeSpan &t2)
88  { return t1.ns() != t2.ns(); }
89 
90  friend bool operator< (const Gaudi::TimeSpan &t1, const Gaudi::TimeSpan &t2)
91  { return t1.ns() < t2.ns(); }
92 
93  friend bool operator<= (const Gaudi::TimeSpan &t1, const Gaudi::TimeSpan &t2)
94  { return t1.ns() <= t2.ns(); }
95 
96  friend bool operator> (const Gaudi::TimeSpan &t1, const Gaudi::TimeSpan &t2)
97  { return t1.ns() > t2.ns(); }
98 
99  friend bool operator>= (const Gaudi::TimeSpan &t1, const Gaudi::TimeSpan &t2)
100  { return t1.ns() >= t2.ns(); }
101 
103  { return Gaudi::TimeSpan (ts1.ns() + ts2.ns()); }
104 
106  { return Gaudi::TimeSpan (ts1.ns() - ts2.ns()); }
107 
108  private:
109  ValueType m_nsecs = 0; //< The span length.
110  };
111 
112 
236  class GAUDI_API Time {
237  friend class TimeSpan;
238  public:
240 
242  enum Months {
243  January = 0,
244  February = 1,
245  March = 2,
246  April = 3,
247  May = 4,
248  June = 5,
249  July = 6,
250  August = 7,
251  September = 8,
252  October = 9,
253  November = 10,
254  December = 11
255  };
256 
258  static const int SECS_PER_DAY = 86400;
259 
261  static const int SECS_PER_HOUR = 3600;
262 
264  static const ValueType SEC_NSECS = 1000000000;
265 
267  Time() = default;
268 
269  Time(TimeSpan ts);
270  Time(ValueType nsecs);
271  Time(ValueType secs, int nsecs);
272  Time(int year, int month, int day, int hour, int min, int sec,
273  ValueType nsecs, bool local = true);
274  // implicit copy constructor
275  // implicit assignment operator
276  // implicit destructor
277 
279  static Time epoch();
281  static Time max();
283  static Time current();
284 # ifdef WIN32
285  static Time from (const FILETIME *systime);
286 # endif
287  static Time build (bool local, const tm &base, TimeSpan diff = 0);
288 
289  tm split (bool local, int *nsecpart = 0) const;
290  tm utc (int *nsecpart = 0) const;
291  tm local (int *nsecpart = 0) const;
292 
293  int year (bool local) const;
294  int month (bool local) const;
295  int day (bool local) const;
296  int hour (bool local) const;
297  int minute (bool local) const;
298  int second (bool local) const;
299  int nsecond () const;
300  int weekday (bool local) const;
301  bool isdst (bool local) const;
302 
303  ValueType utcoffset (int *daylight = 0) const;
304  const char * timezone (int *daylight = 0) const;
305 
306  Time & operator+= (const TimeSpan &x);
307  Time & operator-= (const TimeSpan &x);
308 
309  ValueType ns() const;
310 
311  std::string format (bool local, std::string spec = "%c") const;
312  std::string nanoformat (size_t minwidth = 1, size_t maxwidth = 9) const;
313 
314  static bool isLeap (int year);
315 
316  // Conversion helpers
317  static unsigned toDosDate (Time time);
318  static Time fromDosDate (unsigned dosDate);
319 
320  friend bool operator== (const Gaudi::Time &t1, const Gaudi::Time &t2)
321  { return t1.ns() == t2.ns(); }
322 
323  friend bool operator!= (const Gaudi::Time &t1, const Gaudi::Time &t2)
324  { return t1.ns() != t2.ns(); }
325 
326  friend bool operator< (const Gaudi::Time &t1, const Gaudi::Time &t2)
327  { return t1.ns() < t2.ns(); }
328 
329  friend bool operator<= (const Gaudi::Time &t1, const Gaudi::Time &t2)
330  { return t1.ns() <= t2.ns(); }
331 
332  friend bool operator> (const Gaudi::Time &t1, const Gaudi::Time &t2)
333  { return t1.ns() > t2.ns(); }
334 
335  friend bool operator>= (const Gaudi::Time &t1, const Gaudi::Time &t2)
336  { return t1.ns() >= t2.ns(); }
337 
338  private:
339  ValueType m_nsecs = 0; //< Time value as nsecs from #epoch().
340 
341  inline void TimeAssert(bool cond, const std::string &msg = "time assertion failed") const {
342  if (!cond) throw TimeException(msg);
343  }
344 
345  };
346 
347 }
348 
349 #include "GaudiKernel/Time.icpp"
350 
351 #endif // GAUDIKERNEL_TIME_H
longlong ValueType
Definition: Time.h:239
bool operator!=(const GaudiUtils::Allocator< T1 > &, const GaudiUtils::Allocator< T2 > &)
Definition: Allocator.h:261
Define general base for Gaudi exception.
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:121
longlong ValueType
Definition: Time.h:56
STL namespace.
void TimeAssert(bool cond, const std::string &msg="time assertion failed") const
Definition: Time.h:341
bool operator>(const EventIDBase &lhs, const EventIDBase &rhs)
Definition: EventIDBase.h:208
constexpr double second
STL class.
Months
Symbolic names for months.
Definition: Time.h:242
ValueType ns() const
Return the time span as nanoseconds.
Definition: Time.icpp:123
Based on seal::Time.
Definition: Time.h:236
bool operator==(const GaudiUtils::Allocator< T1 > &, const GaudiUtils::Allocator< T2 > &)
Definition: Allocator.h:249
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
Exception thrown by Gaudi::Time.
Definition: Time.h:20
bool operator<(const EventIDBase &lhs, const EventIDBase &rhs)
Definition: EventIDBase.h:185
The Message class.
Definition: Message.h:15
TimeException(std::string Message="unspecified exception", std::string Tag="*Gaudi::Time*", StatusCode Code=StatusCode::FAILURE)
Definition: Time.h:22
bool operator>=(const EventIDBase &lhs, const EventIDBase &rhs)
Definition: EventIDBase.h:217
GAUDI_API Stat operator-(const Stat &stat, const double value)
external operator for subtraction of Stat and a number
Definition: Stat.cpp:168
ValueType ns() const
Return the time as nanoseconds since 00:00:00 on January 1, 1970 in UTC.
Definition: Time.icpp:29
#define GAUDI_API
Definition: Kernel.h:107
Based on seal::TimeSpan.
Definition: Time.h:53
constexpr double ns
Helper functions to set/get the application return code.
Definition: __init__.py:1
bool operator<=(const EventIDBase &lhs, const EventIDBase &rhs)
Definition: EventIDBase.h:214
decltype(std::declval< TP >()+std::declval< T >()) operator+(const T &v, const Property< TP, V, H > &p)
implemantation of (value + property)
Definition: Property.h:688