The Gaudi Framework  master (b9786168)
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>
17#include <cstdint>
18#include <iostream>
19#include <limits>
20#include <string>
21#include <string_view>
22#include <utility>
23
35 // Standard constructor
36 TimeException( std::string Message = "unspecified exception", std::string Tag = "*Gaudi::Time*",
38 : GaudiException( std::move( Message ), std::move( Tag ), std::move( Code ) ) {}
39};
40
41struct tm;
42
43namespace Gaudi {
44
45 class Time;
46 class TimeSpan;
47
64 friend class Time;
65
66 public:
67 using ValueType = std::int64_t;
68
70 TimeSpan() = default;
71
72 TimeSpan( Time t );
73 TimeSpan( ValueType nsecs );
74 TimeSpan( ValueType secs, int nsecs );
75 TimeSpan( int days, int hours, int mins, int secs, int nsecs );
76
77 int days() const;
78 int hours() const;
79 int minutes() const;
80 ValueType seconds() const;
81
82 int lastHours() const;
83 int lastMinutes() const;
84 int lastSeconds() const;
85 int lastNSeconds() const;
86
87 TimeSpan& operator+=( const TimeSpan& x );
88 TimeSpan& operator-=( const TimeSpan& x );
89 TimeSpan& operator*=( const TimeSpan& n );
90 TimeSpan& operator/=( const TimeSpan& n );
91 TimeSpan& operator%=( const TimeSpan& n );
92
93 ValueType ns() const;
94
95 friend auto operator<=>( const Gaudi::TimeSpan& t1, const Gaudi::TimeSpan& t2 ) = default;
96
97 friend Gaudi::TimeSpan operator+( const Gaudi::TimeSpan& ts1, const Gaudi::TimeSpan& ts2 ) {
98 return Gaudi::TimeSpan( ts1.ns() + ts2.ns() );
99 }
100
101 friend Gaudi::TimeSpan operator-( const Gaudi::TimeSpan& ts1, const Gaudi::TimeSpan& ts2 ) {
102 return Gaudi::TimeSpan( ts1.ns() - ts2.ns() );
103 }
104
105 private:
106 ValueType m_nsecs = 0; //< The span length.
107 };
108
233 friend class TimeSpan;
234
235 public:
236 using ValueType = std::int64_t;
237
239 enum Months {
242 March = 2,
243 April = 3,
244 May = 4,
245 June = 5,
246 July = 6,
252 };
253
255 inline static constexpr int SECS_PER_DAY = 86400;
256
258 inline static constexpr int SECS_PER_HOUR = 3600;
259
261 inline static constexpr ValueType SEC_NSECS = 1000000000;
262
264 Time() = default;
265
266 Time( TimeSpan ts );
267 Time( ValueType nsecs );
268 Time( ValueType secs, int nsecs );
269 Time( int year, int month, int day, int hour, int min, int sec, ValueType nsecs, bool local = true );
270 // implicit copy constructor
271 // implicit assignment operator
272 // implicit destructor
273
275 static Time epoch();
277 static Time max();
279 static Time current();
280
281 static Time build( bool local, const tm& base, TimeSpan diff = 0 );
282
283 tm split( bool local, int* nsecpart = 0 ) const;
284 tm utc( int* nsecpart = 0 ) const;
285 tm local( int* nsecpart = 0 ) const;
286
287 int year( bool local ) const;
288 int month( bool local ) const;
289 int day( bool local ) const;
290 int hour( bool local ) const;
291 int minute( bool local ) const;
292 int second( bool local ) const;
293 int nsecond() const;
294 int weekday( bool local ) const;
295 bool isdst( bool local ) const;
296
297 ValueType utcoffset( int* daylight = 0 ) const;
298 const char* timezone( int* daylight = 0 ) const;
299
300 Time& operator+=( const TimeSpan& x );
301 Time& operator-=( const TimeSpan& x );
302
303 ValueType ns() const;
304
305 std::string format( bool local, std::string spec = "%c" ) const;
306 std::string nanoformat( std::size_t minwidth = 1, std::size_t maxwidth = 9 ) const;
307
308 static bool isLeap( int year );
309
310 // Conversion helpers
311 static unsigned toDosDate( Time time );
312 static Time fromDosDate( unsigned dosDate );
313
314 friend auto operator<=>( const Gaudi::Time& t1, const Gaudi::Time& t2 ) = default;
315
316 private:
317 ValueType m_nsecs = 0; //< Time value as nsecs from #epoch().
318
319 // Taking string_view means there will never be any dynamic allocation if cond == true
320 inline void TimeAssert( bool cond, std::string_view msg = "time assertion failed" ) const {
321 if ( !cond ) throw TimeException( std::string{ msg } );
322 }
323 };
324
327 inline Time::Time( ValueType nsecs ) : m_nsecs( nsecs ) {
328 TimeAssert( m_nsecs >= 0, "cannot create a negative time" );
329 }
330
333 inline Time::Time( TimeSpan ts ) : m_nsecs( ts.m_nsecs ) {
334 TimeAssert( m_nsecs >= 0, "cannot create a negative time" );
335 }
336
339 inline Time::Time( ValueType secs, int nsecs ) : m_nsecs( secs * Time::SEC_NSECS + nsecs ) {
340 TimeAssert( m_nsecs >= 0, "cannot create a negative time" );
341 }
342
345 inline Time::ValueType Time::ns() const { return m_nsecs; }
346
349 inline Time& Time::operator+=( const TimeSpan& x ) {
350 TimeAssert( m_nsecs >= -x.m_nsecs, "time operation lead to negative time" );
351 m_nsecs += x.m_nsecs;
352 return *this;
353 }
354
357 inline Time& Time::operator-=( const TimeSpan& x ) {
358 TimeAssert( m_nsecs >= x.m_nsecs, "time operation lead to negative time" );
359 m_nsecs -= x.m_nsecs;
360 return *this;
361 }
362
364 inline Time Time::epoch() { return 0LL; }
365
367 inline Time Time::max() { return std::numeric_limits<ValueType>::max(); }
368
370 inline bool Time::isLeap( int year ) {
371 return ( ( year % 4 ) == 0 && ( ( year % 100 ) != 0 || ( year % 400 ) == 0 ) );
372 }
373
376
378 inline TimeSpan::TimeSpan( ValueType nsecs ) : m_nsecs( nsecs ) {}
379
388 inline TimeSpan::TimeSpan( ValueType secs, int nsecs ) : m_nsecs( secs * Time::SEC_NSECS + nsecs ) {}
389
401 inline TimeSpan::TimeSpan( int days, int hours, int mins, int secs, int nsecs ) {
402 m_nsecs = ( secs + 60 * ( mins + 60 * ( hours + 24 * days ) ) ) * Time::SEC_NSECS + nsecs;
403 }
404
406 inline int TimeSpan::days() const { return int( m_nsecs / Time::SEC_NSECS / Time::SECS_PER_DAY ); }
407
409 inline int TimeSpan::hours() const { return int( m_nsecs / Time::SEC_NSECS / Time::SECS_PER_HOUR ); }
410
412 inline int TimeSpan::minutes() const { return int( m_nsecs / Time::SEC_NSECS / 60 ); }
413
416
418 inline TimeSpan::ValueType TimeSpan::ns() const { return m_nsecs; }
419
422 inline int TimeSpan::lastHours() const { return hours() - days() * 24; }
423
426 inline int TimeSpan::lastMinutes() const { return minutes() - hours() * 60; }
427
430 inline int TimeSpan::lastSeconds() const { return int( seconds() - ( (ValueType)minutes() * (ValueType)60 ) ); }
431
434 inline int TimeSpan::lastNSeconds() const { return int( m_nsecs % Time::SEC_NSECS ); }
435
438 m_nsecs += x.m_nsecs;
439 return *this;
440 }
441
444 m_nsecs -= x.m_nsecs;
445 return *this;
446 }
447
450 m_nsecs *= x.m_nsecs;
451 return *this;
452 }
453
456 m_nsecs /= x.m_nsecs;
457 return *this;
458 }
459
462 m_nsecs %= x.m_nsecs;
463 return *this;
464 }
465
467 inline std::ostream& operator<<( std::ostream& out, const Gaudi::Time& time ) {
468 return out << Gaudi::TimeSpan( time ).seconds() << '.' << time.nanoformat();
469 }
470
472 inline std::ostream& operator<<( std::ostream& out, const Gaudi::TimeSpan& time ) {
473 return out << time.seconds() << '.' << Gaudi::Time( time ).nanoformat();
474 }
475} // namespace Gaudi
476
477inline Gaudi::Time operator+( const Gaudi::Time& t, const Gaudi::TimeSpan& ts ) {
478 return Gaudi::Time( t.ns() + ts.ns() );
479}
480
481inline Gaudi::Time operator+( const Gaudi::TimeSpan& ts, const Gaudi::Time& t ) {
482 return Gaudi::Time( t.ns() + ts.ns() );
483}
484
486 return Gaudi::TimeSpan( t1.ns() - t2.ns() );
487}
488
489inline Gaudi::Time operator-( const Gaudi::Time& t, const Gaudi::TimeSpan& ts ) {
490 return Gaudi::Time( t.ns() - ts.ns() );
491}
492
493inline bool operator!( const Gaudi::Time& t ) { return !t.ns(); }
494
495inline Gaudi::TimeSpan operator+( const Gaudi::TimeSpan& ts ) { return ts; }
496
497inline Gaudi::TimeSpan operator-( const Gaudi::TimeSpan& ts ) { return Gaudi::TimeSpan( -ts.ns() ); }
498
499inline bool operator!( const Gaudi::TimeSpan& ts ) { return !ts.ns(); }
500
501// --- operators for serialization ---
502
503// Output serialization
504inline StreamBuffer& operator<<( StreamBuffer& s, const Gaudi::Time& t ) { return s << t.ns(); }
505// Input serialization
508 s >> tmp;
509 t = Gaudi::Time( tmp );
510 return s;
511}
512
513// make sure that "namespace Gaudi { using ::operator<; }" continues to compile...
514// to be removed once all instances of the above have been removed from user code...
#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
StreamBuffer & operator<<(StreamBuffer &s, const Gaudi::Time &t)
Definition Time.h:504
bool operator<(backwards_compatibility_hack_time_timespan, backwards_compatibility_hack_time_timespan)
Definition Time.h:518
StreamBuffer & operator>>(StreamBuffer &s, Gaudi::Time &t)
Definition Time.h:506
Gaudi::Time operator+(const Gaudi::Time &t, const Gaudi::TimeSpan &ts)
Definition Time.h:477
bool operator!(const Gaudi::Time &t)
Definition Time.h:493
Gaudi::TimeSpan operator-(const Gaudi::Time &t1, const Gaudi::Time &t2)
Definition Time.h:485
Based on seal::Time.
Definition Time.h:232
Months
Symbolic names for months.
Definition Time.h:239
@ January
Definition Time.h:240
@ October
Definition Time.h:249
@ December
Definition Time.h:251
@ September
Definition Time.h:248
@ November
Definition Time.h:250
@ February
Definition Time.h:241
tm local(int *nsecpart=0) const
Break up the time to the standard library representation, converting it first to local time.
Definition Time.cpp:89
int hour(bool local) const
Get the hour, numbered [0, 23].
Definition Time.cpp:101
std::string nanoformat(std::size_t minwidth=1, std::size_t maxwidth=9) const
Format the nanosecond fractional part of the time as a string.
Definition Time.cpp:193
std::int64_t ValueType
Definition Time.h:236
static Time epoch()
Returns the minimum time.
Definition Time.h:364
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:58
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:109
static constexpr int SECS_PER_HOUR
Seconds in one hour hour.
Definition Time.h:258
friend class TimeSpan
Definition Time.h:233
int month(bool local) const
Get the month, numbered [0,11].
Definition Time.cpp:95
const char * timezone(int *daylight=0) const
Return the local timezone name that applies at this time value.
Definition Time.cpp:144
int weekday(bool local) const
Get the day of week, numbered [0,6] and starting from Sunday.
Definition Time.cpp:117
static Time max()
Returns the maximum time.
Definition Time.h:367
int nsecond() const
Get the nanoseconds.
Definition Time.cpp:114
static bool isLeap(int year)
Check if the year is a leap-year.
Definition Time.h:370
ValueType ns() const
Return the time as nanoseconds since 00:00:00 on January 1, 1970 in UTC.
Definition Time.h:345
int minute(bool local) const
Get the minute, numbered [0, 59].
Definition Time.cpp:104
static Time fromDosDate(unsigned dosDate)
Convert the MS-DOS date dosDate into a Time.
Definition Time.cpp:229
Time & operator-=(const TimeSpan &x)
Subtract the specified amount from the time.
Definition Time.h:357
friend auto operator<=>(const Gaudi::Time &t1, const Gaudi::Time &t2)=default
bool isdst(bool local) const
Check whether daylight savings is in effect.
Definition Time.cpp:122
Time()=default
Initialize an empty (zero) time value.
int year(bool local) const
Get the year.
Definition Time.cpp:92
static constexpr int SECS_PER_DAY
Seconds in 24 hours.
Definition Time.h:255
Time & operator+=(const TimeSpan &x)
Add the specified amount to the time.
Definition Time.h:349
tm utc(int *nsecpart=0) const
Break up the time to the standard library representation, keeping it in UTC.
Definition Time.cpp:84
ValueType m_nsecs
Definition Time.h:317
static Time current()
Returns the current time.
Definition Time.cpp:41
int day(bool local) const
Get the day of month, numbered [1,31].
Definition Time.cpp:98
static constexpr ValueType SEC_NSECS
Nanoseconds in one second.
Definition Time.h:261
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:130
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:67
void TimeAssert(bool cond, std::string_view msg="time assertion failed") const
Definition Time.h:320
static unsigned toDosDate(Time time)
Convert the Time t into a MS-DOS date format.
Definition Time.cpp:215
Based on seal::TimeSpan.
Definition Time.h:63
TimeSpan & operator/=(const TimeSpan &n)
Divide a time span.
Definition Time.h:455
int lastNSeconds() const
Get the number of nanoseconds in the last incomplete second of the span.
Definition Time.h:434
ValueType seconds() const
Get the number of complete seconds in the span.
Definition Time.h:415
int lastSeconds() const
Get the number of complete seconds in the last incomplete minute of the span.
Definition Time.h:430
ValueType ns() const
Return the time span as nanoseconds.
Definition Time.h:418
TimeSpan & operator-=(const TimeSpan &x)
Subtract from a time span.
Definition Time.h:443
friend Gaudi::TimeSpan operator-(const Gaudi::TimeSpan &ts1, const Gaudi::TimeSpan &ts2)
Definition Time.h:101
friend class Time
Definition Time.h:64
TimeSpan & operator+=(const TimeSpan &x)
Add to a time span.
Definition Time.h:437
friend Gaudi::TimeSpan operator+(const Gaudi::TimeSpan &ts1, const Gaudi::TimeSpan &ts2)
Definition Time.h:97
int days() const
Get the number of complete days in the span.
Definition Time.h:406
TimeSpan & operator*=(const TimeSpan &n)
Multiply a time span.
Definition Time.h:449
int hours() const
Get the number of complete hours in the span.
Definition Time.h:409
TimeSpan & operator%=(const TimeSpan &n)
Compute a modulo of a time span.
Definition Time.h:461
TimeSpan()=default
Initialize an empty (zero) time difference.
int minutes() const
Get the number of complete minutes in the span.
Definition Time.h:412
int lastMinutes() const
Get the number of complete minutes in the last incomplete hour of the span.
Definition Time.h:426
friend auto operator<=>(const Gaudi::TimeSpan &t1, const Gaudi::TimeSpan &t2)=default
int lastHours() const
Get the number of complete hours in the last incomplete day of the span.
Definition Time.h:422
ValueType m_nsecs
Definition Time.h:106
std::int64_t ValueType
Definition Time.h:67
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
The stream buffer is a small object collecting object data.
This file provides a Grammar for the type Gaudi::Accumulators::Axis It allows to use that type from p...
Definition __init__.py:1
std::ostream & operator<<(std::ostream &o, const Gaudi::StringKey &key)
printout of the object reply on the native printout for the string
Definition StringKey.h:176
STL namespace.
Exception thrown by Gaudi::Time.
Definition Time.h:34
TimeException(std::string Message="unspecified exception", std::string Tag="*Gaudi::Time*", StatusCode Code=StatusCode::FAILURE)
Definition Time.h:36