The Gaudi Framework  master (d98a2936)
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 #pragma once
12 
13 // Implementation of inline function for classes Gaudi::Time and Gaudi::TimeSpan
14 
15 namespace Gaudi {
16 
19  inline Time::Time( ValueType nsecs ) : m_nsecs( nsecs ) {
20  TimeAssert( m_nsecs >= 0, "cannot create a negative time" );
21  }
22 
25  inline Time::Time( TimeSpan ts ) : m_nsecs( ts.m_nsecs ) {
26  TimeAssert( m_nsecs >= 0, "cannot create a negative time" );
27  }
28 
31  inline Time::Time( ValueType secs, int nsecs ) : m_nsecs( secs * Time::SEC_NSECS + nsecs ) {
32  TimeAssert( m_nsecs >= 0, "cannot create a negative time" );
33  }
34 
37  inline Time::ValueType Time::ns() const { return m_nsecs; }
38 
41  inline Time& Time::operator+=( const TimeSpan& x ) {
42  TimeAssert( m_nsecs >= -x.m_nsecs, "time operation lead to negative time" );
43  m_nsecs += x.m_nsecs;
44  return *this;
45  }
46 
49  inline Time& Time::operator-=( const TimeSpan& x ) {
50  TimeAssert( m_nsecs >= x.m_nsecs, "time operation lead to negative time" );
51  m_nsecs -= x.m_nsecs;
52  return *this;
53  }
54 
56  inline Time Time::epoch() { return 0LL; }
57 
59  inline Time Time::max() { return 0x7fffffffffffffffLL; }
60 
62  inline bool Time::isLeap( int year ) {
63  return ( ( year % 4 ) == 0 && ( ( year % 100 ) != 0 || ( year % 400 ) == 0 ) );
64  }
65 
69 
71  inline TimeSpan::TimeSpan( Time t ) : m_nsecs( t.m_nsecs ) {}
72 
74  inline TimeSpan::TimeSpan( ValueType nsecs ) : m_nsecs( nsecs ) {}
75 
84  inline TimeSpan::TimeSpan( ValueType secs, int nsecs ) : m_nsecs( secs * Time::SEC_NSECS + nsecs ) {}
85 
97  inline TimeSpan::TimeSpan( int days, int hours, int mins, int secs, int nsecs ) {
98  m_nsecs = ( secs + 60 * ( mins + 60 * ( hours + 24 * days ) ) ) * Time::SEC_NSECS + nsecs;
99  }
100 
102  inline int TimeSpan::days() const { return int( m_nsecs / Time::SEC_NSECS / Time::SECS_PER_DAY ); }
103 
105  inline int TimeSpan::hours() const { return int( m_nsecs / Time::SEC_NSECS / Time::SECS_PER_HOUR ); }
106 
108  inline int TimeSpan::minutes() const { return int( m_nsecs / Time::SEC_NSECS / 60 ); }
109 
112 
114  inline TimeSpan::ValueType TimeSpan::ns() const { return m_nsecs; }
115 
118  inline int TimeSpan::lastHours() const { return hours() - days() * 24; }
119 
122  inline int TimeSpan::lastMinutes() const { return minutes() - hours() * 60; }
123 
126  inline int TimeSpan::lastSeconds() const { return int( seconds() - ( (ValueType)minutes() * (ValueType)60 ) ); }
127 
130  inline int TimeSpan::lastNSeconds() const { return int( m_nsecs % Time::SEC_NSECS ); }
131 
133  inline TimeSpan& TimeSpan::operator+=( const TimeSpan& x ) {
134  m_nsecs += x.m_nsecs;
135  return *this;
136  }
137 
139  inline TimeSpan& TimeSpan::operator-=( const TimeSpan& x ) {
140  m_nsecs -= x.m_nsecs;
141  return *this;
142  }
143 
145  inline TimeSpan& TimeSpan::operator*=( const TimeSpan& x ) {
146  m_nsecs *= x.m_nsecs;
147  return *this;
148  }
149 
151  inline TimeSpan& TimeSpan::operator/=( const TimeSpan& x ) {
152  m_nsecs /= x.m_nsecs;
153  return *this;
154  }
155 
157  inline TimeSpan& TimeSpan::operator%=( const TimeSpan& x ) {
158  m_nsecs %= x.m_nsecs;
159  return *this;
160  }
161 
163  inline std::ostream& operator<<( std::ostream& out, const Gaudi::Time& time ) {
164  return out << Gaudi::TimeSpan( time ).seconds() << '.' << time.nanoformat();
165  }
166 
168  inline std::ostream& operator<<( std::ostream& out, const Gaudi::TimeSpan& time ) {
169  return out << time.seconds() << '.' << Gaudi::Time( time ).nanoformat();
170  }
171 } // namespace Gaudi
172 
173 //<<<<<< INLINE PUBLIC FUNCTIONS >>>>>>
175  return Gaudi::Time( t.ns() + ts.ns() );
176 }
177 
179  return Gaudi::Time( t.ns() + ts.ns() );
180 }
181 
183  return Gaudi::TimeSpan( t1.ns() - t2.ns() );
184 }
185 
187  return Gaudi::Time( t.ns() - ts.ns() );
188 }
189 
190 inline bool operator!( const Gaudi::Time& t ) { return !t.ns(); }
191 
195 
196 inline Gaudi::TimeSpan operator+( const Gaudi::TimeSpan& ts ) { return ts; }
197 
198 inline Gaudi::TimeSpan operator-( const Gaudi::TimeSpan& ts ) { return Gaudi::TimeSpan( -ts.ns() ); }
199 
200 inline bool operator!( const Gaudi::TimeSpan& ts ) { return !ts.ns(); }
201 
202 // --- operators for serialization ---
203 
204 // Output serialization
205 inline StreamBuffer& operator<<( StreamBuffer& s, const Gaudi::Time& t ) { return s << t.ns(); }
206 // Input serialization
209  s >> tmp;
210  t = Gaudi::Time( tmp );
211  return s;
212 }
213 
214 // make sure that "namespace Gaudi { using ::operator<; }" continues to compile...
215 // to be removed once all instances of the above have been removed from user code...
218 };
220  return false;
221 }
Gaudi::TimeSpan::operator-=
TimeSpan & operator-=(const TimeSpan &x)
Subtract from a time span.
Definition: Time.icpp:139
operator+
Gaudi::Time operator+(const Gaudi::Time &t, const Gaudi::TimeSpan &ts)
Definition: Time.icpp:174
operator-
Gaudi::TimeSpan operator-(const Gaudi::Time &t1, const Gaudi::Time &t2)
Definition: Time.icpp:182
Gaudi::Time::operator+=
Time & operator+=(const TimeSpan &x)
Add the specified amount to the time.
Definition: Time.icpp:41
operator<
bool operator<(backwards_compatibility_hack_time_timespan, backwards_compatibility_hack_time_timespan)
Definition: Time.icpp:219
Gaudi::TimeSpan::minutes
int minutes() const
Get the number of complete minutes in the span.
Definition: Time.icpp:108
Gaudi::TimeSpan::seconds
ValueType seconds() const
Get the number of complete seconds in the span.
Definition: Time.icpp:111
Gaudi::Time::year
int year(bool local) const
Get the year.
Definition: Time.cpp:94
Gaudi::TimeSpan::ValueType
long long ValueType
Definition: Time.h:60
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:258
Gaudi::TimeSpan::m_nsecs
ValueType m_nsecs
Definition: Time.h:109
Gaudi::Time::epoch
static Time epoch()
Returns the minimum time.
Definition: Time.icpp:56
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:197
Gaudi::TimeSpan::lastNSeconds
int lastNSeconds() const
Get the number of nanoseconds in the last incomplete second of the span.
Definition: Time.icpp:130
Gaudi::Time::max
static Time max()
Returns the maximum time.
Definition: Time.icpp:59
Gaudi::Time::m_nsecs
ValueType m_nsecs
Definition: Time.h:330
operator<<
StreamBuffer & operator<<(StreamBuffer &s, const Gaudi::Time &t)
Definition: Time.icpp:205
operator!
bool operator!(const Gaudi::Time &t)
Definition: Time.icpp:190
Gaudi::TimeSpan::ns
ValueType ns() const
Return the time span as nanoseconds.
Definition: Time.icpp:114
StreamBuffer
Definition: StreamBuffer.h:48
Gaudi::Time
Definition: Time.h:235
Gaudi::TimeSpan
Definition: Time.h:56
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
Gaudi::Time::isLeap
static bool isLeap(int year)
Check if the year is a leap-year.
Definition: Time.icpp:62
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:151
Gaudi::TimeSpan::lastSeconds
int lastSeconds() const
Get the number of complete seconds in the last incomplete minute of the span.
Definition: Time.icpp:126
Gaudi::Time::ValueType
long long ValueType
Definition: Time.h:239
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:157
Gaudi::Time::ns
ValueType ns() const
Return the time as nanoseconds since 00:00:00 on January 1, 1970 in UTC.
Definition: Time.icpp:37
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:133
Gaudi::TimeSpan::operator*=
TimeSpan & operator*=(const TimeSpan &n)
Multiply a time span.
Definition: Time.icpp:145
Gaudi::TimeSpan::days
int days() const
Get the number of complete days in the span.
Definition: Time.icpp:102
operator>>
StreamBuffer & operator>>(StreamBuffer &s, Gaudi::Time &t)
Definition: Time.icpp:207
Gaudi::Time::SEC_NSECS
static const ValueType SEC_NSECS
Nanoseconds in one second.
Definition: Time.h:264
Gaudi::TimeSpan::lastMinutes
int lastMinutes() const
Get the number of complete minutes in the last incomplete hour of the span.
Definition: Time.icpp:122
Gaudi::TimeSpan::hours
int hours() const
Get the number of complete hours in the span.
Definition: Time.icpp:105
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:176
Gaudi::Time::TimeAssert
void TimeAssert(bool cond, std::string_view msg="time assertion failed") const
Definition: Time.h:333
Gaudi::Time::SECS_PER_HOUR
static const int SECS_PER_HOUR
Seconds in one hour hour.
Definition: Time.h:261
backwards_compatibility_hack_time_timespan
Definition: Time.icpp:216
Gaudi::TimeSpan::lastHours
int lastHours() const
Get the number of complete hours in the last incomplete day of the span.
Definition: Time.icpp:118
Gaudi::Functional::details::out
OptOut && out
Definition: details.h:179
Gaudi::Time::operator-=
Time & operator-=(const TimeSpan &x)
Subtract the specified amount from the time.
Definition: Time.icpp:49