/search.css" rel="stylesheet" type="text/css"/> /search.js">
| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

In This Package:

Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | Friends
TimeStamp Class Reference

Encapsulate the seconds and ns since EPOCH. More...

#include <TimeStamp.h>

List of all members.

Public Member Functions

 TimeStamp ()
 Empty ctor (builds current time with nsec field filled as best possible)
 TimeStamp (const TimeStamp &source)
 Copy.
TimeStampoperator= (const TimeStamp &source)
 Assignment.
 TimeStamp (const timespec &ts)
 Construction from timespec struct.
 TimeStamp (const time_t &t, const int nsec)
 Construction from time_t and separate nsec.
 TimeStamp (unsigned int year, unsigned int month, unsigned int day, unsigned int hour, unsigned int min, unsigned int sec, unsigned int nsec=0, bool isUTC=true, int secOffset=0)
 Create a TimeStamp and set it to the specified year, month, day, time, hour, minute, second and nanosec.
 TimeStamp (unsigned int date, unsigned int time, unsigned int nsec, bool isUTC=true, int secOffset=0)
 Create a TimeStamp and set it to the specified date, time, nanosec.
 TimeStamp (double seconds)
virtual ~TimeStamp ()
 operator double () const
timespec GetTimeSpec () const
 Get timestamp as a timespec.
time_t GetSec (void) const
 Get (integral) seconds after the EPOCH.
int GetNanoSec (void) const
 Get nanoseconds after the second.
double GetSeconds (void) const
 Get time from the epoch in seconds.
const char * AsString (const char *option="") const
void Copy (TimeStamp &vldts) const
int GetDate (bool inUTC=true, int secOffset=0, unsigned int *year=0, unsigned int *month=0, unsigned int *day=0) const
 Return date in form of 19971224 (i.e.
int GetTime (bool inUTC=true, int secOffset=0, unsigned int *hour=0, unsigned int *min=0, unsigned int *sec=0) const
 Return time in form of 123623 (i.e.
void Add (const TimeStamp &offset)
void Add (double seconds)
void Subtract (const TimeStamp &offset)
void Subtract (double seconds)
TimeStamp CloneAndSubtract (const TimeStamp &offset)
void Print (const char *option="") const
bool IsNull () const

Static Public Member Functions

static TimeStamp GetBOT ()
 Static method to return the "beginning of time" (start of Unix EPOCH)
static TimeStamp GetEOT ()
 Static method to return the "end of time" which is sometime in the future and near or at the limit of TimeStamp's ability to hold large times.
static TimeStamp GetNBOT ()
 Static method to return the "negative beginning of time", i.e.
static int GetZoneOffset ()
 Static method returning local (current) time zone offset from UTC.
static time_t MktimeFromUTC (tm *tmstruct)
static bool IsLeapYear (int year)
 Is the given year a leap year.
static void DumpTMStruct (const tm &tmstruct)
 Print out the "tm" structure:

Private Member Functions

void Set ()
void Set (int year, int month, int day, int hour, int min, int sec, int nsec, bool isUTC, int secOffset)
void Set (int date, int time, int nsec, bool isUTC, int secOffset)
void NormalizeNanoSec ()

Private Attributes

int mSec
int mNanoSec

Friends

bool operator== (const TimeStamp &lhs, const TimeStamp &rhs)
bool operator!= (const TimeStamp &lhs, const TimeStamp &rhs)
bool operator< (const TimeStamp &lhs, const TimeStamp &rhs)
bool operator<= (const TimeStamp &lhs, const TimeStamp &rhs)
bool operator> (const TimeStamp &lhs, const TimeStamp &rhs)
bool operator>= (const TimeStamp &lhs, const TimeStamp &rhs)
TimeStamp operator- (const TimeStamp &lhs, const TimeStamp &rhs)

Detailed Description

Encapsulate the seconds and ns since EPOCH.

Stolen shamelessly from MINOS framework.

CAUTION: this class takes and returns values that are implicitly in units of seconds and not in the standard CLHEP SystemOfUnits that are used elsewhere. Be sure to convert take this into account when mixing TimeStamps with times that are expressed in the SystemOfUnits. For example:

#include "CLHEP/Units/SystemOfUnits.h" ... double timeStampToSystemOfUnits(const TimeStamp& ts) { // States that ts *IS* in seconds so multiply return (double)ts * CLHEP::seconds; }

TimeStamp systemOfUnitsToTimeStamp(double time) { // States that we *WANT* time in seconds so divide return TimeStamp(time / CLHEP::seconds); }

TimeStamp extends (and isolates) struct timespec

struct timespec { time_t tv_sec; // seconds long tv_nsec; // nanoseconds } time_t seconds is relative to Jan 1, 1970 00:00:00 UTC

Due to ROOT/CINT limitations TimeStamp does not explicitly hold a timespec struct; attempting to do so means the Streamer must be hand written. Instead we have chosen to simply contain similar fields within the private area of this class.

Note:
the use of time_t (and its default implementation as a 32 int) implies overflow conditions occurs somewhere around Jan 18, 19:14:07, 2038. If this experiment is still going when it becomes significant someone will have to deal with it.
Author:
Robert Hatcher (MINOS). Introduced to Dyb by Nathaniel Tagg.
Version:
Revision:
1.2
Date:
Date:
2006/10/31 19:59:24

Contact: N. Tagg

Created on: Wed Apr 13 17:53:23 2005

Id:
TimeStamp.hh,v 1.2 2006/10/31 19:59:24 tagg Exp

Definition at line 81 of file TimeStamp.h.


Constructor & Destructor Documentation

TimeStamp::TimeStamp ( )

Empty ctor (builds current time with nsec field filled as best possible)

Definition at line 75 of file TimeStamp.cc.

{ Set(); }
TimeStamp::TimeStamp ( const TimeStamp source) [inline]

Copy.

Definition at line 110 of file TimeStamp.h.

        { mSec = source.mSec; mNanoSec = source.mNanoSec; }
TimeStamp::TimeStamp ( const timespec &  ts) [inline]

Construction from timespec struct.

Definition at line 118 of file TimeStamp.h.

        { mSec = ts.tv_sec; mNanoSec = ts.tv_nsec; NormalizeNanoSec(); }
TimeStamp::TimeStamp ( const time_t &  t,
const int  nsec 
) [inline]

Construction from time_t and separate nsec.

Definition at line 122 of file TimeStamp.h.

        { mSec = t; mNanoSec = 0; mNanoSec = nsec; NormalizeNanoSec(); }
TimeStamp::TimeStamp ( unsigned int  year,
unsigned int  month,
unsigned int  day,
unsigned int  hour,
unsigned int  min,
unsigned int  sec,
unsigned int  nsec = 0,
bool  isUTC = true,
int  secOffset = 0 
)

Create a TimeStamp and set it to the specified year, month, day, time, hour, minute, second and nanosec.

If !isUTC then it is assumed to be the standard local time zone.

If local time is PST then one can use

TimeStamp(year,month,day,hour,min,sec,nsec,false,0);

or

int secOffset = 8*60*60; TimeStamp(year,month,day,hour,min,sec,nsec,true,8*60*60);

Definition at line 79 of file TimeStamp.cc.

{
    // Create a TimeStamp and set it to the specified year, month,
    // day, time, hour, minute, second and nanosec.
    // If !isUTC then it is assumed to be the standard local time zone.
    //
    // If local time is PST then one can use
    //    TimeStamp(year,month,day,hour,min,sec,nsec,kFALSE,0);
    // or
    //    int secOffset = 8*60*60;
    //    TimeStamp(year,month,day,hour,min,sec,nsec,kTRUE,8*60*60);
   
    Set(year, month, day, hour, min, sec, nsec, isUTC, secOffset);
}
TimeStamp::TimeStamp ( unsigned int  date,
unsigned int  time,
unsigned int  nsec,
bool  isUTC = true,
int  secOffset = 0 
)

Create a TimeStamp and set it to the specified date, time, nanosec.

If !isUTC then it is assumed to be the standard local time zone.

Definition at line 99 of file TimeStamp.cc.

{
    // Create a TimeStamp and set it to the specified date, time, nanosec.
    // If !isUTC then it is assumed to be the standard local time zone.

    Set(date, time, nsec, isUTC, secOffset);
}
TimeStamp::TimeStamp ( double  seconds) [inline]

Definition at line 155 of file TimeStamp.h.

        { mSec = (int)seconds; mNanoSec = (int)((seconds-mSec)*1.0e9); 
            NormalizeNanoSec();}
TimeStamp::~TimeStamp ( ) [virtual]

Definition at line 76 of file TimeStamp.cc.

{ ; }

Member Function Documentation

TimeStamp TimeStamp::GetBOT ( ) [static]

Static method to return the "beginning of time" (start of Unix EPOCH)

Definition at line 54 of file TimeStamp.cc.

{
    return TimeStamp((time_t)0,0);
}
TimeStamp TimeStamp::GetEOT ( ) [static]

Static method to return the "end of time" which is sometime in the future and near or at the limit of TimeStamp's ability to hold large times.

Definition at line 59 of file TimeStamp.cc.

{
    return TimeStamp((time_t)INT_MAX,0);
}
TimeStamp TimeStamp::GetNBOT ( ) [static]

Static method to return the "negative beginning of time", i.e.

the earliest time prior to start of Unix EPOCH which is negative seconds.

Definition at line 64 of file TimeStamp.cc.

{
    return TimeStamp((time_t)INT_MIN,0);
}
TimeStamp& TimeStamp::operator= ( const TimeStamp source) [inline]

Assignment.

Definition at line 113 of file TimeStamp.h.

        { if (this != &source) {mSec = source.mSec; mNanoSec = source.mNanoSec;}
            return *this; }
TimeStamp::operator double ( ) const [inline]

Definition at line 169 of file TimeStamp.h.

{ return mSec + 1.0e-9 * mNanoSec; }
timespec TimeStamp::GetTimeSpec ( ) const [inline]

Get timestamp as a timespec.

Definition at line 173 of file TimeStamp.h.

{ timespec value = {mSec,mNanoSec}; return value; }
time_t TimeStamp::GetSec ( void  ) const [inline]

Get (integral) seconds after the EPOCH.

Definition at line 176 of file TimeStamp.h.

{ return mSec;}
int TimeStamp::GetNanoSec ( void  ) const [inline]

Get nanoseconds after the second.

Definition at line 178 of file TimeStamp.h.

{ return mNanoSec; }
double TimeStamp::GetSeconds ( void  ) const [inline]

Get time from the epoch in seconds.

Definition at line 181 of file TimeStamp.h.

{ return mSec+(mNanoSec/1.0e9); }
const char * TimeStamp::AsString ( const char *  option = "") const

Definition at line 109 of file TimeStamp.cc.

{
    // Return the date & time as a string.
    //
    // Result is pointer to a statically allocated string.
    // User should copy this into their own buffer before calling
    // this method again.  This is somewhat mitigated
    // by use of a circular buffer of strings.
    //
    // Option "l" returns it in local zone format
    // (can be applied to default or compact format).
    //
    // Default format is RFC822 compliant:
    //   "Mon, 02 Jan 2001 18:11:12 +0000 (GMT) +999999999 nsec"
    //   "Mon, 02 Jan 2001 10:11:12 -0800 (PST) +999999999 nsec"
    //
    // Option "c" compact is (almost) ISO 8601 compliant:
    //   "2001-01-02 18:11:12.9999999999Z"
    //   "2001-01-02 10:11:12.9999999999-0800"  if PST
    //      * uses "-" as date separator as specified in ISO 8601
    //      * uses "." rather than preferred "," for decimal separator
    //      * -HHMM is the difference between local and UTC (if behind, + if ahead).
    //   The "-HHMM" is replaced with "Z" if given as UTC.
    //   To be strictly conforming it should use "T" instead of the
    //   blank separating the date and time.
    //
    // Option "2" returns as {sec,nsec} integers.
    //
    // Option "s" returns "2001-01-02 18:11:12" with an implied UTC,
    // overrides "l" option.

    // Internally uses a circular list of buffers to avoid problems
    // using AsString multiple times in a single statement.

    const int nbuffers = 8;     // # of buffers

    static char formatted[nbuffers][64];  // strftime fields substituted
    static char formatted2[nbuffers][64]; // nanosec field substituted
    static int ibuffer = nbuffers;
    ibuffer = (ibuffer+1)%nbuffers; // each call moves to next buffer

    std::string opt = option;
    std::transform(opt.begin(),opt.end(),opt.begin(),::tolower);

    if (opt.find("2") != std::string::npos) {
        // return string formatted as integer {sec,nsec}
        sprintf(formatted[ibuffer], "{%d,%d}", mSec, mNanoSec);
        return formatted[ibuffer];
    }

#ifdef linux
    // under linux %z is the hour offset and %Z is the timezone name
    const char *RFC822   = "%a, %d %b %Y %H:%M:%S %z (%Z) +#9ld nsec";
    const char *ISO8601  = "%Y-%m-%d %H:%M:%S.#9.9ld%z";
    const char *ISO8601Z = "%Y-%m-%d %H:%M:%S.#9.9ldZ";
#else
    // otherwise only %Z is guarenteed to be defind
    const char *RFC822   = "%a, %d %b %Y %H:%M:%S %Z +#9ld nsec";
    const char *ISO8601  = "%Y-%m-%d %H:%M:%S.#9.9ld%Z";
    const char *ISO8601Z = "%Y-%m-%d %H:%M:%S.#9.9ldZ";
#endif
    const char *SQL = "%Y-%m-%d %H:%M:%S";

    bool asLocal = (opt.find("l") != std::string::npos);
    bool asSQL   = (opt.find("s") != std::string::npos);
    if (asSQL) asLocal = false;

    const char *format = RFC822;
    if (opt.find("c") != std::string::npos) {
        format = ISO8601;
        if (!asLocal) format = ISO8601Z;
    }
    if (asSQL) format = SQL;

    struct tm *ptm;
    time_t seconds = (time_t) mSec;   // deal with possible mismatch of types 
    // of mSec and the time_t required 
    // by functions

    // get the components into a tm struct
    ptm = (asLocal) ? localtime(&seconds) : gmtime(&seconds);

    // format all but the nsec field
    // size_t length =
    strftime(formatted[ibuffer], sizeof(formatted[ibuffer]), format, ptm);

    if (asSQL) return formatted[ibuffer];

    // hack in the nsec part
    char *ptr = strrchr(formatted[ibuffer], '#');
    if (ptr) *ptr = '%';    // substitute % for #
    sprintf(formatted2[ibuffer], formatted[ibuffer], mNanoSec);

    return formatted2[ibuffer];
}
void TimeStamp::Copy ( TimeStamp vldts) const

Definition at line 206 of file TimeStamp.cc.

{
    // Copy this to ts.

    ts.mSec     = mSec;
    ts.mNanoSec = mNanoSec;

}
int TimeStamp::GetDate ( bool  inUTC = true,
int  secOffset = 0,
unsigned int *  year = 0,
unsigned int *  month = 0,
unsigned int *  day = 0 
) const

Return date in form of 19971224 (i.e.

24/12/1997), if non-zero pointers supplied for year, month, day fill those as well

Definition at line 216 of file TimeStamp.cc.

{
    // Return date in form of 19971224 (i.e. 24/12/1997),
    // if non-zero pointers supplied for year, month, day fill those as well

    time_t atime = mSec + secOffset;
    struct tm *ptm = (inUTC) ? gmtime(&atime) : localtime(&atime);

    if (year)  *year  = ptm->tm_year + 1900;
    if (month) *month = ptm->tm_mon + 1;
    if (day)   *day   = ptm->tm_mday;

    return (1900+ptm->tm_year)*10000 + (1+ptm->tm_mon)*100 + ptm->tm_mday;

}
int TimeStamp::GetTime ( bool  inUTC = true,
int  secOffset = 0,
unsigned int *  hour = 0,
unsigned int *  min = 0,
unsigned int *  sec = 0 
) const

Return time in form of 123623 (i.e.

12:36:23), if non-zero pointers supplied for hour, min, sec fill those as well

Definition at line 234 of file TimeStamp.cc.

{
    // Return time in form of 123623 (i.e. 12:36:23),
    // if non-zero pointers supplied for hour, min, sec fill those as well

    time_t atime = mSec + secOffset;
    struct tm *ptm = (inUTC) ? gmtime(&atime) : localtime(&atime);

    if (hour) *hour = ptm->tm_hour;
    if (min)  *min  = ptm->tm_min;
    if (sec)  *sec  = ptm->tm_sec;

    return ptm->tm_hour*10000 + ptm->tm_min*100 + ptm->tm_sec;

}
void TimeStamp::Add ( const TimeStamp offset)

Definition at line 274 of file TimeStamp.cc.

{
    // Add "offset" as a delta time.

    mSec     += offset.mSec;
    mNanoSec += offset.mNanoSec;
    NormalizeNanoSec();

}
void TimeStamp::Add ( double  seconds)

Definition at line 284 of file TimeStamp.cc.

{
    // Add 'seconds' as a delta time

    mSec += (int) seconds;
    mNanoSec += (int) (fmod(seconds,1.0) * 1e9);
    NormalizeNanoSec();
    if(seconds > 1e6) 
        std::cerr << "TimeStamp moved by offset " << seconds <<" which is too large to maintain ns accuracy." << std::endl;
}
void TimeStamp::Subtract ( const TimeStamp offset)

Definition at line 295 of file TimeStamp.cc.

{
    // Subtract "offset" as a delta time.

    mSec     -= offset.mSec;
    mNanoSec -= offset.mNanoSec;
    NormalizeNanoSec();

}
void TimeStamp::Subtract ( double  seconds)

Definition at line 312 of file TimeStamp.cc.

{
    // Subtract 'seconds' as a delta time

    mSec -= (int) seconds;
    mNanoSec -= (int) (fmod(seconds,1.0) * 1e9);
    NormalizeNanoSec();
    if(seconds > 1e6) 
        std::cerr << "TimeStamp moved by offset " << seconds <<" which is too large to maintain ns accuracy." << std::endl;
}
TimeStamp TimeStamp::CloneAndSubtract ( const TimeStamp offset)

Definition at line 306 of file TimeStamp.cc.

{
    // same as operator -, but can be called from PyROOT
    return *this - offset;
}
void TimeStamp::Print ( const char *  option = "") const

Definition at line 325 of file TimeStamp.cc.

{
    // Print date and time.

    printf("Date/Time = %s\n", AsString(option));

}
bool TimeStamp::IsNull ( ) const [inline]

Definition at line 238 of file TimeStamp.h.

{ return (mSec==0)&&(mNanoSec==0); };
int TimeStamp::GetZoneOffset ( ) [static]

Static method returning local (current) time zone offset from UTC.

This is the difference in seconds between UTC and local standard time.

Definition at line 252 of file TimeStamp.cc.

{
    // Static method returning local (current) time zone offset from UTC.
    // This is the difference in seconds between UTC and local standard time.

    // ?? should tzset (_tzset) be called?
#ifndef R__WIN32
    tzset();
#if !defined(R__FBSD) && !defined(__APPLE__)
    return  timezone;   /* unix has extern long int */
#else
    time_t *tp = 0;
    time(tp);
    return localtime(tp)->tm_gmtoff;
#endif
#else
    _tzset();
    return _timezone;   /* Win32 prepends "_" */
#endif
}
time_t TimeStamp::MktimeFromUTC ( tm *  tmstruct) [static]

Definition at line 480 of file TimeStamp.cc.

{
    // Equivalent of standard routine "mktime" but
    // using the assumption that tm struct is filled with UTC, not local, time.

    // This version *ISN'T* configured to handle every possible
    // weirdness of out-of-range values in the case of normalizing
    // the tm struct.

    // This version *DOESN'T* correctly handle values that can't be
    // fit into a time_t (i.e. beyond year 2038-01-18 19:14:07, or
    // before the start of Epoch).

    const int days[]     = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int daysLeap[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    int year = tmstruct->tm_year + 1900;
    bool isleap = TimeStamp::IsLeapYear(year);

    const int *daysInMonth = days;
    if (isleap) daysInMonth = daysLeap;

    // fill in tmstruct->tm_yday

    int &ref_tm_mon = tmstruct->tm_mon;
    int &ref_tm_mday = tmstruct->tm_mday;
    // count days in months past
    tmstruct->tm_yday = 0;
    for (int imonth = 0; imonth < ref_tm_mon; imonth++) {
        tmstruct->tm_yday += daysInMonth[imonth];
    }
    tmstruct->tm_yday += ref_tm_mday - 1;  // day [1-31] but yday [0-365]

    // adjust if day in this month is more than the month has
    while (ref_tm_mday > daysInMonth[ref_tm_mon]) {
        ref_tm_mday -= daysInMonth[ref_tm_mon];
        ref_tm_mon++;
    }

    // *should* calculate tm_wday (0-6) here ...

    // UTC is never DST
    tmstruct->tm_isdst = 0;

    // Calculate seconds since the Epoch based on formula in
    // POSIX  IEEEE Std 1003.1b-1993 pg 22

    int utc_sec = tmstruct->tm_sec +
        tmstruct->tm_min*60 +
        tmstruct->tm_hour*3600 +
        tmstruct->tm_yday*86400 +
        (tmstruct->tm_year-70)*31536000 +
        ((tmstruct->tm_year-69)/4)*86400;

    return utc_sec;
}
bool TimeStamp::IsLeapYear ( int  year) [static]

Is the given year a leap year.

Definition at line 538 of file TimeStamp.cc.

{
    // Is the given year a leap year.


    // The calendar year is 365 days long, unless the year is exactly divisible
    // by 4, in which case an extra day is added to February to make the year
    // 366 days long. If the year is the last year of a century, eg. 1700, 1800,
    // 1900, 2000, then it is only a leap year if it is exactly divisible by
    // 400. Therefore, 1900 wasn't a leap year but 2000 was. The reason for
    // these rules is to bring the average length of the calendar year into
    // line with the length of the Earth's orbit around the Sun, so that the
    // seasons always occur during the same months each year.

    if (year%4 != 0) {
        return false;
    }
    else {
        if (year%400 == 0) {
            return true;
        }
        else { 
            if (year%100 == 0) {
                return false;
            }
            else {
                return true;
            }
        }
    }

}
void TimeStamp::DumpTMStruct ( const tm &  tmstruct) [static]

Print out the "tm" structure:

Definition at line 572 of file TimeStamp.cc.

{
    // Print out the "tm" structure:
    // tmstruct.tm_year = year;    // years since 1900
    // tmstruct.tm_mon  = month-1; // months since Jan [0,11]
    // tmstruct.tm_mday = day;     // day of the month [1,31]
    // tmstruct.tm_hour = hour;    // hours since midnight [0,23]
    // tmstruct.tm_min  = min;     // minutes after the hour [0,59]
    // tmstruct.tm_sec  = sec;     // seconds after the minute [0,59]
    // tmstruct.tm_wday            // day of week [0,6]
    // tmstruct.tm_yday            // days in year [0,365]
    // tmstruct.tm_isdst           // DST [-1/0/1]  (unknown,false,true)

    printf(" tm { year %4d, mon   %2d, day   %2d,\n",
           tmstruct.tm_year,
           tmstruct.tm_mon,
           tmstruct.tm_mday);
    printf("      hour   %2d, min   %2d, sec   %2d,\n",
           tmstruct.tm_hour,
           tmstruct.tm_min,
           tmstruct.tm_sec);
    printf("      wday   %2d, yday %3d, isdst %2d",
           tmstruct.tm_wday,
           tmstruct.tm_yday,
           tmstruct.tm_isdst);
#ifdef linux
    printf(",\n      tm_gmtoff %7ld,  tm_zone \"%s\"",
#ifdef __USE_BSD
           tmstruct.tm_gmtoff,tmstruct.tm_zone);
#else
    tmstruct.__tm_gmtoff,tmstruct.__tm_zone);
#endif
#endif
    printf("}\n");
}
void TimeStamp::Set ( ) [private]

Definition at line 336 of file TimeStamp.cc.

{
    // Set Date/Time to current time as reported by the system.
    // no accounting for nanoseconds with std ANSI functions,
    // ns part faked so that subsequent calls simply add 1 to it
    // this ensures that calls within the same second come back
    // distinct (and sortable).

    // this should work on UNIX to get microsec precision
    // we'll stick to a ns hack to make calls unique
    struct timeval now;
    if (!gettimeofday(&now,0)) {
        mSec     = now.tv_sec;
        mNanoSec = now.tv_usec * 1000;
    }
    else {
        time_t nowtime;
        time(&nowtime);
        mSec     = nowtime;
        mNanoSec = 0;
    }

    static int sec = 0, nsec = 0, fake_ns = 0;

    if (mSec == sec && mNanoSec == nsec)
        mNanoSec += ++fake_ns;
    else {
        fake_ns = 0;
        sec     = mSec;
        nsec    = mNanoSec;
    }

}
void TimeStamp::Set ( int  year,
int  month,
int  day,
int  hour,
int  min,
int  sec,
int  nsec,
bool  isUTC,
int  secOffset 
) [private]

Definition at line 371 of file TimeStamp.cc.

{
    // Set Date/Time from components.
    // 
    // month & day both use normal 1..12 and 1..31 counting
    // hours, min, sec run from 0 to 23, 59, 59 respectively;
    // secOffset provides method for adjusting for alternative timezones
    //
    // "year"  |    0    1 ... 37 | 38...69   |   70 .. 100  101 ..  137
    // true    | 2000 2001   2037 | undefined | 1970   2000 2001 .. 2037
    //
    // "year"  | 138...1969 | 1970 .. 2037 | ...
    // true    | undefined  | 1970 .. 2037 | undefined
    //


    // deal with special formats of year
    if (year <= 37)                year += 2000;
    if (year >= 70 && year <= 137) year += 1900;
    // tm.tm_year is years since 1900
    if (year >= 1900)              year -= 1900;

    struct tm tmstruct;
    tmstruct.tm_year  = year;    // years since 1900
    tmstruct.tm_mon   = month-1; // months since Jan [0,11]
    tmstruct.tm_mday  = day;     // day of the month [1,31]
    tmstruct.tm_hour  = hour;    // hours since midnight [0,23]
    tmstruct.tm_min   = min;     // minutes after the hour [0,59]
    tmstruct.tm_sec   = sec + secOffset;  // seconds after the minute [0,59]
    tmstruct.tm_isdst = -1;     // let "mktime" determine DST setting

    const time_t bad_time_t = (time_t) -1;
    // convert tm struct to time_t, if values are given in UTC then
    // no standard routine exists and we'll have to use our homegrown routine,
    // if values are given in local time then use "mktime"
    // which also normalizes the tm struct as a byproduct
    time_t utc_sec = (isUTC) ? MktimeFromUTC(&tmstruct) : mktime(&tmstruct);
 
    //   TimeStamp::Dump_tm_struct(tmstruct);

    if (utc_sec == bad_time_t)
        std::cerr << "TimeStamp::Set mktime returned -1" << std::endl;

    mSec  = utc_sec;
    mNanoSec = nsec;

    NormalizeNanoSec();
}
void TimeStamp::Set ( int  date,
int  time,
int  nsec,
bool  isUTC,
int  secOffset 
) [private]

Definition at line 423 of file TimeStamp.cc.

{
    // Set date/time from integers of the form [yy]YYMMDD and HHMMSS,
    // assume UTC (UTC) components:
    //
    //  MM: 01=January .. 12=December
    //  DD: 01 .. 31
    //
    //  HH: 00=midnight .. 23
    //  MM: 00 .. 59
    //  SS: 00 .. 69
    //
    // Date must be in format 980418 or 19980418
    //                       1001127 or 20001127  (i.e. year 100 = 2000).
    // Time must be in format 224512 (second precision).
    // Date must be >= 700101.

    int year  = date/10000;
    int month = (date-year*10000)/100;
    int day   = date%100;

    // protect against odd attempts at time offsets
    const int oneday = 240000;
    while (time < 0) {
        time += oneday;
        day  -= 1;
    }
    while (time > oneday) {
        time -= oneday;
        day  += 1;
    }
    int hour  = time/10000;
    int min   = (time-hour*10000)/100;
    int sec   = time%100;

    Set(year, month, day, hour, min, sec, nsec, isUTC, secOffset);

}
void TimeStamp::NormalizeNanoSec ( ) [private]

Definition at line 464 of file TimeStamp.cc.

{
    // Ensure that the mNanoSec field is in range [0,99999999].

    // deal with negative values
    while (mNanoSec < 0) {
        mNanoSec += kNsPerSec;
        mSec -= 1;
    }
    // deal with values inf mNanoSec greater than one sec
    while (mNanoSec >= kNsPerSec) {
        mNanoSec -= kNsPerSec;
        mSec += 1;
    }
}

Friends And Related Function Documentation

bool operator== ( const TimeStamp lhs,
const TimeStamp rhs 
) [friend]

Definition at line 289 of file TimeStamp.h.

{ return lhs.mSec  == rhs.mSec && 
        lhs.mNanoSec == rhs.mNanoSec; }
bool operator!= ( const TimeStamp lhs,
const TimeStamp rhs 
) [friend]

Definition at line 293 of file TimeStamp.h.

{ return lhs.mSec  != rhs.mSec ||
        lhs.mNanoSec != rhs.mNanoSec; }
bool operator< ( const TimeStamp lhs,
const TimeStamp rhs 
) [friend]

Definition at line 297 of file TimeStamp.h.

{ return lhs.mSec  < rhs.mSec ||
        ( lhs.mSec  == rhs.mSec &&
          lhs.mNanoSec <  rhs.mNanoSec   ); }
bool operator<= ( const TimeStamp lhs,
const TimeStamp rhs 
) [friend]

Definition at line 302 of file TimeStamp.h.

{ return lhs.mSec  < rhs.mSec ||
        ( lhs.mSec  == rhs.mSec &&
          lhs.mNanoSec <= rhs.mNanoSec   ); }
bool operator> ( const TimeStamp lhs,
const TimeStamp rhs 
) [friend]

Definition at line 307 of file TimeStamp.h.

{ return lhs.mSec  > rhs.mSec ||
        ( lhs.mSec  == rhs.mSec &&
          lhs.mNanoSec >  rhs.mNanoSec   ); }
bool operator>= ( const TimeStamp lhs,
const TimeStamp rhs 
) [friend]

Definition at line 312 of file TimeStamp.h.

{ return lhs.mSec  > rhs.mSec ||
        ( lhs.mSec  == rhs.mSec &&
          lhs.mNanoSec >= rhs.mNanoSec   ); }
TimeStamp operator- ( const TimeStamp lhs,
const TimeStamp rhs 
) [friend]

Definition at line 317 of file TimeStamp.h.

{
    return TimeStamp(lhs.GetSec()     - rhs.GetSec(),
                     lhs.GetNanoSec() - rhs.GetNanoSec());
}

Member Data Documentation

int TimeStamp::mSec [private]

Definition at line 279 of file TimeStamp.h.

int TimeStamp::mNanoSec [private]

Definition at line 280 of file TimeStamp.h.


The documentation for this class was generated from the following files:
| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

Generated on Fri May 16 2014 09:56:40 for Context by doxygen 1.7.4