Loading...
Searching...
No Matches
TimeVal.cpp
Go to the documentation of this file.
// -*- c++ -*-
//------------------------------------------------------------------------------
// TimeVal.cpp
//------------------------------------------------------------------------------
// Copyright (c) 2000 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/99
//------------------------------------------------------------------------------
#include <time.h> // localtime(3), gmtime(3)
#include <stdio.h> // sprintf(3)
#include "TimeVal.h"
#include "Logger.h"
#if defined (WIN32)
# include <windows.h>
#endif
using namespace ASSA;
//------------------------------------------------------------------------------
// Static definitions
//------------------------------------------------------------------------------
TimeVal TimeVal::m_zero; // zero time
static const long ONE_SECOND = 1000000;
#ifndef __GNUC__
#define EPOCHFILETIME (116444736000000000i64)
#else
#define EPOCHFILETIME (116444736000000000LL)
#endif
//------------------------------------------------------------------------------
// Wrappers
//------------------------------------------------------------------------------
TimeVal
TimeVal::
gettimeofday ()
{
timeval tv;
#ifdef WIN32
FILETIME ft;
LARGE_INTEGER li;
__int64 t;
static int tzflag;
GetSystemTimeAsFileTime(&ft);
li.LowPart = ft.dwLowDateTime;
li.HighPart = ft.dwHighDateTime;
t = li.QuadPart; /* In 100-nanosecond intervals */
t -= EPOCHFILETIME; /* Offset to the Epoch time */
t /= 10; /* In microseconds */
tv.tv_sec = (long)(t / 1000000);
tv.tv_usec = (long)(t % 1000000);
#else
::gettimeofday (&tv, 0);
#endif
return tv;
}
//------------------------------------------------------------------------------
// Membe functions
//------------------------------------------------------------------------------
TimeVal&
TimeVal::
operator+=(const TimeVal& rhs_)
{
tv_sec += rhs_.tv_sec;
tv_usec += rhs_.tv_usec;
if (tv_usec >= ONE_SECOND) {
tv_usec -= ONE_SECOND;
tv_sec++;
}
else if (tv_sec >= 1 && tv_usec < 0) {
tv_usec += ONE_SECOND;
tv_sec--;
}
normalize ();
return *this;
}
TimeVal&
TimeVal::
operator-=(const TimeVal& rhs_)
{
tv_sec -= rhs_.tv_sec;
tv_usec -= rhs_.tv_usec;
if (tv_usec < 0) {
tv_usec += ONE_SECOND;
tv_sec--;
}
else if (tv_usec >= ONE_SECOND) {
tv_usec -= ONE_SECOND;
tv_sec++;
}
normalize ();
return *this;
}
void
TimeVal::
normalize ()
{
if (tv_usec >= ONE_SECOND) {
do {
tv_sec++;
tv_usec -= ONE_SECOND;
}
while (tv_usec >= ONE_SECOND);
}
else if (tv_usec <= -ONE_SECOND) {
do {
tv_sec--;
tv_usec += ONE_SECOND;
}
while (tv_usec <= -ONE_SECOND);
}
if (tv_sec >= 1 && tv_usec < 0) {
tv_sec--;
tv_usec += ONE_SECOND;
}
else if (tv_sec < 0 && tv_usec > 0) {
tv_sec++;
tv_usec -= ONE_SECOND;
}
}
//------------------------------------------------------------------------------
// All possible variation of HH:MM:SS.MMM I could think of:
//------------------------------------------------------------------------------
string
TimeVal::
fmtString (const char* fmt_) const
{
struct tm ct;
char buf[80];
memset (buf, 0, 80);
if (m_tz == gmt)
ct = *( localtime ((const time_t*) &tv_sec) );
else
ct = *( gmtime ((const time_t*) &tv_sec) );
if (fmt_ == NULL) {
strftime (buf, 80, "%Y/%j %H:%M:%S", &ct);
sprintf (buf + strlen(buf),
".%03ld", (tv_usec %1000000)/1000);
}
else {
strftime(buf, 80, fmt_, &ct);
}
return string (buf);
}
string
TimeVal::
fmt_hh_mm_ss_mls () const
{
struct tm ct;
char buf [80];
memset (buf, 0, 80);
if (m_tz == gmt)
ct = *( localtime ((const time_t*) &tv_sec) );
else
ct = *( gmtime ((const time_t*) &tv_sec) );
strftime (buf, 80, "%H:%M:%S", &ct);
sprintf (buf + strlen(buf), ".%03ld", millisec ());
return string (buf);
}
string
TimeVal::
fmt_mm_ss_mls () const
{
struct tm ct;
char buf [80];
memset (buf, 0, 80);
if (m_tz == gmt)
ct = *( localtime ((const time_t*) &tv_sec) );
else
ct = *( gmtime ((const time_t*) &tv_sec) );
strftime (buf, 80, "%M:%S", &ct);
sprintf (buf + strlen(buf), ".%03ld", millisec ());
return string (buf);
}
string
TimeVal::
fmt_ss_mls () const
{
struct tm ct;
char buf [80];
memset (buf, 0, 80);
if (m_tz == gmt)
ct = *( localtime ((const time_t*) &tv_sec) );
else
ct = *( gmtime ((const time_t*) &tv_sec) );
strftime (buf, 80, "%S", &ct);
sprintf (buf + strlen(buf), ".%03ld", millisec ());
return string (buf);
}
void
TimeVal::
dump_to_log (const string& var_name_) const
{
static const char self []="TimeVal::dump_to_log"; trace(self);
if (Logger::get_instance ()->group_enabled (REACT))
{
DL((REACT,"=== TimeVal %s ===\n", var_name_.c_str ()));
DL((REACT,"MM:SS:MLS = %s\n", fmt_mm_ss_mls ().c_str ()));
DL((REACT,"tv_sec = %d, tv_msec = %d, tv_mls = %d\n",
sec (), msec (), millisec ()));
DL((REACT,"(double) = %7.4f\n", double (*this)));
DL((REACT,"==================\n"));
}
}
Logger.h
An abstraction to message logging facility.
trace
#define trace(s)
trace() is used to trace function call chain in C++ program.
Definition Logger.h:429
DL
#define DL(X)
A macro for writing debug message to the Logger.
Definition Logger.h:273
EPOCHFILETIME
#define EPOCHFILETIME
Definition TimeVal.cpp:34
ONE_SECOND
static const long ONE_SECOND
Definition TimeVal.cpp:31
TimeVal.h
Class TimeVal is a wrapper around UNIX timeval structure.
ASSA::Singleton< Logger >::get_instance
static Logger * get_instance()
Definition Singleton.h:47
ASSA::TimeVal
Definition TimeVal.h:31
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::normalize
void normalize()
Normalization after arithmetic operation.
Definition TimeVal.cpp:112
ASSA::TimeVal::fmt_ss_mls
string fmt_ss_mls() const
Format timeval structure in readable format SS.MLS.
Definition TimeVal.cpp:208
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+=
TimeVal & operator+=(const TimeVal &rhs_)
Addition.
Definition TimeVal.cpp:74
ASSA::TimeVal::gmt
@ gmt
GMT.
Definition TimeVal.h:34
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::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::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::operator-=
TimeVal & operator-=(const TimeVal &rhs_)
Substraction.
Definition TimeVal.cpp:93
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::REACT
@ REACT
Class Reactor/PrioriyQueue messages.
Definition LogMask.h:39