Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

facilities::Timestamp Class Reference

#include <Timestamp.h>

Inheritance diagram for facilities::Timestamp:

CalibData::CalibTime CalibData::CalibTime List of all members.

Public Member Functions

long int getClibTime () const
long int getClibTime () const
double getJulian () const
 Return julian date.
double getJulian () const
 Return julian date.
double getNano () const
double getNano () const
std::string getString () const
 Return string representation of time, not including nanoseconds;.
std::string getString () const
 Return string representation of time, not including nanoseconds;.
bool operator!= (const Timestamp &other) 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
bool operator<= (const Timestamp &other) const
Timestampoperator= (const Timestamp &other)
Timestampoperator= (const Timestamp &other)
bool operator== (const Timestamp &other) 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
bool operator>= (const Timestamp &other) const
 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.
 Timestamp (const std::string &str, int tzOffset=0)
 Timestamp (double julian)
 Constructor for Julian date. Must be GMT.
 Timestamp (long int seconds, int nano=0, int tzOffset=0)
 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.
 Timestamp (const std::string &str, int tzOffset=0)
 Timestamp (double julian)
 Constructor for Julian date. Must be GMT.
 Timestamp (long int seconds, int nano=0, int tzOffset=0)
 Timestamp ()

Protected Attributes

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

Static Private Member Functions

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

Static Private Attributes

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

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


Constructor & Destructor Documentation

facilities::Timestamp::Timestamp  ) 
 

Default constructor builds object representing current time, expressed in GMT

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)

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.

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

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.

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   }

facilities::Timestamp::Timestamp  ) 
 

Default constructor builds object representing current time, expressed in GMT

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)

facilities::Timestamp::Timestamp double  julian  ) 
 

Constructor for Julian date. Must be GMT.

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

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.


Member Function Documentation

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

00088 {return m_time;}

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

00088 {return m_time;}

double facilities::Timestamp::getJulian  )  const
 

Return julian date.

double facilities::Timestamp::getJulian  )  const
 

Return julian date.

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

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

00087 {return m_nano;}

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

00087 {return m_nano;}

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

Return string representation of time, not including nanoseconds;.

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

Return string representation of time, not including nanoseconds;.

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

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

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

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

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

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

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]
 

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]
 

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

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

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

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

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

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

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]
 

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

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

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

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

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

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

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

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

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

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

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.

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

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

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.

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

Assemble string rep. from count of seconds.

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]
 

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

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

int facilities::Timestamp::m_nano [protected]
 

Save fractional seconds separately (associated with m_time).

time_t facilities::Timestamp::m_time [protected]
 

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

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

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

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


The documentation for this class was generated from the following files:
Generated on Wed Feb 2 19:21:13 2011 for BOSS6.5.5 by  doxygen 1.3.9.1