Loading...
Searching...
No Matches
TimeVal.h
Go to the documentation of this file.
// -*- c++ -*-
//------------------------------------------------------------------------------
// TimeVal.h
//------------------------------------------------------------------------------
// Copyright (c) 1999 by Vladislav Grinchenko
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//------------------------------------------------------------------------------
// Created: 09/28/1999
//------------------------------------------------------------------------------
#ifndef TIME_VAL_H
#define TIME_VAL_H
#include <sys/time.h> // gettimeofday(3)
#include <unistd.h> // gettimeofday(3)
#include <string>
using std::string;
namespace ASSA {
class TimeVal : public timeval
{
public:
enum {
gmt,
loc
};
TimeVal ();
TimeVal (long sec_, long msec_);
TimeVal (double d_);
TimeVal (const timeval& tv_);
TimeVal (const TimeVal& tv_);
operator double () const;
void sec (long sec_) { tv_sec = sec_; }
long sec (void) const { return tv_sec; }
void msec (long msec_) { tv_usec = msec_; }
long msec (void) const { return tv_usec; }
long millisec () const;
void tz (int tz_) { m_tz = tz_; }
int tz (void) const { return m_tz; }
TimeVal& operator= (const TimeVal& tv_);
TimeVal& operator+= (const TimeVal& rhs_);
TimeVal& operator-= (const TimeVal& rhs_);
friend TimeVal operator+(const TimeVal& lhs_, const TimeVal& rhs_);
friend TimeVal operator-(const TimeVal& lhs_, const TimeVal& rhs_);
bool operator< (const TimeVal& rhs_) const;
bool operator==(const TimeVal& rhs_) const;
friend bool operator> (const TimeVal& lhs_, const TimeVal& rhs_);
friend bool operator!=(const TimeVal& lhs_, const TimeVal& rhs_);
friend bool operator<=(const TimeVal& lhs_, const TimeVal& rhs_);
friend bool operator>=(const TimeVal& lhs_, const TimeVal& rhs_);
string fmtString (const char* fmt_ = NULL) const;
string fmt_hh_mm_ss () const;
string fmt_hh_mm_ss_mls () const;
string fmt_mm_ss () const;
string fmt_mm_ss_mls () const;
string fmt_ss_mls () const;
void dump_to_log (const string& name_ = "") const;
static TimeVal zeroTime () { return m_zero; }
static TimeVal gettimeofday ();
protected:
void init (long, long, int);
private:
void normalize ();
private:
int m_tz;
static TimeVal m_zero;
};
//------------------------------------------------------------------------------
// Inlines
//------------------------------------------------------------------------------
inline void
TimeVal::
init (long s_, long ms_, int tz_)
{
tv_sec = s_;
tv_usec = ms_;
m_tz = tz_;
normalize ();
}
inline
TimeVal::
TimeVal ()
{
init (0, 0, gmt);
}
inline
TimeVal::
TimeVal (long sec_, long msec_)
{
init (sec_, msec_, gmt);
}
inline
TimeVal::
TimeVal (double d_)
: m_tz (gmt)
{
long l = long(d_);
tv_sec = l;
tv_usec = (long) ((d_ - double(l))*1000000.0);
normalize();
}
inline
TimeVal::
TimeVal (const timeval& tv_)
{
init (tv_.tv_sec, tv_.tv_usec, gmt);
}
inline
TimeVal::
TimeVal (const TimeVal& tv_)
{
init (tv_.tv_sec, tv_.tv_usec, tv_.m_tz);
}
inline
TimeVal::operator double () const
{
return tv_sec + tv_usec / 1000000.0;
}
inline long
TimeVal::
millisec () const
{
return (msec () % 1000000) / 1000;
}
inline string
TimeVal::
fmt_hh_mm_ss () const
{
return fmtString ("%T");
}
inline string
TimeVal::
fmt_mm_ss () const
{
return fmtString ("%M:%S");
}
//------------------------------------------------------------------------------
// Friend functions
//------------------------------------------------------------------------------
inline TimeVal&
TimeVal::
operator=(const TimeVal& tv_)
{
init (tv_.tv_sec, tv_.tv_usec, tv_.m_tz);
return *this;
}
inline TimeVal
operator+(const TimeVal& lhs_, const TimeVal& rhs_)
{
TimeVal temp(lhs_);
temp += rhs_;
temp.normalize ();
return temp;
}
inline TimeVal
operator-(const TimeVal& lhs_, const TimeVal& rhs_)
{
TimeVal temp(lhs_);
temp -= rhs_;
temp.normalize ();
return temp;
}
inline bool
TimeVal::
operator<(const TimeVal& rhs_) const
{
return (tv_sec < rhs_.tv_sec
|| (tv_sec == rhs_.tv_sec && tv_usec < rhs_.tv_usec) ) ;
}
inline bool
TimeVal::
operator==(const TimeVal& rhs_) const
{
return !(*this < rhs_ || rhs_ < *this);
}
inline bool
operator> (const TimeVal& lhs_, const TimeVal& rhs_)
{
return rhs_ < lhs_;
}
inline bool
operator!=(const TimeVal& lhs_, const TimeVal& rhs_)
{
return !( lhs_ == rhs_ );
}
inline bool
operator<=(const TimeVal& lhs_, const TimeVal& rhs_)
{
return !(rhs_ < lhs_);
}
inline bool
operator>=(const TimeVal& lhs_, const TimeVal& rhs_)
{
return !(lhs_ < rhs_);
}
} // end namespace ASSA
#endif /* TIME_VAL_H */
ASSA::TimeVal
Definition TimeVal.h:31
ASSA::TimeVal::tz
void tz(int tz_)
Set timezone.
Definition TimeVal.h:81
ASSA::TimeVal::m_zero
static TimeVal m_zero
Zero time value.
Definition TimeVal.h:178
ASSA::TimeVal::fmtString
string fmtString(const char *fmt_=NULL) const
Format timeval structure into readable format.
Definition TimeVal.cpp:146
ASSA::TimeVal::fmt_mm_ss
string fmt_mm_ss() const
Format timeval structure in readable format MM:SS.
Definition TimeVal.h:255
ASSA::TimeVal::normalize
void normalize()
Normalization after arithmetic operation.
Definition TimeVal.cpp:112
ASSA::TimeVal::tz
int tz(void) const
Get timezone.
Definition TimeVal.h:84
ASSA::TimeVal::operator=
TimeVal & operator=(const TimeVal &tv_)
Definition TimeVal.h:266
ASSA::TimeVal::fmt_ss_mls
string fmt_ss_mls() const
Format timeval structure in readable format SS.MLS.
Definition TimeVal.cpp:208
ASSA::TimeVal::zeroTime
static TimeVal zeroTime()
Static that returns zero timeval: {0,0}.
Definition TimeVal.h:157
ASSA::TimeVal::operator+
friend TimeVal operator+(const TimeVal &lhs_, const TimeVal &rhs_)
Addition.
Definition TimeVal.h:273
ASSA::TimeVal::operator>=
friend bool operator>=(const TimeVal &lhs_, const TimeVal &rhs_)
Comparison.
Definition TimeVal.h:324
ASSA::TimeVal::operator>
friend bool operator>(const TimeVal &lhs_, const TimeVal &rhs_)
Comparison.
Definition TimeVal.h:306
ASSA::TimeVal::operator<=
friend bool operator<=(const TimeVal &lhs_, const TimeVal &rhs_)
Comparison.
Definition TimeVal.h:318
ASSA::TimeVal::fmt_mm_ss_mls
string fmt_mm_ss_mls() const
Format timeval structure in readable format MM:SS.MLS.
Definition TimeVal.cpp:189
ASSA::TimeVal::operator!=
friend bool operator!=(const TimeVal &lhs_, const TimeVal &rhs_)
Comparison.
Definition TimeVal.h:312
ASSA::TimeVal::fmt_hh_mm_ss
string fmt_hh_mm_ss() const
Format timeval structure in readable format HH:MM:SS.
Definition TimeVal.h:248
ASSA::TimeVal::msec
long msec(void) const
Get microseconds.
Definition TimeVal.h:73
ASSA::TimeVal::operator+=
TimeVal & operator+=(const TimeVal &rhs_)
Addition.
Definition TimeVal.cpp:74
ASSA::TimeVal::gmt
@ gmt
GMT.
Definition TimeVal.h:34
ASSA::TimeVal::loc
@ loc
Local Time Zone.
Definition TimeVal.h:35
ASSA::TimeVal::millisec
long millisec() const
Convert tv_usec's microseconds (=1/1,000,000 sec) to milliseconds (=1/1,000 sec).
Definition TimeVal.h:241
ASSA::TimeVal::operator-
friend TimeVal operator-(const TimeVal &lhs_, const TimeVal &rhs_)
Substraction.
Definition TimeVal.h:282
ASSA::TimeVal::init
void init(long, long, int)
Internal initialization common to most constructors.
Definition TimeVal.h:186
ASSA::TimeVal::dump_to_log
void dump_to_log(const string &name_="") const
Dump value of struct timeval to the log file with mask TRACE = DBG_APP15.
Definition TimeVal.cpp:227
ASSA::TimeVal::sec
void sec(long sec_)
Set seconds.
Definition TimeVal.h:64
ASSA::TimeVal::operator<
bool operator<(const TimeVal &rhs_) const
Comparison.
Definition TimeVal.h:292
ASSA::TimeVal::TimeVal
TimeVal()
Default constructor.
Definition TimeVal.h:196
ASSA::TimeVal::gettimeofday
static TimeVal gettimeofday()
Shields off underlying OS differences in getting current time.
Definition TimeVal.cpp:44
ASSA::TimeVal::sec
long sec(void) const
Get secons.
Definition TimeVal.h:67
ASSA::TimeVal::operator-=
TimeVal & operator-=(const TimeVal &rhs_)
Substraction.
Definition TimeVal.cpp:93
ASSA::TimeVal::operator==
bool operator==(const TimeVal &rhs_) const
Equality.
Definition TimeVal.h:300
ASSA::TimeVal::msec
void msec(long msec_)
Set microseconds.
Definition TimeVal.h:70
ASSA::TimeVal::fmt_hh_mm_ss_mls
string fmt_hh_mm_ss_mls() const
Format timeval structure in readable format HH:MM:SS.MLS.
Definition TimeVal.cpp:170
ASSA::TimeVal::m_tz
int m_tz
Time zone.
Definition TimeVal.h:175
ASSA
Definition Acceptor.h:40
ASSA::operator!=
bool operator!=(const TimeVal &lhs_, const TimeVal &rhs_)
Definition TimeVal.h:312
ASSA::operator>=
bool operator>=(const TimeVal &lhs_, const TimeVal &rhs_)
Definition TimeVal.h:324
ASSA::operator+
TimeVal operator+(const TimeVal &lhs_, const TimeVal &rhs_)
Definition TimeVal.h:273
ASSA::operator>
bool operator>(const TimeVal &lhs_, const TimeVal &rhs_)
Definition TimeVal.h:306
ASSA::operator-
TimeVal operator-(const TimeVal &lhs_, const TimeVal &rhs_)
Definition TimeVal.h:282
ASSA::operator<=
bool operator<=(const TimeVal &lhs_, const TimeVal &rhs_)
Definition TimeVal.h:318