The Gaudi Framework  v30r3 (a5ef0a68)
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 {
8 
11  inline Time::Time( ValueType nsecs ) : m_nsecs( nsecs )
12  {
13  TimeAssert( m_nsecs >= 0, "cannot create a negative time" );
14  }
15 
18  inline Time::Time( TimeSpan ts ) : m_nsecs( ts.m_nsecs )
19  {
20  TimeAssert( m_nsecs >= 0, "cannot create a negative time" );
21  }
22 
25  inline Time::Time( ValueType secs, int nsecs ) : m_nsecs( secs * Time::SEC_NSECS + nsecs )
26  {
27  TimeAssert( m_nsecs >= 0, "cannot create a negative time" );
28  }
29 
32  inline Time::ValueType Time::ns() const { return m_nsecs; }
33 
36  inline Time& Time::operator+=( const TimeSpan& x )
37  {
38  TimeAssert( m_nsecs >= -x.m_nsecs, "time operation lead to negative time" );
39  m_nsecs += x.m_nsecs;
40  return *this;
41  }
42 
45  inline Time& Time::operator-=( const TimeSpan& x )
46  {
47  TimeAssert( m_nsecs >= x.m_nsecs, "time operation lead to negative time" );
48  m_nsecs -= x.m_nsecs;
49  return *this;
50  }
51 
53  inline Time Time::epoch() { return 0LL; }
54 
56  inline Time Time::max() { return 0x7fffffffffffffffLL; }
57 
59  inline bool Time::isLeap( int year )
60  {
61  return ( ( year % 4 ) == 0 && ( ( year % 100 ) != 0 || ( year % 400 ) == 0 ) );
62  }
63 
67 
69  inline TimeSpan::TimeSpan( Time t ) : m_nsecs( t.m_nsecs ) {}
70 
72  inline TimeSpan::TimeSpan( ValueType nsecs ) : m_nsecs( nsecs ) {}
73 
82  inline TimeSpan::TimeSpan( ValueType secs, int nsecs ) : m_nsecs( secs * Time::SEC_NSECS + nsecs ) {}
83 
95  inline TimeSpan::TimeSpan( int days, int hours, int mins, int secs, int nsecs )
96  {
97  m_nsecs = ( secs + 60 * ( mins + 60 * ( hours + 24 * days ) ) ) * Time::SEC_NSECS + nsecs;
98  }
99 
101  inline int TimeSpan::days() const { return int( m_nsecs / Time::SEC_NSECS / Time::SECS_PER_DAY ); }
102 
104  inline int TimeSpan::hours() const { return int( m_nsecs / Time::SEC_NSECS / Time::SECS_PER_HOUR ); }
105 
107  inline int TimeSpan::minutes() const { return int( m_nsecs / Time::SEC_NSECS / 60 ); }
108 
111 
113  inline TimeSpan::ValueType TimeSpan::ns() const { return m_nsecs; }
114 
117  inline int TimeSpan::lastHours() const { return hours() - days() * 24; }
118 
121  inline int TimeSpan::lastMinutes() const { return minutes() - hours() * 60; }
122 
125  inline int TimeSpan::lastSeconds() const { return int( seconds() - ( (ValueType)minutes() * (ValueType)60 ) ); }
126 
129  inline int TimeSpan::lastNSeconds() const { return int( m_nsecs % Time::SEC_NSECS ); }
130 
133  {
134  m_nsecs += x.m_nsecs;
135  return *this;
136  }
137 
140  {
141  m_nsecs -= x.m_nsecs;
142  return *this;
143  }
144 
147  {
148  m_nsecs *= x.m_nsecs;
149  return *this;
150  }
151 
154  {
155  m_nsecs /= x.m_nsecs;
156  return *this;
157  }
158 
161  {
162  m_nsecs %= x.m_nsecs;
163  return *this;
164  }
165 
168  {
169  return out << Gaudi::TimeSpan( time ).seconds() << '.' << time.nanoformat();
170  }
171 
174  {
175  return out << time.seconds() << '.' << Gaudi::Time( time ).nanoformat();
176  }
177 }
178 
179 //<<<<<< INLINE PUBLIC FUNCTIONS >>>>>>
181 {
182  return Gaudi::Time( t.ns() + ts.ns() );
183 }
184 
186 {
187  return Gaudi::Time( t.ns() + ts.ns() );
188 }
189 
190 inline Gaudi::TimeSpan operator-( const Gaudi::Time& t1, const Gaudi::Time& t2 )
191 {
192  return Gaudi::TimeSpan( t1.ns() - t2.ns() );
193 }
194 
196 {
197  return Gaudi::Time( t.ns() - ts.ns() );
198 }
199 
200 inline bool operator!( const Gaudi::Time& t ) { return !t.ns(); }
201 
205 
206 inline Gaudi::TimeSpan operator+( const Gaudi::TimeSpan& ts ) { return ts; }
207 
208 inline Gaudi::TimeSpan operator-( const Gaudi::TimeSpan& ts ) { return Gaudi::TimeSpan( -ts.ns() ); }
209 
210 inline bool operator!( const Gaudi::TimeSpan& ts ) { return !ts.ns(); }
211 
212 // --- operators for serialization ---
213 
214 // Output serialization
215 inline StreamBuffer& operator<<( StreamBuffer& s, const Gaudi::Time& t ) { return s << t.ns(); }
216 // Input serialization
218 {
220  s >> tmp;
221  t = Gaudi::Time( tmp );
222  return s;
223 }
224 
225 // make sure that "namespace Gaudi { using ::operator<; }" continues to compile...
226 // to be removed once all instances of the above have been removed from user code...
228 {
230 };
232 {
233  return false;
234 }
235 
236 #endif
longlong ValueType
Definition: Time.h:242
Time()=default
Initialize an empty (zero) time value.
static const int SECS_PER_HOUR
Seconds in one hour hour.
Definition: Time.h:264
static Time max()
Returns the maximum time.
Definition: Time.icpp:56
bool operator!(const Gaudi::Time &t)
Definition: Time.icpp:200
TimeSpan & operator/=(const TimeSpan &n)
Divide a time span.
Definition: Time.icpp:153
The stream buffer is a small object collecting object data.
Definition: StreamBuffer.h:41
ValueType m_nsecs
Definition: Time.h:111
void TimeAssert(bool cond, const std::string &msg="time assertion failed") const
Definition: Time.h:337
TimeSpan & operator*=(const TimeSpan &n)
Multiply a time span.
Definition: Time.icpp:146
friend Gaudi::TimeSpan operator+(const Gaudi::TimeSpan &ts1, const Gaudi::TimeSpan &ts2)
Definition: Time.h:100
TimeSpan()=default
Initialize an empty (zero) time difference.
TimeSpan & operator+=(const TimeSpan &x)
Add to a time span.
Definition: Time.icpp:132
int lastNSeconds() const
Get the number of nanoseconds in the last incomplete second of the span.
Definition: Time.icpp:129
static const ValueType SEC_NSECS
Nanoseconds in one second.
Definition: Time.h:267
ValueType ns() const
Return the time span as nanoseconds.
Definition: Time.icpp:113
Based on seal::Time.
Definition: Time.h:237
int days() const
Get the number of complete days in the span.
Definition: Time.icpp:101
TimeSpan & operator-=(const TimeSpan &x)
Subtract from a time span.
Definition: Time.icpp:139
int lastMinutes() const
Get the number of complete minutes in the last incomplete hour of the span.
Definition: Time.icpp:121
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:303
TimeSpan & operator%=(const TimeSpan &n)
Compute a modulo of a time span.
Definition: Time.icpp:160
Time & operator+=(const TimeSpan &x)
Add the specified amount to the time.
Definition: Time.icpp:36
int hours() const
Get the number of complete hours in the span.
Definition: Time.icpp:104
static bool isLeap(int year)
Check if the year is a leap-year.
Definition: Time.icpp:59
ValueType seconds() const
Get the number of complete seconds in the span.
Definition: Time.icpp:110
ValueType m_nsecs
Definition: Time.h:335
string s
Definition: gaudirun.py:253
static const int SECS_PER_DAY
Seconds in 24 hours.
Definition: Time.h:261
static Time epoch()
Returns the minimum time.
Definition: Time.icpp:53
ValueType ns() const
Return the time as nanoseconds since 00:00:00 on January 1, 1970 in UTC.
Definition: Time.icpp:32
StreamBuffer & operator>>(StreamBuffer &s, Gaudi::Time &t)
Definition: Time.icpp:217
friend Gaudi::TimeSpan operator-(const Gaudi::TimeSpan &ts1, const Gaudi::TimeSpan &ts2)
Definition: Time.h:105
Time & operator-=(const TimeSpan &x)
Subtract the specified amount from the time.
Definition: Time.icpp:45
int year(bool local) const
Get the year.
Definition: Time.cpp:172
friend bool operator<(const Gaudi::TimeSpan &t1, const Gaudi::TimeSpan &t2)
Definition: Time.h:92
Based on seal::TimeSpan.
Definition: Time.h:55
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:117
int lastSeconds() const
Get the number of complete seconds in the last incomplete minute of the span.
Definition: Time.icpp:125
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:243
int minutes() const
Get the number of complete minutes in the span.
Definition: Time.icpp:107