Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v31r0 (aeb156f0)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 (long long)
7 #include "GaudiKernel/Kernel.h"
9 
21  // Standard constructor
22  TimeException( std::string Message = "unspecified exception", std::string Tag = "*Gaudi::Time*",
24  : GaudiException( std::move( Message ), std::move( Tag ), std::move( Code ) ) {}
25 };
26 
27 struct tm;
28 #ifdef WIN32
29 typedef struct _FILETIME FILETIME;
30 #endif
31 
32 namespace Gaudi {
33 
34  class Time;
35  class TimeSpan;
36 
53  friend class Time;
54 
55  public:
56  typedef long long ValueType;
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 ) { return t1.ns() == t2.ns(); }
85 
86  friend bool operator!=( const Gaudi::TimeSpan& t1, const Gaudi::TimeSpan& t2 ) { return t1.ns() != t2.ns(); }
87 
88  friend bool operator<( const Gaudi::TimeSpan& t1, const Gaudi::TimeSpan& t2 ) { return t1.ns() < t2.ns(); }
89 
90  friend bool operator<=( const Gaudi::TimeSpan& t1, const Gaudi::TimeSpan& t2 ) { return t1.ns() <= t2.ns(); }
91 
92  friend bool operator>( const Gaudi::TimeSpan& t1, const Gaudi::TimeSpan& t2 ) { return t1.ns() > t2.ns(); }
93 
94  friend bool operator>=( const Gaudi::TimeSpan& t1, const Gaudi::TimeSpan& t2 ) { return t1.ns() >= t2.ns(); }
95 
96  friend Gaudi::TimeSpan operator+( const Gaudi::TimeSpan& ts1, const Gaudi::TimeSpan& ts2 ) {
97  return Gaudi::TimeSpan( ts1.ns() + ts2.ns() );
98  }
99 
100  friend Gaudi::TimeSpan operator-( const Gaudi::TimeSpan& ts1, const Gaudi::TimeSpan& ts2 ) {
101  return Gaudi::TimeSpan( ts1.ns() - ts2.ns() );
102  }
103 
104  private:
105  ValueType m_nsecs = 0; //< The span length.
106  };
107 
231  class GAUDI_API Time {
232  friend class TimeSpan;
233 
234  public:
235  typedef long long ValueType;
236 
238  enum Months {
239  January = 0,
240  February = 1,
241  March = 2,
242  April = 3,
243  May = 4,
244  June = 5,
245  July = 6,
246  August = 7,
247  September = 8,
248  October = 9,
249  November = 10,
250  December = 11
251  };
252 
254  static const int SECS_PER_DAY = 86400;
255 
257  static const int SECS_PER_HOUR = 3600;
258 
260  static const ValueType SEC_NSECS = 1000000000;
261 
263  Time() = default;
264 
265  Time( TimeSpan ts );
266  Time( ValueType nsecs );
267  Time( ValueType secs, int nsecs );
268  Time( int year, int month, int day, int hour, int min, int sec, ValueType nsecs, bool local = true );
269  // implicit copy constructor
270  // implicit assignment operator
271  // implicit destructor
272 
274  static Time epoch();
276  static Time max();
278  static Time current();
279 #ifdef WIN32
280  static Time from( const FILETIME* systime );
281 #endif
282  static Time build( bool local, const tm& base, TimeSpan diff = 0 );
283 
284  tm split( bool local, int* nsecpart = 0 ) const;
285  tm utc( int* nsecpart = 0 ) const;
286  tm local( int* nsecpart = 0 ) const;
287 
288  int year( bool local ) const;
289  int month( bool local ) const;
290  int day( bool local ) const;
291  int hour( bool local ) const;
292  int minute( bool local ) const;
293  int second( bool local ) const;
294  int nsecond() const;
295  int weekday( bool local ) const;
296  bool isdst( bool local ) const;
297 
298  ValueType utcoffset( int* daylight = 0 ) const;
299  const char* timezone( int* daylight = 0 ) const;
300 
301  Time& operator+=( const TimeSpan& x );
302  Time& operator-=( const TimeSpan& x );
303 
304  ValueType ns() const;
305 
306  std::string format( bool local, std::string spec = "%c" ) const;
307  std::string nanoformat( size_t minwidth = 1, size_t maxwidth = 9 ) const;
308 
309  static bool isLeap( int year );
310 
311  // Conversion helpers
312  static unsigned toDosDate( Time time );
313  static Time fromDosDate( unsigned dosDate );
314 
315  friend bool operator==( const Gaudi::Time& t1, const Gaudi::Time& t2 ) { return t1.ns() == t2.ns(); }
316 
317  friend bool operator!=( const Gaudi::Time& t1, const Gaudi::Time& t2 ) { return t1.ns() != t2.ns(); }
318 
319  friend bool operator<( const Gaudi::Time& t1, const Gaudi::Time& t2 ) { return t1.ns() < t2.ns(); }
320 
321  friend bool operator<=( const Gaudi::Time& t1, const Gaudi::Time& t2 ) { return t1.ns() <= t2.ns(); }
322 
323  friend bool operator>( const Gaudi::Time& t1, const Gaudi::Time& t2 ) { return t1.ns() > t2.ns(); }
324 
325  friend bool operator>=( const Gaudi::Time& t1, const Gaudi::Time& t2 ) { return t1.ns() >= t2.ns(); }
326 
327  private:
328  ValueType m_nsecs = 0; //< Time value as nsecs from #epoch().
329 
330  inline void TimeAssert( bool cond, const std::string& msg = "time assertion failed" ) const {
331  if ( !cond ) throw TimeException( msg );
332  }
333  };
334 } // namespace Gaudi
335 
336 #include "GaudiKernel/Time.icpp"
337 
338 #endif // GAUDIKERNEL_TIME_H
friend bool operator==(const Gaudi::Time &t1, const Gaudi::Time &t2)
Definition: Time.h:315
Define general base for Gaudi exception.
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:109
friend bool operator<(const Gaudi::Time &t1, const Gaudi::Time &t2)
Definition: Time.h:319
friend bool operator==(const Gaudi::TimeSpan &t1, const Gaudi::TimeSpan &t2)
Definition: Time.h:84
friend bool operator!=(const Gaudi::TimeSpan &t1, const Gaudi::TimeSpan &t2)
Definition: Time.h:86
EventIDBase min(const EventIDBase &lhs, const EventIDBase &rhs)
Definition: EventIDBase.h:202
STL namespace.
void TimeAssert(bool cond, const std::string &msg="time assertion failed") const
Definition: Time.h:330
EventIDBase max(const EventIDBase &lhs, const EventIDBase &rhs)
Definition: EventIDBase.h:215
friend bool operator<=(const Gaudi::TimeSpan &t1, const Gaudi::TimeSpan &t2)
Definition: Time.h:90
friend Gaudi::TimeSpan operator+(const Gaudi::TimeSpan &ts1, const Gaudi::TimeSpan &ts2)
Definition: Time.h:96
constexpr double second
STL class.
Months
Symbolic names for months.
Definition: Time.h:238
ValueType ns() const
Return the time span as nanoseconds.
Definition: Time.icpp:105
Based on seal::Time.
Definition: Time.h:231
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
friend bool operator>(const Gaudi::TimeSpan &t1, const Gaudi::TimeSpan &t2)
Definition: Time.h:92
Exception thrown by Gaudi::Time.
Definition: Time.h:20
friend bool operator>=(const Gaudi::Time &t1, const Gaudi::Time &t2)
Definition: Time.h:325
friend bool operator<=(const Gaudi::Time &t1, const Gaudi::Time &t2)
Definition: Time.h:321
friend bool operator>(const Gaudi::Time &t1, const Gaudi::Time &t2)
Definition: Time.h:323
The Message class.
Definition: Message.h:17
TimeException(std::string Message="unspecified exception", std::string Tag="*Gaudi::Time*", StatusCode Code=StatusCode::FAILURE)
Definition: Time.h:22
constexpr static const auto FAILURE
Definition: StatusCode.h:86
ValueType ns() const
Return the time as nanoseconds since 00:00:00 on January 1, 1970 in UTC.
Definition: Time.icpp:28
friend Gaudi::TimeSpan operator-(const Gaudi::TimeSpan &ts1, const Gaudi::TimeSpan &ts2)
Definition: Time.h:100
friend bool operator!=(const Gaudi::Time &t1, const Gaudi::Time &t2)
Definition: Time.h:317
#define GAUDI_API
Definition: Kernel.h:71
friend bool operator<(const Gaudi::TimeSpan &t1, const Gaudi::TimeSpan &t2)
Definition: Time.h:88
friend bool operator>=(const Gaudi::TimeSpan &t1, const Gaudi::TimeSpan &t2)
Definition: Time.h:94
Based on seal::TimeSpan.
Definition: Time.h:52
constexpr double ns
Helper functions to set/get the application return code.
Definition: __init__.py:1
long long ValueType
Definition: Time.h:235
long long ValueType
Definition: Time.h:56