Warning, /include/GaudiKernel/Time.icpp is written in an unsupported language. File is not indexed.
0001 /***********************************************************************************\
0002 * (c) Copyright 1998-2019 CERN for the benefit of the LHCb and ATLAS collaborations *
0003 * *
0004 * This software is distributed under the terms of the Apache version 2 licence, *
0005 * copied verbatim in the file "LICENSE". *
0006 * *
0007 * In applying this licence, CERN does not waive the privileges and immunities *
0008 * granted to it by virtue of its status as an Intergovernmental Organization *
0009 * or submit itself to any jurisdiction. *
0010 \***********************************************************************************/
0011 #ifndef GAUDIKERNEL_TIME_ICPP
0012 #define GAUDIKERNEL_TIME_ICPP 1
0013
0014 // Implementation of inline function for classes Gaudi::Time and Gaudi::TimeSpan
0015
0016 namespace Gaudi {
0017
0018 /** Initialize time to @a nsecs nanoseconds since 00:00:00 on January
0019 1, 1970 in UTC. */
0020 inline Time::Time( ValueType nsecs ) : m_nsecs( nsecs ) {
0021 TimeAssert( m_nsecs >= 0, "cannot create a negative time" );
0022 }
0023
0024 /** Initialize time to @a ts nanoseconds since 00:00:00 on January 1,
0025 1970 in UTC. */
0026 inline Time::Time( TimeSpan ts ) : m_nsecs( ts.m_nsecs ) {
0027 TimeAssert( m_nsecs >= 0, "cannot create a negative time" );
0028 }
0029
0030 /** Initialize time to @a secs (seconds) and @a nsecs (nanoseconds)
0031 summed since 00:00:00 on January 1, 1970 in UTC. */
0032 inline Time::Time( ValueType secs, int nsecs ) : m_nsecs( secs * Time::SEC_NSECS + nsecs ) {
0033 TimeAssert( m_nsecs >= 0, "cannot create a negative time" );
0034 }
0035
0036 /** Return the time as nanoseconds since 00:00:00 on January 1, 1970
0037 in UTC. */
0038 inline Time::ValueType Time::ns() const { return m_nsecs; }
0039
0040 /** Add the specified amount to the time. Note that #Time is always
0041 expressed in UTC. */
0042 inline Time& Time::operator+=( const TimeSpan& x ) {
0043 TimeAssert( m_nsecs >= -x.m_nsecs, "time operation lead to negative time" );
0044 m_nsecs += x.m_nsecs;
0045 return *this;
0046 }
0047
0048 /** Subtract the specified amount from the time. Note that #Time is
0049 always expressed in UTC. */
0050 inline Time& Time::operator-=( const TimeSpan& x ) {
0051 TimeAssert( m_nsecs >= x.m_nsecs, "time operation lead to negative time" );
0052 m_nsecs -= x.m_nsecs;
0053 return *this;
0054 }
0055
0056 /** Return the time for the epoch (= zero time). */
0057 inline Time Time::epoch() { return 0LL; }
0058
0059 /** Return the maximum time. */
0060 inline Time Time::max() { return 0x7fffffffffffffffLL; }
0061
0062 /** Check if the @a year is a leap-year. */
0063 inline bool Time::isLeap( int year ) {
0064 return ( ( year % 4 ) == 0 && ( ( year % 100 ) != 0 || ( year % 400 ) == 0 ) );
0065 }
0066
0067 //////////////////////////////////////////////////////////////////////
0068 //////////////////////////////////////////////////////////////////////
0069 //////////////////////////////////////////////////////////////////////
0070
0071 /** Initialize a time span from #Time @a t. */
0072 inline TimeSpan::TimeSpan( Time t ) : m_nsecs( t.m_nsecs ) {}
0073
0074 /** Initialize a time span to a specific length. */
0075 inline TimeSpan::TimeSpan( ValueType nsecs ) : m_nsecs( nsecs ) {}
0076
0077 /** Initialise a time span to a specific length. The value is
0078 initialised to the sum of the parts---the parts do not need to
0079 fall into their "natural" ranges. The values are normalised to
0080 the natural meanings (e.g. 1000 seconds - 500 nanoseconds), so be
0081 careful with signs if you are producing values from other sources.
0082
0083 @param secs Seconds.
0084 @param nsecs Nanoseconds. */
0085 inline TimeSpan::TimeSpan( ValueType secs, int nsecs ) : m_nsecs( secs * Time::SEC_NSECS + nsecs ) {}
0086
0087 /** Initialise a time span to a specific length. The value is
0088 initialised to the sum of the parts---the parts do not need to
0089 fall into their "natural" ranges. The values are normalised to
0090 the natural meanings (e.g. 1000 seconds - 500 nanoseconds), so be
0091 careful with signs if you are producing values from other sources.
0092
0093 @param days Whole days.
0094 @param hours Whole hours.
0095 @param mins Whole minutes.
0096 @param secs Whole seconds.
0097 @param nsecs Nanoseconds. */
0098 inline TimeSpan::TimeSpan( int days, int hours, int mins, int secs, int nsecs ) {
0099 m_nsecs = ( secs + 60 * ( mins + 60 * ( hours + 24 * days ) ) ) * Time::SEC_NSECS + nsecs;
0100 }
0101
0102 /** Get the number of complete days in the span. */
0103 inline int TimeSpan::days() const { return int( m_nsecs / Time::SEC_NSECS / Time::SECS_PER_DAY ); }
0104
0105 /** Get the number of complete hours in the span. */
0106 inline int TimeSpan::hours() const { return int( m_nsecs / Time::SEC_NSECS / Time::SECS_PER_HOUR ); }
0107
0108 /** Get the number of complete minutes in the span. */
0109 inline int TimeSpan::minutes() const { return int( m_nsecs / Time::SEC_NSECS / 60 ); }
0110
0111 /** Get the number of complete seconds in the span. */
0112 inline TimeSpan::ValueType TimeSpan::seconds() const { return m_nsecs / Time::SEC_NSECS; }
0113
0114 /** Return the time span as nanoseconds. */
0115 inline TimeSpan::ValueType TimeSpan::ns() const { return m_nsecs; }
0116
0117 /** Get the number of complete hours in the last incomplete day of the
0118 span. */
0119 inline int TimeSpan::lastHours() const { return hours() - days() * 24; }
0120
0121 /** Get the number of complete minutes in the last incomplete hour of
0122 the span. */
0123 inline int TimeSpan::lastMinutes() const { return minutes() - hours() * 60; }
0124
0125 /** Get the number of complete seconds in the last incomplete minute
0126 of the span. */
0127 inline int TimeSpan::lastSeconds() const { return int( seconds() - ( (ValueType)minutes() * (ValueType)60 ) ); }
0128
0129 /** Get the number of nanoseconds in the last incomplete second
0130 of the span. */
0131 inline int TimeSpan::lastNSeconds() const { return int( m_nsecs % Time::SEC_NSECS ); }
0132
0133 /** Add to a time span. */
0134 inline TimeSpan& TimeSpan::operator+=( const TimeSpan& x ) {
0135 m_nsecs += x.m_nsecs;
0136 return *this;
0137 }
0138
0139 /** Subtract from a time span. */
0140 inline TimeSpan& TimeSpan::operator-=( const TimeSpan& x ) {
0141 m_nsecs -= x.m_nsecs;
0142 return *this;
0143 }
0144
0145 /** Multiply a time span. */
0146 inline TimeSpan& TimeSpan::operator*=( const TimeSpan& x ) {
0147 m_nsecs *= x.m_nsecs;
0148 return *this;
0149 }
0150
0151 /** Divide a time span. */
0152 inline TimeSpan& TimeSpan::operator/=( const TimeSpan& x ) {
0153 m_nsecs /= x.m_nsecs;
0154 return *this;
0155 }
0156
0157 /** Compute a modulo of a time span. */
0158 inline TimeSpan& TimeSpan::operator%=( const TimeSpan& x ) {
0159 m_nsecs %= x.m_nsecs;
0160 return *this;
0161 }
0162
0163 /// Output operator.
0164 inline std::ostream& operator<<( std::ostream& out, const Gaudi::Time& time ) {
0165 return out << Gaudi::TimeSpan( time ).seconds() << '.' << time.nanoformat();
0166 }
0167
0168 /// Output operator.
0169 inline std::ostream& operator<<( std::ostream& out, const Gaudi::TimeSpan& time ) {
0170 return out << time.seconds() << '.' << Gaudi::Time( time ).nanoformat();
0171 }
0172 } // namespace Gaudi
0173
0174 //<<<<<< INLINE PUBLIC FUNCTIONS >>>>>>
0175 inline Gaudi::Time operator+( const Gaudi::Time& t, const Gaudi::TimeSpan& ts ) {
0176 return Gaudi::Time( t.ns() + ts.ns() );
0177 }
0178
0179 inline Gaudi::Time operator+( const Gaudi::TimeSpan& ts, const Gaudi::Time& t ) {
0180 return Gaudi::Time( t.ns() + ts.ns() );
0181 }
0182
0183 inline Gaudi::TimeSpan operator-( const Gaudi::Time& t1, const Gaudi::Time& t2 ) {
0184 return Gaudi::TimeSpan( t1.ns() - t2.ns() );
0185 }
0186
0187 inline Gaudi::Time operator-( const Gaudi::Time& t, const Gaudi::TimeSpan& ts ) {
0188 return Gaudi::Time( t.ns() - ts.ns() );
0189 }
0190
0191 inline bool operator!( const Gaudi::Time& t ) { return !t.ns(); }
0192
0193 //////////////////////////////////////////////////////////////////////
0194 //////////////////////////////////////////////////////////////////////
0195 //////////////////////////////////////////////////////////////////////
0196
0197 inline Gaudi::TimeSpan operator+( const Gaudi::TimeSpan& ts ) { return ts; }
0198
0199 inline Gaudi::TimeSpan operator-( const Gaudi::TimeSpan& ts ) { return Gaudi::TimeSpan( -ts.ns() ); }
0200
0201 inline bool operator!( const Gaudi::TimeSpan& ts ) { return !ts.ns(); }
0202
0203 // --- operators for serialization ---
0204
0205 // Output serialization
0206 inline StreamBuffer& operator<<( StreamBuffer& s, const Gaudi::Time& t ) { return s << t.ns(); }
0207 // Input serialization
0208 inline StreamBuffer& operator>>( StreamBuffer& s, Gaudi::Time& t ) {
0209 Gaudi::Time::ValueType tmp;
0210 s >> tmp;
0211 t = Gaudi::Time( tmp );
0212 return s;
0213 }
0214
0215 // make sure that "namespace Gaudi { using ::operator<; }" continues to compile...
0216 // to be removed once all instances of the above have been removed from user code...
0217 class backwards_compatibility_hack_time_timespan {
0218 backwards_compatibility_hack_time_timespan() = delete;
0219 };
0220 inline bool operator<( backwards_compatibility_hack_time_timespan, backwards_compatibility_hack_time_timespan ) {
0221 return false;
0222 }
0223
0224 #endif