// Time.cxx #include "dataset_util/Time.h" #include #include #include #include using std::string; using std::ostream; using std::ostringstream; using std::setw; using std::setfill; //********************************************************************** // Static methods for Time. //********************************************************************** // Now. Time Time::now() { return Time(time(0)); } //********************************************************************** // Methods for Time. //********************************************************************** // Constructor from unix time. Time::Time(unsigned long time) : m_time(time) { } //********************************************************************** // Set time. Time& Time::set() { m_time = time(0); return *this; } //********************************************************************** // Add time interval. Time& Time::operator+=(TimeInterval dtim) { if ( is_valid() ) { m_time += dtim.seconds(); } return *this; } //********************************************************************** // Subtract time interval. Time& Time::operator-=(TimeInterval dtim) { if ( is_valid() ) { m_time -= dtim.seconds(); } return *this; } //********************************************************************** // Validity. bool Time::is_valid() const { return m_time != 0; } //********************************************************************** // Time in seconds. unsigned long Time::unix_time() const { return m_time; } //********************************************************************** // Formatted UTC. string Time::to_string() const { if ( ! is_valid() ) { return "invalid time"; } time_t utim = m_time; const tm* ptm = gmtime(&utim); char ctim[128]; const char* fmt = "%Y %b %d %H:%M:%S"; size_t nchar = strftime(ctim, 128, fmt, ptm); string stim(ctim); return stim; } //********************************************************************** // Methods for TimeInterval. //********************************************************************** // Constructor from interval. TimeInterval::TimeInterval(long dtime) : m_dtime(dtime) { } //********************************************************************** // Constructor from start and stop. TimeInterval::TimeInterval(Time start, Time stop) : m_dtime(stop.unix_time() - start.unix_time()) { } //********************************************************************** // Add time interval. TimeInterval& TimeInterval::operator+=(TimeInterval dtim) { m_dtime += dtim.seconds(); return *this; } //********************************************************************** // Subtract time interval. TimeInterval& TimeInterval::operator-=(TimeInterval dtim) { m_dtime -= dtim.seconds(); return *this; } //********************************************************************** // Interval in seconds. long TimeInterval::seconds() const { return m_dtime; } //********************************************************************** // Formatted interval. string TimeInterval::to_string() const { long rem = m_dtime; long sign = 1; if ( rem < 0 ) { sign = -1; rem = -rem; } long days = rem/(24*3600); rem -= 24*3600*days; long hours = rem/3600; rem -= 3600*hours; long mins = rem/60; rem -= 60*mins; long secs = rem; ostringstream ssint; if ( sign < 0 ) { ssint << "-"; } if ( days > 0 ) { ssint << days << ":" << setw(2) << setfill('0'); } ssint << hours << ":"; ssint << setw(2) << setfill('0') << mins << ":"; ssint << setw(2) << setfill('0') << secs; return ssint.str(); } //********************************************************************** // Free functions. //********************************************************************** // Time output stream. ostream& operator<<(std::ostream& lhs, const Time& rhs) { lhs << rhs.to_string(); return lhs; } //********************************************************************** // TimeInterval output stream. ostream& operator<<(std::ostream& lhs, const TimeInterval& rhs) { lhs << rhs.to_string(); return lhs; } //**********************************************************************