facilities::Timestamp Class Reference

#include <Timestamp.h>

List of all members.

Public Member Functions

 Timestamp ()
 Timestamp (long int seconds, int nano=0, int tzOffset=0)
 Timestamp (double julian)
 Constructor for Julian date. Must be GMT.
 Timestamp (const std::string &str, int tzOffset=0)
 Timestamp (int year, int month, int day, int hour=0, int minute=0, int second=0, int nano=0)
 Construct absolute time with specified fields.
std::string getString () const
 Return string representation of time, not including nanoseconds;.
double getJulian () const
 Return julian date.
double getNano () const
long int getClibTime () const
bool operator< (const Timestamp &other) const
bool operator> (const Timestamp &other) const
bool operator<= (const Timestamp &other) const
bool operator>= (const Timestamp &other) const
Timestampoperator= (const Timestamp &other)
bool operator== (const Timestamp &other) const
bool operator!= (const Timestamp &other) const

Protected Attributes

time_t m_time
 internal binary rep of time; count seconds from Jan 1, 1970
int m_nano
 Save fractional seconds separately (associated with m_time).

Static Private Member Functions

static time_t toBinary (const std::string &strTime)
 Return "standard" binary time: count in seconds since 1 jan 1970.
static void toString (time_t bin, std::string &strTime)
 Assemble string rep. from count of seconds.

Static Private Attributes

static const double julian1970 = 2440587.5
static const int secPerDay = (24*60*60)
static const double inverseNano = 1000 * 1000 * 1000
static const int inverseNanoInt = 1000 * 1000 * 1000
static const long int maxInt = 0x7fffffff
static TZOffset s_tz

Classes

class  TZOffset


Detailed Description

Timestamp class, valid for dates from 1970 through 2037

Supports comparisons

Input to constructors may be Julian date seconds since start of 1970, Jan. 1 with optional nanosecond field individual fields (year, month, etc.) string format

yyyy-mm-dd hh:mm:ss 1969 < yyyy < 2038 where 0 < mm < 13 0 < dd < 32 -1 < hh < 24 -1 < mm < 60 -1 < ss < 60

o only the first three fields are required. Omitted trailing fields will be interpreted as equal to 0. o by default : will be used to delimit fields, but user may specify an alternative in most circumstances o leading zeros are optional

Definition at line 49 of file Timestamp.h.


Constructor & Destructor Documentation

facilities::Timestamp::Timestamp (  ) 

Default constructor builds object representing current time, expressed in GMT

Definition at line 19 of file Timestamp.cxx.

References m_time.

00019                        : m_nano(0) {
00020     m_time = time(0);
00021   }

facilities::Timestamp::Timestamp ( long int  seconds,
int  nano = 0,
int  tzOffset = 0 
)

Count seconds from the creation-according-to-unix, start of 1970 Optional third argument is offset in seconds from GMT (e.g., PST is +28800)

Definition at line 28 of file Timestamp.cxx.

References inverseNanoInt.

00029     : m_time((time_t) seconds), m_nano(nano)
00030   {
00031     if  ((nano >= inverseNanoInt)  || (nano < 0) || (seconds < 0))
00032       throw BadTimeInput("facilities::Timestamp bad nano argument");
00033     seconds += tzOffset;
00034   }

facilities::Timestamp::Timestamp ( double  julian  ) 

Constructor for Julian date. Must be GMT.

Definition at line 37 of file Timestamp.cxx.

References inverseNano, julian1970, m_nano, m_time, maxInt, and secPerDay.

00037                                     {
00038     double secs;
00039     secs = (julian - julian1970) * secPerDay;
00040 
00041     if ((fabs(secs) > maxInt) || (secs < 0) )
00042       throw BadTimeInput("Julian time not in range [1970, 2037]");
00043 
00044     m_time = (long int) secs;
00045 
00046     // In case time is negative, truncation will go the "wrong way".
00047     // Want m_time always <= secs
00048     if (m_time > secs) m_time--;
00049     m_nano = (int) ((secs - m_time)/inverseNano);
00050   } 

facilities::Timestamp::Timestamp ( const std::string str,
int  tzOffset = 0 
)

Create a timestamp from an ascii string of standard form yyyy-mm-dd hh:mm:ss where only the first three fields are required.

If the string is invalid, object will represent unix creation time. If the string represents a time in a timezone other than GMT, tzOffset should represent time zone offset relative to GMT in seconds so if local time is, for example, PST, tzOffset should be 28800

Definition at line 53 of file Timestamp.cxx.

References m_time, and toBinary().

00053                                                          : m_nano(0) {
00054     m_time = toBinary(str);
00055     m_time += tzOffset;
00056   }

facilities::Timestamp::Timestamp ( int  year,
int  month,
int  day,
int  hour = 0,
int  minute = 0,
int  second = 0,
int  nano = 0 
)

Construct absolute time with specified fields.

Definition at line 59 of file Timestamp.cxx.

References inverseNanoInt, m_time, facilities::Timestamp::TZOffset::m_tzseconds, and s_tz.

00062                                  :  m_nano(nano) {
00063     struct tm fields;
00064 
00065     // check input   
00066     // for now don't bother checking whether, e.g., someone
00067     // specified April 31
00068     if ((month < 1 ) || (month > 12) || (day < 1) || (day > 31) ||
00069         (hour < 0) || (hour > 23) || 
00070         (minute < 0) || (minute > 59) || 
00071         (second < 0 ) || (second >= 60) ||
00072         (year < 1970) || (year > 2037) || 
00073         (nano < 0 ) || (nano >= inverseNanoInt) ) 
00074       throw BadTimeInput("facilities::Timestamp Bad subfield");
00075 
00076     fields.tm_year = year - 1900;
00077     fields.tm_mon = month - 1;
00078     fields.tm_mday = day;
00079     fields.tm_hour = hour;
00080     fields.tm_min = minute;
00081     fields.tm_sec = (long int) second;
00082     fields.tm_wday = -1;
00083     fields.tm_yday = -1;
00084     
00085     // let system figure out whether daylight savings time is in effect
00086     fields.tm_isdst = 0;
00087 
00088     //    m_time = timegm(&fields);
00089     m_time = mktime(&fields) - Timestamp::s_tz.m_tzseconds;
00090   }


Member Function Documentation

long int facilities::Timestamp::getClibTime (  )  const [inline]

Definition at line 88 of file Timestamp.h.

References m_time.

Referenced by Coverage::Coverage().

00088 {return m_time;}

double facilities::Timestamp::getJulian (  )  const

Return julian date.

Definition at line 99 of file Timestamp.cxx.

References inverseNano, julian1970, m_nano, m_time, and secPerDay.

00099                                      {
00100     double julian = (m_time +  m_nano/inverseNano)/secPerDay;
00101     julian += julian1970;
00102     return julian;
00103   }

double facilities::Timestamp::getNano (  )  const [inline]

Definition at line 87 of file Timestamp.h.

References m_nano.

00087 {return m_nano;}

std::string facilities::Timestamp::getString (  )  const

Return string representation of time, not including nanoseconds;.

Definition at line 92 of file Timestamp.cxx.

References m_time, deljobs::string, and toString().

Referenced by calibUtil::Metadata::adjustVend(), Coverage::checkType(), rdbModel::Table::fillProgramCols(), lookup(), main(), operator<<(), calibUtil::Metadata::registerCalib(), and soonest().

00092                                        {
00093     std::string str;
00094 
00095     toString(m_time, str);
00096     return str;
00097   }

bool facilities::Timestamp::operator!= ( const Timestamp other  )  const [inline]

Definition at line 118 of file Timestamp.h.

References EvtCyclic3::other().

00118                                                   {
00119       return (!(*this == other));
00120     }

bool facilities::Timestamp::operator< ( const Timestamp other  )  const [inline]

Definition at line 90 of file Timestamp.h.

References m_nano, m_time, and EvtCyclic3::other().

00090                                                  {
00091       return ((m_time < other.m_time) || 
00092               ((m_time == other.m_time) && (m_nano < other.m_nano)));
00093     }

bool facilities::Timestamp::operator<= ( const Timestamp other  )  const [inline]

Definition at line 100 of file Timestamp.h.

References EvtCyclic3::other().

00100                                                   {
00101       return ( !(other < (*this)) );
00102     }

Timestamp& facilities::Timestamp::operator= ( const Timestamp other  )  [inline]

Definition at line 109 of file Timestamp.h.

References m_nano, m_time, and EvtCyclic3::other().

00109                                                   {
00110       m_time = other.m_time; m_nano = other.m_nano; 
00111       return *this;
00112     }

bool facilities::Timestamp::operator== ( const Timestamp other  )  const [inline]

Definition at line 114 of file Timestamp.h.

References m_nano, m_time, and EvtCyclic3::other().

00114                                                  {
00115       return ((m_time == other.m_time) && (m_nano == other.m_nano));
00116     }

bool facilities::Timestamp::operator> ( const Timestamp other  )  const [inline]

Definition at line 95 of file Timestamp.h.

References EvtCyclic3::other().

00095                                                  {
00096       return (other < (*this));
00097         
00098     }

bool facilities::Timestamp::operator>= ( const Timestamp other  )  const [inline]

Definition at line 104 of file Timestamp.h.

References EvtCyclic3::other().

00104                                                   {
00105       return ( !( (*this) < other ) );
00106     }

time_t facilities::Timestamp::toBinary ( const std::string strTime  )  [static, private]

Return "standard" binary time: count in seconds since 1 jan 1970.

Definition at line 106 of file Timestamp.cxx.

References EvtCyclic3::c_str(), facilities::Timestamp::TZOffset::m_tzseconds, boss::pos, and s_tz.

Referenced by Timestamp().

00106                                                      {
00107     // Break out fields
00108     struct tm fields;
00109 
00110     unsigned int pos;
00111     unsigned int oldPos = 0;
00112 
00113     // Three delimiter characters may occur in ascii time representation.
00114     // First is hyphen.
00115     char  delim = '-';
00116 
00117     pos = strTime.find(delim, oldPos);
00118 
00119     // Must have two occurrences of hyphen delimiter
00120     if (pos >= strTime.size()) return 0;
00121 
00122     fields.tm_year = atoi((strTime.substr(oldPos, pos)).c_str()) - 1900;
00123     if ((fields.tm_year < 70) || (fields.tm_year > 137)) 
00124       throw BadTimeInput("facilities::Timestamp  bad year");
00125     oldPos = pos + 1;
00126     pos = strTime.find(delim, oldPos);
00127 
00128     // Should have at least two occurrences of delim
00129     if (pos >= strTime.size()) 
00130       throw BadTimeInput("Bad string format for facilities::Timestamp");
00131 
00132     fields.tm_mon = atoi((strTime.substr(oldPos, pos)).c_str()) - 1;
00133     if ((fields.tm_mon < 0) || (fields.tm_mon > 11)) 
00134       throw BadTimeInput("facilities::Timestamp bad month");
00135 
00136     // day
00137     oldPos = pos + 1;
00138 
00139     // A space separates time from date (if time is present at all)
00140     delim = ' ';
00141     pos = strTime.find(delim, oldPos);
00142 
00143     fields.tm_mday = atoi((strTime.substr(oldPos, pos)).c_str());
00144 
00145     if ((fields.tm_mday < 1) || (fields.tm_mday > 31))
00146     throw BadTimeInput("facilities::Timestamp bad day of month");
00147 
00148     // Remaining fields in string representation default to 0.
00149     fields.tm_hour = fields.tm_min = fields.tm_sec = 0;
00150 
00151     if (pos < strTime.size() ) {   // more fields to process
00152       delim = ':';
00153       oldPos = pos + 1;
00154 
00155       pos = strTime.find(delim, oldPos);
00156 
00157       fields.tm_hour = atoi((strTime.substr(oldPos, pos)).c_str());
00158       if ((fields.tm_hour > 23) || (fields.tm_hour < 0)) 
00159       throw BadTimeInput("facilities::Timestamp bad hour");
00160 
00161       
00162       if (pos < strTime.size() ) {
00163         oldPos = pos + 1;
00164         pos = strTime.find(delim, oldPos);
00165         fields.tm_min = atoi((strTime.substr(oldPos, pos)).c_str());
00166         if ((fields.tm_min > 59) || (fields.tm_hour < 0))
00167           throw BadTimeInput("facilities::Timestamp bad minutes");
00168 
00169         if (pos < strTime.size() ) {
00170           oldPos = pos + 1;
00171           pos = strTime.find(delim, oldPos);
00172           fields.tm_sec = atoi((strTime.substr(oldPos, pos)).c_str());
00173           if ((fields.tm_sec > 59) || (fields.tm_hour < 0)) 
00174             throw BadTimeInput("facilities::Timestamp bad seconds");
00175         }
00176       }
00177     }
00178 
00179     fields.tm_wday = -1;
00180     fields.tm_yday = -1;
00181     fields.tm_isdst = 0;
00182     return mktime(&fields)  - Timestamp::s_tz.m_tzseconds;
00183   }

void facilities::Timestamp::toString ( time_t  bin,
std::string strTime 
) [static, private]

Assemble string rep. from count of seconds.

Definition at line 185 of file Timestamp.cxx.

Referenced by getString().

00185                                                          {
00186     struct tm * fields = gmtime(&bin);
00187 
00188     strTime.resize(0);
00189     
00190     char buf[20];
00191     char* bufPtr = &buf[0];
00192     sprintf(buf, "%i", fields->tm_year + 1900);
00193     strTime += bufPtr; strTime +=  "-";
00194     sprintf(buf, "%02i", fields->tm_mon +1);
00195     strTime += bufPtr; strTime +=  "-";
00196     sprintf(buf, "%02i", fields->tm_mday);
00197     strTime += bufPtr; strTime +=  " ";
00198     sprintf(buf, "%02i", fields->tm_hour);
00199     strTime += bufPtr; strTime +=  ":";
00200     sprintf(buf, "%02i", fields->tm_min);
00201     strTime += bufPtr; strTime +=  ":";
00202     sprintf(buf, "%02i", fields->tm_sec);
00203     strTime += bufPtr;
00204   }


Member Data Documentation

const double facilities::Timestamp::inverseNano = 1000 * 1000 * 1000 [static, private]

Definition at line 129 of file Timestamp.h.

Referenced by getJulian(), and Timestamp().

const int facilities::Timestamp::inverseNanoInt = 1000 * 1000 * 1000 [static, private]

Definition at line 130 of file Timestamp.h.

Referenced by Timestamp().

const double facilities::Timestamp::julian1970 = 2440587.5 [static, private]

Definition at line 127 of file Timestamp.h.

Referenced by getJulian(), and Timestamp().

int facilities::Timestamp::m_nano [protected]

Save fractional seconds separately (associated with m_time).

Definition at line 156 of file Timestamp.h.

Referenced by getJulian(), getNano(), operator<(), operator=(), operator==(), and Timestamp().

time_t facilities::Timestamp::m_time [protected]

internal binary rep of time; count seconds from Jan 1, 1970

Definition at line 153 of file Timestamp.h.

Referenced by getClibTime(), getJulian(), getString(), operator<(), operator=(), operator==(), and Timestamp().

const long int facilities::Timestamp::maxInt = 0x7fffffff [static, private]

Definition at line 131 of file Timestamp.h.

Referenced by Timestamp().

Timestamp::TZOffset facilities::Timestamp::s_tz [static, private]

Definition at line 143 of file Timestamp.h.

Referenced by Timestamp(), and toBinary().

const int facilities::Timestamp::secPerDay = (24*60*60) [static, private]

Definition at line 128 of file Timestamp.h.

Referenced by getJulian(), and Timestamp().


Generated on Tue Nov 29 23:36:33 2016 for BOSS_7.0.2 by  doxygen 1.4.7