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