The Gaudi Framework  master (d98a2936)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 
36 namespace Gaudi {
37 
38  class Time;
39  class TimeSpan;
40 
57  friend class Time;
58 
59  public:
60  typedef long long ValueType;
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  return Gaudi::TimeSpan( ts1.ns() + ts2.ns() );
102  }
103 
104  friend Gaudi::TimeSpan operator-( const Gaudi::TimeSpan& ts1, const Gaudi::TimeSpan& ts2 ) {
105  return Gaudi::TimeSpan( ts1.ns() - ts2.ns() );
106  }
107 
108  private:
109  ValueType m_nsecs = 0; //< The span length.
110  };
111 
235  class GAUDI_API Time {
236  friend class TimeSpan;
237 
238  public:
239  typedef long long ValueType;
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, ValueType nsecs, bool local = true );
273  // implicit copy constructor
274  // implicit assignment operator
275  // implicit destructor
276 
278  static Time epoch();
280  static Time max();
282  static Time current();
283 
284  static Time build( bool local, const tm& base, TimeSpan diff = 0 );
285 
286  tm split( bool local, int* nsecpart = 0 ) const;
287  tm utc( int* nsecpart = 0 ) const;
288  tm local( int* nsecpart = 0 ) const;
289 
290  int year( bool local ) const;
291  int month( bool local ) const;
292  int day( bool local ) const;
293  int hour( bool local ) const;
294  int minute( bool local ) const;
295  int second( bool local ) const;
296  int nsecond() const;
297  int weekday( bool local ) const;
298  bool isdst( bool local ) const;
299 
300  ValueType utcoffset( int* daylight = 0 ) const;
301  const char* timezone( int* daylight = 0 ) const;
302 
303  Time& operator+=( const TimeSpan& x );
304  Time& operator-=( const TimeSpan& x );
305 
306  ValueType ns() const;
307 
308  std::string format( bool local, std::string spec = "%c" ) const;
309  std::string nanoformat( size_t minwidth = 1, size_t maxwidth = 9 ) const;
310 
311  static bool isLeap( int year );
312 
313  // Conversion helpers
314  static unsigned toDosDate( Time time );
315  static Time fromDosDate( unsigned dosDate );
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  friend bool operator>=( const Gaudi::Time& t1, const Gaudi::Time& t2 ) { return t1.ns() >= t2.ns(); }
328 
329  private:
330  ValueType m_nsecs = 0; //< Time value as nsecs from #epoch().
331 
332  // Taking string_view means there will never be any dynamic allocation if cond == true
333  inline void TimeAssert( bool cond, std::string_view msg = "time assertion failed" ) const {
334  if ( !cond ) throw TimeException( std::string{ msg } );
335  }
336  };
337 } // namespace Gaudi
338 
339 #include <GaudiKernel/Time.icpp>
Gaudi::Time::operator>=
friend bool operator>=(const Gaudi::Time &t1, const Gaudi::Time &t2)
Definition: Time.h:327
Gaudi::TimeSpan::operator<=
friend bool operator<=(const Gaudi::TimeSpan &t1, const Gaudi::TimeSpan &t2)
Definition: Time.h:94
GaudiPartProp.decorators.std
std
Definition: decorators.py:32
GaudiException.h
Gaudi::TimeSpan::ValueType
long long ValueType
Definition: Time.h:60
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:235
Gaudi::TimeSpan
Definition: Time.h:56
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:104
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:96
Gaudi::TimeSpan::operator==
friend bool operator==(const Gaudi::TimeSpan &t1, const Gaudi::TimeSpan &t2)
Definition: Time.h:88
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:239
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:317
format
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:93
Gaudi::TimeSpan::operator<
friend bool operator<(const Gaudi::TimeSpan &t1, const Gaudi::TimeSpan &t2)
Definition: Time.h:92
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:325
Kernel.h
Gaudi::Time::Months
Months
Symbolic names for months.
Definition: Time.h:242
Gaudi::Time::TimeAssert
void TimeAssert(bool cond, std::string_view msg="time assertion failed") const
Definition: Time.h:333
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:319
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:100
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:323
Gaudi::TimeSpan::operator>=
friend bool operator>=(const Gaudi::TimeSpan &t1, const Gaudi::TimeSpan &t2)
Definition: Time.h:98
Gaudi::TimeSpan::operator!=
friend bool operator!=(const Gaudi::TimeSpan &t1, const Gaudi::TimeSpan &t2)
Definition: Time.h:90
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:49
Gaudi::Time::operator<
friend bool operator<(const Gaudi::Time &t1, const Gaudi::Time &t2)
Definition: Time.h:321