Source

mana-core-gaudikernel / GaudiKernel / Time.icpp

// $Id: Time.icpp,v 1.1 2006/01/25 16:02:48 hmd Exp $
#ifndef GAUDIKERNEL_TIME_ICPP 
#define GAUDIKERNEL_TIME_ICPP 1

// Implementation of inline function for classes Gaudi::Time and Gaudi::TimeSpan

namespace Gaudi {
  
  /** Initialize an empty (zero) time value.  */
  inline Time::Time (void): m_nsecs(0) {}

  /** Initialize time to @a nsecs nanoseconds since 00:00:00 on January
      1, 1970 in UTC.  */
  inline Time::Time (ValueType nsecs): m_nsecs(nsecs) {
    TimeAssert( m_nsecs >= 0, "cannot create a negative time");
  }

  /** Initialize time to @a ts nanoseconds since 00:00:00 on January 1,
      1970 in UTC.  */
  inline Time::Time (TimeSpan ts) : m_nsecs(ts.m_nsecs) {
    TimeAssert( m_nsecs >= 0, "cannot create a negative time");
  }

  /** Initialize time to @a secs (seconds) and @a nsecs (nanoseconds)
      summed since 00:00:00 on January 1, 1970 in UTC.  */
  inline Time::Time (ValueType secs, int nsecs)
    : m_nsecs(secs * Time::SEC_NSECS + nsecs) {
    TimeAssert( m_nsecs >= 0, "cannot create a negative time");
  }

  /** Return the time as nanoseconds since 00:00:00 on January 1, 1970
      in UTC.  */
  inline Time::ValueType Time::ns (void) const {
    return m_nsecs;
  }

  /** Add the specified amount to the time.  Note that #Time is always
      expressed in UTC.  */
  inline Time &Time::operator+= (const TimeSpan &x) {
    TimeAssert( m_nsecs >= -x.m_nsecs, "time operation lead to negative time");
    m_nsecs += x.m_nsecs;
    return *this;
  }

  /** Subtract the specified amount from the time.  Note that #Time is
      always expressed in UTC.  */
  inline Time &Time::operator-= (const TimeSpan &x) {
    TimeAssert( m_nsecs >= x.m_nsecs, "time operation lead to negative time");
    m_nsecs -= x.m_nsecs;
    return *this;
  }

  /** Return the time for the epoch (= zero time).  */
  inline Time Time::epoch (void) {
    return 0LL;
  }

  /** Return the maximum time.  */
  inline Time Time::max (void) {
    return 0x7fffffffffffffffLL;
  }

  /** Check if the @a year is a leap-year.  */
  inline bool Time::isLeap (int year) {
    return ((year % 4) == 0
            && ((year % 100) != 0
                || (year % 400) == 0));
  }

  //////////////////////////////////////////////////////////////////////
  //////////////////////////////////////////////////////////////////////
  //////////////////////////////////////////////////////////////////////
  /** Initialize an empty (zero) time difference.  */
  inline TimeSpan::TimeSpan (void): m_nsecs(0) {}

  /** Initialize a time span from #Time @a t. */
  inline TimeSpan::TimeSpan (Time t): m_nsecs (t.m_nsecs) {}

  /** Initialize a time span to a specific length. */
  inline TimeSpan::TimeSpan (ValueType nsecs): m_nsecs (nsecs) {}

  /** Initialise a time span to a specific length.  The value is
      initialised to the sum of the parts---the parts do not need to
      fall into their "natural" ranges.  The values are normalised to
      the natural meanings (e.g. 1000 seconds - 500 nanoseconds), so be
      careful with signs if you are producing values from other sources.

      @param secs  Seconds.
      @param nsecs Nanoseconds.  */
  inline TimeSpan::TimeSpan (ValueType secs, int nsecs)
    : m_nsecs(secs * Time::SEC_NSECS + nsecs) {}

  /** Initialise a time span to a specific length.  The value is
      initialised to the sum of the parts---the parts do not need to
      fall into their "natural" ranges.  The values are normalised to
      the natural meanings (e.g. 1000 seconds - 500 nanoseconds), so be
      careful with signs if you are producing values from other sources.

      @param days  Whole days.
      @param hours Whole hours.
      @param mins  Whole minutes.
      @param secs  Whole seconds.
      @param nsecs Nanoseconds.  */
  inline TimeSpan::TimeSpan (int days, int hours, int mins, int secs, int nsecs) {
    m_nsecs = (secs + 60 * (mins + 60 * (hours + 24*days)))*Time::SEC_NSECS + nsecs;
  }

  /** Get the number of complete days in the span.  */
  inline int TimeSpan::days (void) const {
    return int(m_nsecs / Time::SEC_NSECS / Time::SECS_PER_DAY);
  }

  /** Get the number of complete hours in the span.  */
  inline int TimeSpan::hours (void) const {
    return int(m_nsecs / Time::SEC_NSECS / Time::SECS_PER_HOUR);
  }

  /** Get the number of complete minutes in the span.  */
  inline int TimeSpan::minutes (void) const {
    return int(m_nsecs / Time::SEC_NSECS / 60);
  }

  /** Get the number of complete seconds in the span.  */
  inline TimeSpan::ValueType TimeSpan::seconds (void) const {
    return m_nsecs / Time::SEC_NSECS;
  }

  /** Return the time span as nanoseconds.  */
  inline TimeSpan::ValueType TimeSpan::ns (void) const {
    return m_nsecs;
  }

  /** Get the number of complete hours in the last incomplete day of the
      span.  */
  inline int TimeSpan::lastHours (void) const {
    return hours () - days () * 24;
  }

  /** Get the number of complete minutes in the last incomplete hour of
      the span.  */
  inline int TimeSpan::lastMinutes (void) const {
    return minutes () - hours () * 60;
  }

  /** Get the number of complete seconds in the last incomplete minute
      of the span. */
  inline int TimeSpan::lastSeconds (void) const {
    return int(seconds() - ( (ValueType)minutes() * (ValueType)60 ));
  }

  /** Get the number of nanoseconds in the last incomplete second
      of the span. */
  inline int TimeSpan::lastNSeconds (void) const {
    return int(m_nsecs % Time::SEC_NSECS);
  }

  /** Add to a time span.  */
  inline TimeSpan &TimeSpan::operator+= (const TimeSpan &x) {
    m_nsecs += x.m_nsecs;
    return *this;
  }

  /** Subtract from a time span.  */
  inline TimeSpan & TimeSpan::operator-= (const TimeSpan &x) {
    m_nsecs -= x.m_nsecs;
    return *this;
  }

  /** Multiply a time span.  */
  inline TimeSpan & TimeSpan::operator*= (const TimeSpan &x) {
    m_nsecs *= x.m_nsecs;
    return *this;
  }

  /** Divide a time span.  */
  inline TimeSpan & TimeSpan::operator/= (const TimeSpan &x) {
    m_nsecs /= x.m_nsecs; return *this;
  }

  /** Compute a modulo of a time span.  */
  inline TimeSpan & TimeSpan::operator%= (const TimeSpan &x) {
    m_nsecs %= x.m_nsecs; return *this;
  }
}

//<<<<<< INLINE PUBLIC FUNCTIONS                                        >>>>>>

inline std::ostream& operator<< (std::ostream &out, const Gaudi::Time &time) {
  return out << Gaudi::TimeSpan(time).seconds() << '.' << time.nanoformat();
}

inline std::ostream& operator<< (std::ostream &out, const Gaudi::TimeSpan &time) {
  return out << time.seconds() << '.' << Gaudi::Time(time).nanoformat();
}

inline Gaudi::Time operator+ (const Gaudi::Time &t, const Gaudi::TimeSpan &ts) {
  return Gaudi::Time (t.ns() + ts.ns());
}

inline Gaudi::Time operator+ (const Gaudi::TimeSpan &ts, const Gaudi::Time &t) {
  return Gaudi::Time (t.ns() + ts.ns());
}

inline Gaudi::TimeSpan operator- (const Gaudi::Time &t1, const Gaudi::Time &t2) {
  return Gaudi::TimeSpan (t1.ns() - t2.ns());
}

inline Gaudi::Time operator- (const Gaudi::Time &t, const Gaudi::TimeSpan &ts) {
  return Gaudi::Time (t.ns() - ts.ns());
}

//inline Gaudi::TimeSpan operator* (const Gaudi::Time &t, const Gaudi::TimeSpan &ts) {
//  return Gaudi::TimeSpan (t.ns() * ts.ns());
//}

//inline Gaudi::TimeSpan operator/ (const Gaudi::Time &t, const Gaudi::TimeSpan &ts) {
//  return Gaudi::TimeSpan (t.ns() / ts.ns());
//}

//inline Gaudi::TimeSpan operator% (const Gaudi::Time &t, const Gaudi::TimeSpan &ts) {
//  return Gaudi::TimeSpan (t.ns() % ts.ns());
//}

inline bool operator== (const Gaudi::Time &t1, const Gaudi::Time &t2) {
  return t1.ns() == t2.ns();
}
 
inline bool operator!= (const Gaudi::Time &t1, const Gaudi::Time &t2) {
  return t1.ns() != t2.ns();
}

inline bool operator< (const Gaudi::Time &t1, const Gaudi::Time &t2) {
  return t1.ns() < t2.ns();
}

inline bool operator<= (const Gaudi::Time &t1, const Gaudi::Time &t2) {
  return t1.ns() <= t2.ns();
}

inline bool operator> (const Gaudi::Time &t1, const Gaudi::Time &t2) {
  return t1.ns() > t2.ns();
}

inline bool operator>= (const Gaudi::Time &t1, const Gaudi::Time &t2) {
  return t1.ns() >= t2.ns();
}

inline bool operator! (const Gaudi::Time &t) {
  return ! t.ns();
}

//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////

inline Gaudi::TimeSpan operator+ (const Gaudi::TimeSpan &ts) {
  return ts;
}

inline Gaudi::TimeSpan operator- (const Gaudi::TimeSpan &ts) {
  return Gaudi::TimeSpan (-ts.ns());
}

inline Gaudi::TimeSpan operator+ (const Gaudi::TimeSpan &ts1, const Gaudi::TimeSpan &ts2) {
  return Gaudi::TimeSpan (ts1.ns() + ts2.ns());
}

inline Gaudi::TimeSpan operator- (const Gaudi::TimeSpan &ts1, const Gaudi::TimeSpan &ts2) {
  return Gaudi::TimeSpan (ts1.ns() - ts2.ns());
}

//inline Gaudi::TimeSpan operator* (const Gaudi::TimeSpan &ts1, const Gaudi::TimeSpan &ts2) {
//  return Gaudi::TimeSpan (ts1.ns() * ts2.ns());
//}

//inline Gaudi::TimeSpan operator/ (const Gaudi::TimeSpan &ts1, const Gaudi::TimeSpan &ts2) {
//  return Gaudi::TimeSpan (ts1.ns() / ts2.ns());
//}

//inline Gaudi::TimeSpan operator% (const Gaudi::TimeSpan &ts1, const Gaudi::TimeSpan &ts2) {
//  return Gaudi::TimeSpan (ts1.ns() % ts2.ns());
//}

inline bool operator== (const Gaudi::TimeSpan &t1, const Gaudi::TimeSpan &t2) {
  return t1.ns() == t2.ns();
}

inline bool operator!= (const Gaudi::TimeSpan &t1, const Gaudi::TimeSpan &t2) {
  return t1.ns() != t2.ns();
}

inline bool operator< (const Gaudi::TimeSpan &t1, const Gaudi::TimeSpan &t2) {
  return t1.ns() < t2.ns();
}

inline bool operator<= (const Gaudi::TimeSpan &t1, const Gaudi::TimeSpan &t2) {
  return t1.ns() <= t2.ns();
}

inline bool operator> (const Gaudi::TimeSpan &t1, const Gaudi::TimeSpan &t2) {
  return t1.ns() > t2.ns();
}

inline bool operator>= (const Gaudi::TimeSpan &t1, const Gaudi::TimeSpan &t2) {
  return t1.ns() >= t2.ns();
}

inline bool operator! (const Gaudi::TimeSpan &ts) {
  return ! ts.ns();
}

// --- operators for serialization ---
  
// Output serialization
inline StreamBuffer& operator<<(StreamBuffer &s, const Gaudi::Time &t)  {
  return s << t.ns();
}
// Input serialization
inline StreamBuffer& operator>>(StreamBuffer &s, Gaudi::Time &t)  {
  Gaudi::Time::ValueType tmp;
  s >> tmp;
  t = Gaudi::Time(tmp);
  return s;
}

#endif
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.