Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v31r0 (aeb156f0)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Time.icpp
Go to the documentation of this file.
1 #ifndef GAUDIKERNEL_TIME_ICPP
2 #define GAUDIKERNEL_TIME_ICPP 1
3 
4 // Implementation of inline function for classes Gaudi::Time and Gaudi::TimeSpan
5 
6 namespace Gaudi {
7 
10  inline Time::Time( ValueType nsecs ) : m_nsecs( nsecs ) {
11  TimeAssert( m_nsecs >= 0, "cannot create a negative time" );
12  }
13 
16  inline Time::Time( TimeSpan ts ) : m_nsecs( ts.m_nsecs ) {
17  TimeAssert( m_nsecs >= 0, "cannot create a negative time" );
18  }
19 
22  inline Time::Time( ValueType secs, int nsecs ) : m_nsecs( secs * Time::SEC_NSECS + nsecs ) {
23  TimeAssert( m_nsecs >= 0, "cannot create a negative time" );
24  }
25 
28  inline Time::ValueType Time::ns() const { return m_nsecs; }
29 
32  inline Time& Time::operator+=( const TimeSpan& x ) {
33  TimeAssert( m_nsecs >= -x.m_nsecs, "time operation lead to negative time" );
34  m_nsecs += x.m_nsecs;
35  return *this;
36  }
37 
40  inline Time& Time::operator-=( const TimeSpan& x ) {
41  TimeAssert( m_nsecs >= x.m_nsecs, "time operation lead to negative time" );
42  m_nsecs -= x.m_nsecs;
43  return *this;
44  }
45 
47  inline Time Time::epoch() { return 0LL; }
48 
50  inline Time Time::max() { return 0x7fffffffffffffffLL; }
51 
53  inline bool Time::isLeap( int year ) {
54  return ( ( year % 4 ) == 0 && ( ( year % 100 ) != 0 || ( year % 400 ) == 0 ) );
55  }
56 
60 
62  inline TimeSpan::TimeSpan( Time t ) : m_nsecs( t.m_nsecs ) {}
63 
65  inline TimeSpan::TimeSpan( ValueType nsecs ) : m_nsecs( nsecs ) {}
66 
75  inline TimeSpan::TimeSpan( ValueType secs, int nsecs ) : m_nsecs( secs * Time::SEC_NSECS + nsecs ) {}
76 
88  inline TimeSpan::TimeSpan( int days, int hours, int mins, int secs, int nsecs ) {
89  m_nsecs = ( secs + 60 * ( mins + 60 * ( hours + 24 * days ) ) ) * Time::SEC_NSECS + nsecs;
90  }
91 
93  inline int TimeSpan::days() const { return int( m_nsecs / Time::SEC_NSECS / Time::SECS_PER_DAY ); }
94 
96  inline int TimeSpan::hours() const { return int( m_nsecs / Time::SEC_NSECS / Time::SECS_PER_HOUR ); }
97 
99  inline int TimeSpan::minutes() const { return int( m_nsecs / Time::SEC_NSECS / 60 ); }
100 
103 
105  inline TimeSpan::ValueType TimeSpan::ns() const { return m_nsecs; }
106 
109  inline int TimeSpan::lastHours() const { return hours() - days() * 24; }
110 
113  inline int TimeSpan::lastMinutes() const { return minutes() - hours() * 60; }
114 
117  inline int TimeSpan::lastSeconds() const { return int( seconds() - ( (ValueType)minutes() * (ValueType)60 ) ); }
118 
121  inline int TimeSpan::lastNSeconds() const { return int( m_nsecs % Time::SEC_NSECS ); }
122 
124  inline TimeSpan& TimeSpan::operator+=( const TimeSpan& x ) {
125  m_nsecs += x.m_nsecs;
126  return *this;
127  }
128 
130  inline TimeSpan& TimeSpan::operator-=( const TimeSpan& x ) {
131  m_nsecs -= x.m_nsecs;
132  return *this;
133  }
134 
136  inline TimeSpan& TimeSpan::operator*=( const TimeSpan& x ) {
137  m_nsecs *= x.m_nsecs;
138  return *this;
139  }
140 
142  inline TimeSpan& TimeSpan::operator/=( const TimeSpan& x ) {
143  m_nsecs /= x.m_nsecs;
144  return *this;
145  }
146 
148  inline TimeSpan& TimeSpan::operator%=( const TimeSpan& x ) {
149  m_nsecs %= x.m_nsecs;
150  return *this;
151  }
152 
155  return out << Gaudi::TimeSpan( time ).seconds() << '.' << time.nanoformat();
156  }
157 
160  return out << time.seconds() << '.' << Gaudi::Time( time ).nanoformat();
161  }
162 } // namespace Gaudi
163 
164 //<<<<<< INLINE PUBLIC FUNCTIONS >>>>>>
165 inline Gaudi::Time operator+( const Gaudi::Time& t, const Gaudi::TimeSpan& ts ) {
166  return Gaudi::Time( t.ns() + ts.ns() );
167 }
168 
169 inline Gaudi::Time operator+( const Gaudi::TimeSpan& ts, const Gaudi::Time& t ) {
170  return Gaudi::Time( t.ns() + ts.ns() );
171 }
172 
173 inline Gaudi::TimeSpan operator-( const Gaudi::Time& t1, const Gaudi::Time& t2 ) {
174  return Gaudi::TimeSpan( t1.ns() - t2.ns() );
175 }
176 
177 inline Gaudi::Time operator-( const Gaudi::Time& t, const Gaudi::TimeSpan& ts ) {
178  return Gaudi::Time( t.ns() - ts.ns() );
179 }
180 
181 inline bool operator!( const Gaudi::Time& t ) { return !t.ns(); }
182 
186 
187 inline Gaudi::TimeSpan operator+( const Gaudi::TimeSpan& ts ) { return ts; }
188 
189 inline Gaudi::TimeSpan operator-( const Gaudi::TimeSpan& ts ) { return Gaudi::TimeSpan( -ts.ns() ); }
190 
191 inline bool operator!( const Gaudi::TimeSpan& ts ) { return !ts.ns(); }
192 
193 // --- operators for serialization ---
194 
195 // Output serialization
196 inline StreamBuffer& operator<<( StreamBuffer& s, const Gaudi::Time& t ) { return s << t.ns(); }
197 // Input serialization
200  s >> tmp;
201  t = Gaudi::Time( tmp );
202  return s;
203 }
204 
205 // make sure that "namespace Gaudi { using ::operator<; }" continues to compile...
206 // to be removed once all instances of the above have been removed from user code...
209 };
211  return false;
212 }
213 
214 #endif
Time()=default
Initialize an empty (zero) time value.
static const int SECS_PER_HOUR
Seconds in one hour hour.
Definition: Time.h:257
static Time max()
Returns the maximum time.
Definition: Time.icpp:50
bool operator!(const Gaudi::Time &t)
Definition: Time.icpp:181
TimeSpan & operator/=(const TimeSpan &n)
Divide a time span.
Definition: Time.icpp:142
The stream buffer is a small object collecting object data.
Definition: StreamBuffer.h:41
ValueType m_nsecs
Definition: Time.h:105
void TimeAssert(bool cond, const std::string &msg="time assertion failed") const
Definition: Time.h:330
TimeSpan & operator*=(const TimeSpan &n)
Multiply a time span.
Definition: Time.icpp:136
friend Gaudi::TimeSpan operator+(const Gaudi::TimeSpan &ts1, const Gaudi::TimeSpan &ts2)
Definition: Time.h:96
TimeSpan()=default
Initialize an empty (zero) time difference.
TimeSpan & operator+=(const TimeSpan &x)
Add to a time span.
Definition: Time.icpp:124
int lastNSeconds() const
Get the number of nanoseconds in the last incomplete second of the span.
Definition: Time.icpp:121
static const ValueType SEC_NSECS
Nanoseconds in one second.
Definition: Time.h:260
ValueType ns() const
Return the time span as nanoseconds.
Definition: Time.icpp:105
Based on seal::Time.
Definition: Time.h:231
int days() const
Get the number of complete days in the span.
Definition: Time.icpp:93
TimeSpan & operator-=(const TimeSpan &x)
Subtract from a time span.
Definition: Time.icpp:130
int lastMinutes() const
Get the number of complete minutes in the last incomplete hour of the span.
Definition: Time.icpp:113
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:292
TimeSpan & operator%=(const TimeSpan &n)
Compute a modulo of a time span.
Definition: Time.icpp:148
Time & operator+=(const TimeSpan &x)
Add the specified amount to the time.
Definition: Time.icpp:32
int hours() const
Get the number of complete hours in the span.
Definition: Time.icpp:96
static bool isLeap(int year)
Check if the year is a leap-year.
Definition: Time.icpp:53
ValueType seconds() const
Get the number of complete seconds in the span.
Definition: Time.icpp:102
ValueType m_nsecs
Definition: Time.h:328
string s
Definition: gaudirun.py:312
static const int SECS_PER_DAY
Seconds in 24 hours.
Definition: Time.h:254
static Time epoch()
Returns the minimum time.
Definition: Time.icpp:47
ValueType ns() const
Return the time as nanoseconds since 00:00:00 on January 1, 1970 in UTC.
Definition: Time.icpp:28
StreamBuffer & operator>>(StreamBuffer &s, Gaudi::Time &t)
Definition: Time.icpp:198
friend Gaudi::TimeSpan operator-(const Gaudi::TimeSpan &ts1, const Gaudi::TimeSpan &ts2)
Definition: Time.h:100
Time & operator-=(const TimeSpan &x)
Subtract the specified amount from the time.
Definition: Time.icpp:40
int year(bool local) const
Get the year.
Definition: Time.cpp:166
friend bool operator<(const Gaudi::TimeSpan &t1, const Gaudi::TimeSpan &t2)
Definition: Time.h:88
Based on seal::TimeSpan.
Definition: Time.h:52
STL class.
Helper functions to set/get the application return code.
Definition: __init__.py:1
int lastHours() const
Get the number of complete hours in the last incomplete day of the span.
Definition: Time.icpp:109
int lastSeconds() const
Get the number of complete seconds in the last incomplete minute of the span.
Definition: Time.icpp:117
long long ValueType
Definition: Time.h:235
long long ValueType
Definition: Time.h:56
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:235
int minutes() const
Get the number of complete minutes in the span.
Definition: Time.icpp:99