The Gaudi Framework  v40r0 (475e45c1)
Time.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2025 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 #pragma once
12 
14 #include <GaudiKernel/Kernel.h>
16 
28  // Standard constructor
29  TimeException( std::string Message = "unspecified exception", std::string Tag = "*Gaudi::Time*",
31  : GaudiException( std::move( Message ), std::move( Tag ), std::move( Code ) ) {}
32 };
33 
34 struct tm;
35 #ifdef WIN32
36 typedef struct _FILETIME FILETIME;
37 #endif
38 
39 namespace Gaudi {
40 
41  class Time;
42  class TimeSpan;
43 
60  friend class Time;
61 
62  public:
63  typedef long long ValueType;
64 
66  TimeSpan() = default;
67 
68  TimeSpan( Time t );
69  TimeSpan( ValueType nsecs );
70  TimeSpan( ValueType secs, int nsecs );
71  TimeSpan( int days, int hours, int mins, int secs, int nsecs );
72 
73  int days() const;
74  int hours() const;
75  int minutes() const;
76  ValueType seconds() const;
77 
78  int lastHours() const;
79  int lastMinutes() const;
80  int lastSeconds() const;
81  int lastNSeconds() const;
82 
83  TimeSpan& operator+=( const TimeSpan& x );
84  TimeSpan& operator-=( const TimeSpan& x );
85  TimeSpan& operator*=( const TimeSpan& n );
86  TimeSpan& operator/=( const TimeSpan& n );
87  TimeSpan& operator%=( const TimeSpan& n );
88 
89  ValueType ns() const;
90 
91  friend bool operator==( const Gaudi::TimeSpan& t1, const Gaudi::TimeSpan& t2 ) { return t1.ns() == t2.ns(); }
92 
93  friend bool operator!=( const Gaudi::TimeSpan& t1, const Gaudi::TimeSpan& t2 ) { return t1.ns() != t2.ns(); }
94 
95  friend bool operator<( const Gaudi::TimeSpan& t1, const Gaudi::TimeSpan& t2 ) { return t1.ns() < t2.ns(); }
96 
97  friend bool operator<=( const Gaudi::TimeSpan& t1, const Gaudi::TimeSpan& t2 ) { return t1.ns() <= t2.ns(); }
98 
99  friend bool operator>( const Gaudi::TimeSpan& t1, const Gaudi::TimeSpan& t2 ) { return t1.ns() > t2.ns(); }
100 
101  friend bool operator>=( const Gaudi::TimeSpan& t1, const Gaudi::TimeSpan& t2 ) { return t1.ns() >= t2.ns(); }
102 
103  friend Gaudi::TimeSpan operator+( const Gaudi::TimeSpan& ts1, const Gaudi::TimeSpan& ts2 ) {
104  return Gaudi::TimeSpan( ts1.ns() + ts2.ns() );
105  }
106 
107  friend Gaudi::TimeSpan operator-( const Gaudi::TimeSpan& ts1, const Gaudi::TimeSpan& ts2 ) {
108  return Gaudi::TimeSpan( ts1.ns() - ts2.ns() );
109  }
110 
111  private:
112  ValueType m_nsecs = 0; //< The span length.
113  };
114 
238  class GAUDI_API Time {
239  friend class TimeSpan;
240 
241  public:
242  typedef long long ValueType;
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  // Taking string_view means there will never be any dynamic allocation if cond == true
338  inline void TimeAssert( bool cond, std::string_view msg = "time assertion failed" ) const {
339  if ( !cond ) throw TimeException( std::string{ msg } );
340  }
341  };
342 } // namespace Gaudi
343 
344 #include <GaudiKernel/Time.icpp>
Gaudi::Time::operator>=
friend bool operator>=(const Gaudi::Time &t1, const Gaudi::Time &t2)
Definition: Time.h:332
Gaudi::TimeSpan::operator<=
friend bool operator<=(const Gaudi::TimeSpan &t1, const Gaudi::TimeSpan &t2)
Definition: Time.h:97
GaudiPartProp.decorators.std
std
Definition: decorators.py:32
GaudiException.h
Gaudi::TimeSpan::ValueType
long long ValueType
Definition: Time.h:63
GaudiException
Definition: GaudiException.h:29
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:138
StreamBuffer.h
Gaudi::TimeSpan::ns
ValueType ns() const
Return the time span as nanoseconds.
Definition: Time.icpp:114
Gaudi::Time
Definition: Time.h:238
Gaudi::TimeSpan
Definition: Time.h:59
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:107
StatusCode
Definition: StatusCode.h:64
Message
Definition: Message.h:25
Gaudi::TimeSpan::operator>
friend bool operator>(const Gaudi::TimeSpan &t1, const Gaudi::TimeSpan &t2)
Definition: Time.h:99
Gaudi::TimeSpan::operator==
friend bool operator==(const Gaudi::TimeSpan &t1, const Gaudi::TimeSpan &t2)
Definition: Time.h:91
Gaudi::Units::ns
constexpr double ns
Definition: SystemOfUnits.h:151
compareRootHistos.ts
ts
Definition: compareRootHistos.py:488
TemplatedAlg
Definition: TemplatedAlg.cpp:22
Time.icpp
Gaudi::Time::ValueType
long long ValueType
Definition: Time.h:242
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:322
format
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:99
Gaudi::TimeSpan::operator<
friend bool operator<(const Gaudi::TimeSpan &t1, const Gaudi::TimeSpan &t2)
Definition: Time.h:95
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:330
Kernel.h
Gaudi::Time::Months
Months
Symbolic names for months.
Definition: Time.h:245
Gaudi::Time::TimeAssert
void TimeAssert(bool cond, std::string_view msg="time assertion failed") const
Definition: Time.h:338
TimeException::TimeException
TimeException(std::string Message="unspecified exception", std::string Tag="*Gaudi::Time*", StatusCode Code=StatusCode::FAILURE)
Definition: Time.h:29
Gaudi::Time::operator!=
friend bool operator!=(const Gaudi::Time &t1, const Gaudi::Time &t2)
Definition: Time.h:324
TimeException
Definition: Time.h:27
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:103
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:100
Gaudi::Time::operator<=
friend bool operator<=(const Gaudi::Time &t1, const Gaudi::Time &t2)
Definition: Time.h:328
Gaudi::TimeSpan::operator>=
friend bool operator>=(const Gaudi::TimeSpan &t1, const Gaudi::TimeSpan &t2)
Definition: Time.h:101
Gaudi::TimeSpan::operator!=
friend bool operator!=(const Gaudi::TimeSpan &t1, const Gaudi::TimeSpan &t2)
Definition: Time.h:93
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:83
Gaudi::Time::operator<
friend bool operator<(const Gaudi::Time &t1, const Gaudi::Time &t2)
Definition: Time.h:326