The Gaudi Framework  master (37c0b60a)
Time.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2024 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #ifndef GAUDIKERNEL_TIME_H
12 #define GAUDIKERNEL_TIME_H 1
13 
14 // Include files
15 // for the architecture independent int64 definition (long long)
17 #include <GaudiKernel/Kernel.h>
19 
31  // Standard constructor
32  TimeException( std::string Message = "unspecified exception", std::string Tag = "*Gaudi::Time*",
34  : GaudiException( std::move( Message ), std::move( Tag ), std::move( Code ) ) {}
35 };
36 
37 struct tm;
38 #ifdef WIN32
39 typedef struct _FILETIME FILETIME;
40 #endif
41 
42 namespace Gaudi {
43 
44  class Time;
45  class TimeSpan;
46 
63  friend class Time;
64 
65  public:
66  typedef long long ValueType;
67 
69  TimeSpan() = default;
70 
71  TimeSpan( Time t );
72  TimeSpan( ValueType nsecs );
73  TimeSpan( ValueType secs, int nsecs );
74  TimeSpan( int days, int hours, int mins, int secs, int nsecs );
75 
76  int days() const;
77  int hours() const;
78  int minutes() const;
79  ValueType seconds() const;
80 
81  int lastHours() const;
82  int lastMinutes() const;
83  int lastSeconds() const;
84  int lastNSeconds() const;
85 
86  TimeSpan& operator+=( const TimeSpan& x );
87  TimeSpan& operator-=( const TimeSpan& x );
88  TimeSpan& operator*=( const TimeSpan& n );
89  TimeSpan& operator/=( const TimeSpan& n );
90  TimeSpan& operator%=( const TimeSpan& n );
91 
92  ValueType ns() const;
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 bool operator<=( const Gaudi::TimeSpan& t1, const Gaudi::TimeSpan& t2 ) { return t1.ns() <= t2.ns(); }
101 
102  friend bool operator>( const Gaudi::TimeSpan& t1, const Gaudi::TimeSpan& t2 ) { return t1.ns() > t2.ns(); }
103 
104  friend bool operator>=( const Gaudi::TimeSpan& t1, const Gaudi::TimeSpan& t2 ) { return t1.ns() >= t2.ns(); }
105 
106  friend Gaudi::TimeSpan operator+( const Gaudi::TimeSpan& ts1, const Gaudi::TimeSpan& ts2 ) {
107  return Gaudi::TimeSpan( ts1.ns() + ts2.ns() );
108  }
109 
110  friend Gaudi::TimeSpan operator-( const Gaudi::TimeSpan& ts1, const Gaudi::TimeSpan& ts2 ) {
111  return Gaudi::TimeSpan( ts1.ns() - ts2.ns() );
112  }
113 
114  private:
115  ValueType m_nsecs = 0; //< The span length.
116  };
117 
241  class GAUDI_API Time {
242  friend class TimeSpan;
243 
244  public:
245  typedef long long ValueType;
246 
248  enum Months {
249  January = 0,
250  February = 1,
251  March = 2,
252  April = 3,
253  May = 4,
254  June = 5,
255  July = 6,
256  August = 7,
257  September = 8,
258  October = 9,
259  November = 10,
260  December = 11
261  };
262 
264  static const int SECS_PER_DAY = 86400;
265 
267  static const int SECS_PER_HOUR = 3600;
268 
270  static const ValueType SEC_NSECS = 1000000000;
271 
273  Time() = default;
274 
275  Time( TimeSpan ts );
276  Time( ValueType nsecs );
277  Time( ValueType secs, int nsecs );
278  Time( int year, int month, int day, int hour, int min, int sec, ValueType nsecs, bool local = true );
279  // implicit copy constructor
280  // implicit assignment operator
281  // implicit destructor
282 
284  static Time epoch();
286  static Time max();
288  static Time current();
289 #ifdef WIN32
290  static Time from( const FILETIME* systime );
291 #endif
292  static Time build( bool local, const tm& base, TimeSpan diff = 0 );
293 
294  tm split( bool local, int* nsecpart = 0 ) const;
295  tm utc( int* nsecpart = 0 ) const;
296  tm local( int* nsecpart = 0 ) const;
297 
298  int year( bool local ) const;
299  int month( bool local ) const;
300  int day( bool local ) const;
301  int hour( bool local ) const;
302  int minute( bool local ) const;
303  int second( bool local ) const;
304  int nsecond() const;
305  int weekday( bool local ) const;
306  bool isdst( bool local ) const;
307 
308  ValueType utcoffset( int* daylight = 0 ) const;
309  const char* timezone( int* daylight = 0 ) const;
310 
311  Time& operator+=( const TimeSpan& x );
312  Time& operator-=( const TimeSpan& x );
313 
314  ValueType ns() const;
315 
316  std::string format( bool local, std::string spec = "%c" ) const;
317  std::string nanoformat( size_t minwidth = 1, size_t maxwidth = 9 ) const;
318 
319  static bool isLeap( int year );
320 
321  // Conversion helpers
322  static unsigned toDosDate( Time time );
323  static Time fromDosDate( unsigned dosDate );
324 
325  friend bool operator==( const Gaudi::Time& t1, const Gaudi::Time& t2 ) { return t1.ns() == t2.ns(); }
326 
327  friend bool operator!=( const Gaudi::Time& t1, const Gaudi::Time& t2 ) { return t1.ns() != t2.ns(); }
328 
329  friend bool operator<( const Gaudi::Time& t1, const Gaudi::Time& t2 ) { return t1.ns() < t2.ns(); }
330 
331  friend bool operator<=( const Gaudi::Time& t1, const Gaudi::Time& t2 ) { return t1.ns() <= t2.ns(); }
332 
333  friend bool operator>( const Gaudi::Time& t1, const Gaudi::Time& t2 ) { return t1.ns() > t2.ns(); }
334 
335  friend bool operator>=( const Gaudi::Time& t1, const Gaudi::Time& t2 ) { return t1.ns() >= t2.ns(); }
336 
337  private:
338  ValueType m_nsecs = 0; //< Time value as nsecs from #epoch().
339 
340  // Taking string_view means there will never be any dynamic allocation if cond == true
341  inline void TimeAssert( bool cond, std::string_view msg = "time assertion failed" ) const {
342  if ( !cond ) throw TimeException( std::string{ msg } );
343  }
344  };
345 } // namespace Gaudi
346 
347 #include <GaudiKernel/Time.icpp>
348 
349 #endif // GAUDIKERNEL_TIME_H
Gaudi::Time::operator>=
friend bool operator>=(const Gaudi::Time &t1, const Gaudi::Time &t2)
Definition: Time.h:335
std::string
STL class.
Gaudi::TimeSpan::operator<=
friend bool operator<=(const Gaudi::TimeSpan &t1, const Gaudi::TimeSpan &t2)
Definition: Time.h:100
GaudiException.h
Gaudi::TimeSpan::ValueType
long long ValueType
Definition: Time.h:66
GaudiException
Definition: GaudiException.h:31
GaudiMP.FdsRegistry.msg
msg
Definition: FdsRegistry.py:19
Gaudi::Time::Time
Time()=default
Initialize an empty (zero) time value.
Gaudi::Units::second
constexpr double second
Definition: SystemOfUnits.h:139
StreamBuffer.h
Gaudi::TimeSpan::ns
ValueType ns() const
Return the time span as nanoseconds.
Definition: Time.icpp:115
Gaudi::Time
Definition: Time.h:241
Gaudi::TimeSpan
Definition: Time.h:62
bug_34121.t
t
Definition: bug_34121.py:31
Gaudi::TimeSpan::operator-
friend Gaudi::TimeSpan operator-(const Gaudi::TimeSpan &ts1, const Gaudi::TimeSpan &ts2)
Definition: Time.h:110
StatusCode
Definition: StatusCode.h:65
Message
Definition: Message.h:26
Gaudi::TimeSpan::operator>
friend bool operator>(const Gaudi::TimeSpan &t1, const Gaudi::TimeSpan &t2)
Definition: Time.h:102
Gaudi::TimeSpan::operator==
friend bool operator==(const Gaudi::TimeSpan &t1, const Gaudi::TimeSpan &t2)
Definition: Time.h:94
Gaudi::Units::ns
constexpr double ns
Definition: SystemOfUnits.h:152
compareRootHistos.ts
ts
Definition: compareRootHistos.py:488
TemplatedAlg
Definition: TemplatedAlg.cpp:22
Time.icpp
Gaudi::Time::ValueType
long long ValueType
Definition: Time.h:245
Gaudi::TimeSpan::TimeSpan
TimeSpan()=default
Initialize an empty (zero) time difference.
Gaudi::Time::operator==
friend bool operator==(const Gaudi::Time &t1, const Gaudi::Time &t2)
Definition: Time.h:325
format
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:119
Gaudi::TimeSpan::operator<
friend bool operator<(const Gaudi::TimeSpan &t1, const Gaudi::TimeSpan &t2)
Definition: Time.h:98
Gaudi
This file provides a Grammar for the type Gaudi::Accumulators::Axis It allows to use that type from p...
Definition: __init__.py:1
plotSpeedupsPyRoot.time
time
Definition: plotSpeedupsPyRoot.py:180
cpluginsvc.n
n
Definition: cpluginsvc.py:234
Gaudi::Time::operator>
friend bool operator>(const Gaudi::Time &t1, const Gaudi::Time &t2)
Definition: Time.h:333
std
STL namespace.
Kernel.h
Gaudi::Time::Months
Months
Symbolic names for months.
Definition: Time.h:248
Gaudi::Time::TimeAssert
void TimeAssert(bool cond, std::string_view msg="time assertion failed") const
Definition: Time.h:341
TimeException::TimeException
TimeException(std::string Message="unspecified exception", std::string Tag="*Gaudi::Time*", StatusCode Code=StatusCode::FAILURE)
Definition: Time.h:32
Gaudi::Time::operator!=
friend bool operator!=(const Gaudi::Time &t1, const Gaudi::Time &t2)
Definition: Time.h:327
TimeException
Definition: Time.h:30
check_ParticleID.base
base
Definition: check_ParticleID.py:24
Gaudi::TimeSpan::operator+
friend Gaudi::TimeSpan operator+(const Gaudi::TimeSpan &ts1, const Gaudi::TimeSpan &ts2)
Definition: Time.h:106
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
Gaudi::Time::operator<=
friend bool operator<=(const Gaudi::Time &t1, const Gaudi::Time &t2)
Definition: Time.h:331
Gaudi::TimeSpan::operator>=
friend bool operator>=(const Gaudi::TimeSpan &t1, const Gaudi::TimeSpan &t2)
Definition: Time.h:104
Gaudi::TimeSpan::operator!=
friend bool operator!=(const Gaudi::TimeSpan &t1, const Gaudi::TimeSpan &t2)
Definition: Time.h:96
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:81
Gaudi::Time::operator<
friend bool operator<(const Gaudi::Time &t1, const Gaudi::Time &t2)
Definition: Time.h:329