The Gaudi Framework  master (37c0b60a)
Time.icpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 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_ICPP
12 #define GAUDIKERNEL_TIME_ICPP 1
13 
14 // Implementation of inline function for classes Gaudi::Time and Gaudi::TimeSpan
15 
16 namespace Gaudi {
17 
20  inline Time::Time( ValueType nsecs ) : m_nsecs( nsecs ) {
21  TimeAssert( m_nsecs >= 0, "cannot create a negative time" );
22  }
23 
26  inline Time::Time( TimeSpan ts ) : m_nsecs( ts.m_nsecs ) {
27  TimeAssert( m_nsecs >= 0, "cannot create a negative time" );
28  }
29 
32  inline Time::Time( ValueType secs, int nsecs ) : m_nsecs( secs * Time::SEC_NSECS + nsecs ) {
33  TimeAssert( m_nsecs >= 0, "cannot create a negative time" );
34  }
35 
38  inline Time::ValueType Time::ns() const { return m_nsecs; }
39 
42  inline Time& Time::operator+=( const TimeSpan& x ) {
43  TimeAssert( m_nsecs >= -x.m_nsecs, "time operation lead to negative time" );
44  m_nsecs += x.m_nsecs;
45  return *this;
46  }
47 
50  inline Time& Time::operator-=( const TimeSpan& x ) {
51  TimeAssert( m_nsecs >= x.m_nsecs, "time operation lead to negative time" );
52  m_nsecs -= x.m_nsecs;
53  return *this;
54  }
55 
57  inline Time Time::epoch() { return 0LL; }
58 
60  inline Time Time::max() { return 0x7fffffffffffffffLL; }
61 
63  inline bool Time::isLeap( int year ) {
64  return ( ( year % 4 ) == 0 && ( ( year % 100 ) != 0 || ( year % 400 ) == 0 ) );
65  }
66 
70 
72  inline TimeSpan::TimeSpan( Time t ) : m_nsecs( t.m_nsecs ) {}
73 
75  inline TimeSpan::TimeSpan( ValueType nsecs ) : m_nsecs( nsecs ) {}
76 
85  inline TimeSpan::TimeSpan( ValueType secs, int nsecs ) : m_nsecs( secs * Time::SEC_NSECS + nsecs ) {}
86 
98  inline TimeSpan::TimeSpan( int days, int hours, int mins, int secs, int nsecs ) {
99  m_nsecs = ( secs + 60 * ( mins + 60 * ( hours + 24 * days ) ) ) * Time::SEC_NSECS + nsecs;
100  }
101 
103  inline int TimeSpan::days() const { return int( m_nsecs / Time::SEC_NSECS / Time::SECS_PER_DAY ); }
104 
106  inline int TimeSpan::hours() const { return int( m_nsecs / Time::SEC_NSECS / Time::SECS_PER_HOUR ); }
107 
109  inline int TimeSpan::minutes() const { return int( m_nsecs / Time::SEC_NSECS / 60 ); }
110 
113 
115  inline TimeSpan::ValueType TimeSpan::ns() const { return m_nsecs; }
116 
119  inline int TimeSpan::lastHours() const { return hours() - days() * 24; }
120 
123  inline int TimeSpan::lastMinutes() const { return minutes() - hours() * 60; }
124 
127  inline int TimeSpan::lastSeconds() const { return int( seconds() - ( (ValueType)minutes() * (ValueType)60 ) ); }
128 
131  inline int TimeSpan::lastNSeconds() const { return int( m_nsecs % Time::SEC_NSECS ); }
132 
134  inline TimeSpan& TimeSpan::operator+=( const TimeSpan& x ) {
135  m_nsecs += x.m_nsecs;
136  return *this;
137  }
138 
140  inline TimeSpan& TimeSpan::operator-=( const TimeSpan& x ) {
141  m_nsecs -= x.m_nsecs;
142  return *this;
143  }
144 
146  inline TimeSpan& TimeSpan::operator*=( const TimeSpan& x ) {
147  m_nsecs *= x.m_nsecs;
148  return *this;
149  }
150 
152  inline TimeSpan& TimeSpan::operator/=( const TimeSpan& x ) {
153  m_nsecs /= x.m_nsecs;
154  return *this;
155  }
156 
158  inline TimeSpan& TimeSpan::operator%=( const TimeSpan& x ) {
159  m_nsecs %= x.m_nsecs;
160  return *this;
161  }
162 
165  return out << Gaudi::TimeSpan( time ).seconds() << '.' << time.nanoformat();
166  }
167 
170  return out << time.seconds() << '.' << Gaudi::Time( time ).nanoformat();
171  }
172 } // namespace Gaudi
173 
174 //<<<<<< INLINE PUBLIC FUNCTIONS >>>>>>
176  return Gaudi::Time( t.ns() + ts.ns() );
177 }
178 
180  return Gaudi::Time( t.ns() + ts.ns() );
181 }
182 
184  return Gaudi::TimeSpan( t1.ns() - t2.ns() );
185 }
186 
188  return Gaudi::Time( t.ns() - ts.ns() );
189 }
190 
191 inline bool operator!( const Gaudi::Time& t ) { return !t.ns(); }
192 
196 
197 inline Gaudi::TimeSpan operator+( const Gaudi::TimeSpan& ts ) { return ts; }
198 
199 inline Gaudi::TimeSpan operator-( const Gaudi::TimeSpan& ts ) { return Gaudi::TimeSpan( -ts.ns() ); }
200 
201 inline bool operator!( const Gaudi::TimeSpan& ts ) { return !ts.ns(); }
202 
203 // --- operators for serialization ---
204 
205 // Output serialization
206 inline StreamBuffer& operator<<( StreamBuffer& s, const Gaudi::Time& t ) { return s << t.ns(); }
207 // Input serialization
210  s >> tmp;
211  t = Gaudi::Time( tmp );
212  return s;
213 }
214 
215 // make sure that "namespace Gaudi { using ::operator<; }" continues to compile...
216 // to be removed once all instances of the above have been removed from user code...
219 };
221  return false;
222 }
223 
224 #endif
Gaudi::TimeSpan::operator-=
TimeSpan & operator-=(const TimeSpan &x)
Subtract from a time span.
Definition: Time.icpp:140
operator+
Gaudi::Time operator+(const Gaudi::Time &t, const Gaudi::TimeSpan &ts)
Definition: Time.icpp:175
operator-
Gaudi::TimeSpan operator-(const Gaudi::Time &t1, const Gaudi::Time &t2)
Definition: Time.icpp:183
Gaudi::Time::operator+=
Time & operator+=(const TimeSpan &x)
Add the specified amount to the time.
Definition: Time.icpp:42
operator<
bool operator<(backwards_compatibility_hack_time_timespan, backwards_compatibility_hack_time_timespan)
Definition: Time.icpp:220
Gaudi::TimeSpan::minutes
int minutes() const
Get the number of complete minutes in the span.
Definition: Time.icpp:109
Gaudi::TimeSpan::seconds
ValueType seconds() const
Get the number of complete seconds in the span.
Definition: Time.icpp:112
Gaudi::Time::year
int year(bool local) const
Get the year.
Definition: Time.cpp:176
Gaudi::TimeSpan::ValueType
long long ValueType
Definition: Time.h:66
gaudirun.s
string s
Definition: gaudirun.py:346
Gaudi::Time::SECS_PER_DAY
static const int SECS_PER_DAY
Seconds in 24 hours.
Definition: Time.h:264
Gaudi::TimeSpan::m_nsecs
ValueType m_nsecs
Definition: Time.h:115
Gaudi::Time::epoch
static Time epoch()
Returns the minimum time.
Definition: Time.icpp:57
Gaudi::Time::Time
Time()=default
Initialize an empty (zero) time value.
Gaudi::Time::nanoformat
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:302
Gaudi::TimeSpan::lastNSeconds
int lastNSeconds() const
Get the number of nanoseconds in the last incomplete second of the span.
Definition: Time.icpp:131
Gaudi::Time::max
static Time max()
Returns the maximum time.
Definition: Time.icpp:60
Gaudi::Time::m_nsecs
ValueType m_nsecs
Definition: Time.h:338
operator<<
StreamBuffer & operator<<(StreamBuffer &s, const Gaudi::Time &t)
Definition: Time.icpp:206
operator!
bool operator!(const Gaudi::Time &t)
Definition: Time.icpp:191
Gaudi::TimeSpan::ns
ValueType ns() const
Return the time span as nanoseconds.
Definition: Time.icpp:115
StreamBuffer
Definition: StreamBuffer.h:52
Gaudi::Time
Definition: Time.h:241
Gaudi::TimeSpan
Definition: Time.h:62
bug_34121.t
t
Definition: bug_34121.py:31
backwards_compatibility_hack_time_timespan::backwards_compatibility_hack_time_timespan
backwards_compatibility_hack_time_timespan()=delete
std::ostream
STL class.
Gaudi::Time::isLeap
static bool isLeap(int year)
Check if the year is a leap-year.
Definition: Time.icpp:63
compareRootHistos.ts
ts
Definition: compareRootHistos.py:488
TemplatedAlg
Definition: TemplatedAlg.cpp:22
Gaudi::TimeSpan::operator/=
TimeSpan & operator/=(const TimeSpan &n)
Divide a time span.
Definition: Time.icpp:152
Gaudi::TimeSpan::lastSeconds
int lastSeconds() const
Get the number of complete seconds in the last incomplete minute of the span.
Definition: Time.icpp:127
Gaudi::Time::ValueType
long long ValueType
Definition: Time.h:245
Gaudi::TimeSpan::TimeSpan
TimeSpan()=default
Initialize an empty (zero) time difference.
Gaudi::TimeSpan::operator%=
TimeSpan & operator%=(const TimeSpan &n)
Compute a modulo of a time span.
Definition: Time.icpp:158
Gaudi::Time::ns
ValueType ns() const
Return the time as nanoseconds since 00:00:00 on January 1, 1970 in UTC.
Definition: Time.icpp:38
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
Gaudi::TimeSpan::operator+=
TimeSpan & operator+=(const TimeSpan &x)
Add to a time span.
Definition: Time.icpp:134
Gaudi::TimeSpan::operator*=
TimeSpan & operator*=(const TimeSpan &n)
Multiply a time span.
Definition: Time.icpp:146
Gaudi::TimeSpan::days
int days() const
Get the number of complete days in the span.
Definition: Time.icpp:103
operator>>
StreamBuffer & operator>>(StreamBuffer &s, Gaudi::Time &t)
Definition: Time.icpp:208
Gaudi::Time::SEC_NSECS
static const ValueType SEC_NSECS
Nanoseconds in one second.
Definition: Time.h:270
Gaudi::TimeSpan::lastMinutes
int lastMinutes() const
Get the number of complete minutes in the last incomplete hour of the span.
Definition: Time.icpp:123
Gaudi::TimeSpan::hours
int hours() const
Get the number of complete hours in the span.
Definition: Time.icpp:106
Gaudi::operator<<
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:208
Gaudi::Time::TimeAssert
void TimeAssert(bool cond, std::string_view msg="time assertion failed") const
Definition: Time.h:341
Gaudi::Time::SECS_PER_HOUR
static const int SECS_PER_HOUR
Seconds in one hour hour.
Definition: Time.h:267
backwards_compatibility_hack_time_timespan
Definition: Time.icpp:217
Gaudi::TimeSpan::lastHours
int lastHours() const
Get the number of complete hours in the last incomplete day of the span.
Definition: Time.icpp:119
PrepareBase.out
out
Definition: PrepareBase.py:20
Gaudi::Time::operator-=
Time & operator-=(const TimeSpan &x)
Subtract the specified amount from the time.
Definition: Time.icpp:50