The Gaudi Framework  master (82fdf313)
Loading...
Searching...
No Matches
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
34struct tm;
35
36namespace 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
236 friend class TimeSpan;
237
238 public:
239 typedef long long ValueType;
240
242 enum Months {
245 March = 2,
246 April = 3,
247 May = 4,
248 June = 5,
249 July = 6,
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>
#define GAUDI_API
Definition Kernel.h:49
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition MsgStream.cpp:93
TemplatedAlg< int, std::vector< std::string > > t1
TemplatedAlg< double, bool > t2
Based on seal::Time.
Definition Time.h:235
Months
Symbolic names for months.
Definition Time.h:242
@ January
Definition Time.h:243
@ October
Definition Time.h:252
@ December
Definition Time.h:254
@ September
Definition Time.h:251
@ November
Definition Time.h:253
@ February
Definition Time.h:244
friend bool operator<(const Gaudi::Time &t1, const Gaudi::Time &t2)
Definition Time.h:321
tm local(int *nsecpart=0) const
Break up the time to the standard library representation, converting it first to local time.
Definition Time.cpp:91
int hour(bool local) const
Get the hour, numbered [0, 23].
Definition Time.cpp:103
static Time epoch()
Returns the minimum time.
Definition Time.icpp:56
static Time build(bool local, const tm &base, TimeSpan diff=0)
Construct a time from local time base and a delta diff.
Definition Time.cpp:60
int second(bool local) const
Get the seconds, numbered [0,61] (allowing one or two leap seconds, years with leap seconds can have ...
Definition Time.cpp:111
friend class TimeSpan
Definition Time.h:236
int month(bool local) const
Get the month, numbered [0,11].
Definition Time.cpp:97
const char * timezone(int *daylight=0) const
Return the local timezone name that applies at this time value.
Definition Time.cpp:146
int weekday(bool local) const
Get the day of week, numbered [0,6] and starting from Sunday.
Definition Time.cpp:119
static Time max()
Returns the maximum time.
Definition Time.icpp:59
friend bool operator>(const Gaudi::Time &t1, const Gaudi::Time &t2)
Definition Time.h:325
int nsecond() const
Get the nanoseconds.
Definition Time.cpp:116
static bool isLeap(int year)
Check if the year is a leap-year.
Definition Time.icpp:62
std::string nanoformat(size_t minwidth=1, size_t maxwidth=9) const
Format the nanosecond fractional part of the time as a string.
Definition Time.cpp:197
ValueType ns() const
Return the time as nanoseconds since 00:00:00 on January 1, 1970 in UTC.
Definition Time.icpp:37
int minute(bool local) const
Get the minute, numbered [0, 59].
Definition Time.cpp:106
static Time fromDosDate(unsigned dosDate)
Convert the MS-DOS date dosDate into a Time.
Definition Time.cpp:233
Time & operator-=(const TimeSpan &x)
Subtract the specified amount from the time.
Definition Time.icpp:49
friend bool operator<=(const Gaudi::Time &t1, const Gaudi::Time &t2)
Definition Time.h:323
friend bool operator>=(const Gaudi::Time &t1, const Gaudi::Time &t2)
Definition Time.h:327
bool isdst(bool local) const
Check whether daylight savings is in effect.
Definition Time.cpp:124
Time()=default
Initialize an empty (zero) time value.
int year(bool local) const
Get the year.
Definition Time.cpp:94
Time & operator+=(const TimeSpan &x)
Add the specified amount to the time.
Definition Time.icpp:41
tm utc(int *nsecpart=0) const
Break up the time to the standard library representation, keeping it in UTC.
Definition Time.cpp:86
ValueType m_nsecs
Definition Time.h:330
static Time current()
Returns the current time.
Definition Time.cpp:43
int day(bool local) const
Get the day of month, numbered [1,31].
Definition Time.cpp:100
friend bool operator!=(const Gaudi::Time &t1, const Gaudi::Time &t2)
Definition Time.h:319
static const ValueType SEC_NSECS
Nanoseconds in one second.
Definition Time.h:264
static const int SECS_PER_HOUR
Seconds in one hour hour.
Definition Time.h:261
long long ValueType
Definition Time.h:239
ValueType utcoffset(int *daylight=0) const
Return the number of nanoseconds that needs to be added to UTC to translate this time to the local ti...
Definition Time.cpp:132
tm split(bool local, int *nsecpart=0) const
Break up the time to the standard representation, either in UTC (if local is false) or local time (if...
Definition Time.cpp:69
void TimeAssert(bool cond, std::string_view msg="time assertion failed") const
Definition Time.h:333
static const int SECS_PER_DAY
Seconds in 24 hours.
Definition Time.h:258
friend bool operator==(const Gaudi::Time &t1, const Gaudi::Time &t2)
Definition Time.h:317
static unsigned toDosDate(Time time)
Convert the Time t into a MS-DOS date format.
Definition Time.cpp:219
Based on seal::TimeSpan.
Definition Time.h:56
TimeSpan & operator/=(const TimeSpan &n)
Divide a time span.
Definition Time.icpp:151
int lastNSeconds() const
Get the number of nanoseconds in the last incomplete second of the span.
Definition Time.icpp:130
ValueType seconds() const
Get the number of complete seconds in the span.
Definition Time.icpp:111
int lastSeconds() const
Get the number of complete seconds in the last incomplete minute of the span.
Definition Time.icpp:126
ValueType ns() const
Return the time span as nanoseconds.
Definition Time.icpp:114
TimeSpan & operator-=(const TimeSpan &x)
Subtract from a time span.
Definition Time.icpp:139
friend Gaudi::TimeSpan operator-(const Gaudi::TimeSpan &ts1, const Gaudi::TimeSpan &ts2)
Definition Time.h:104
friend class Time
Definition Time.h:57
friend bool operator<=(const Gaudi::TimeSpan &t1, const Gaudi::TimeSpan &t2)
Definition Time.h:94
TimeSpan & operator+=(const TimeSpan &x)
Add to a time span.
Definition Time.icpp:133
friend Gaudi::TimeSpan operator+(const Gaudi::TimeSpan &ts1, const Gaudi::TimeSpan &ts2)
Definition Time.h:100
int days() const
Get the number of complete days in the span.
Definition Time.icpp:102
TimeSpan & operator*=(const TimeSpan &n)
Multiply a time span.
Definition Time.icpp:145
int hours() const
Get the number of complete hours in the span.
Definition Time.icpp:105
TimeSpan & operator%=(const TimeSpan &n)
Compute a modulo of a time span.
Definition Time.icpp:157
friend bool operator>(const Gaudi::TimeSpan &t1, const Gaudi::TimeSpan &t2)
Definition Time.h:96
TimeSpan()=default
Initialize an empty (zero) time difference.
int minutes() const
Get the number of complete minutes in the span.
Definition Time.icpp:108
int lastMinutes() const
Get the number of complete minutes in the last incomplete hour of the span.
Definition Time.icpp:122
friend bool operator>=(const Gaudi::TimeSpan &t1, const Gaudi::TimeSpan &t2)
Definition Time.h:98
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
int lastHours() const
Get the number of complete hours in the last incomplete day of the span.
Definition Time.icpp:118
long long ValueType
Definition Time.h:60
friend bool operator<(const Gaudi::TimeSpan &t1, const Gaudi::TimeSpan &t2)
Definition Time.h:92
ValueType m_nsecs
Definition Time.h:109
GaudiException(std::string Message, std::string Tag, StatusCode Code)
Constructor (1)
friend class StatusCode
The Message class.
Definition Message.h:25
constexpr static const auto FAILURE
Definition StatusCode.h:100
This file provides a Grammar for the type Gaudi::Accumulators::Axis It allows to use that type from p...
Definition __init__.py:1
STL namespace.
Exception thrown by Gaudi::Time.
Definition Time.h:27
TimeException(std::string Message="unspecified exception", std::string Tag="*Gaudi::Time*", StatusCode Code=StatusCode::FAILURE)
Definition Time.h:29