#include <Timestamp.h>
Inheritance diagram for facilities::Timestamp:
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 |
Timestamp & | operator= (const Timestamp &other) |
Timestamp & | operator= (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) |
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
|
Default constructor builds object representing current time, expressed in GMT
|
|
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 }
|
|
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 }
|
|
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
|
|
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 }
|
|
Default constructor builds object representing current time, expressed in GMT |
|
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) |
|
Constructor for Julian date. Must be GMT.
|
|
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 |
|
Construct absolute time with specified fields.
|
|
00088 {return m_time;}
|
|
00088 {return m_time;}
|
|
Return julian date.
|
|
Return julian date.
00099 { 00100 double julian = (m_time + m_nano/inverseNano)/secPerDay; 00101 julian += julian1970; 00102 return julian; 00103 }
|
|
00087 {return m_nano;}
|
|
00087 {return m_nano;}
|
|
Return string representation of time, not including nanoseconds;.
|
|
Return string representation of time, not including nanoseconds;.
00092 { 00093 std::string str; 00094 00095 toString(m_time, str); 00096 return str; 00097 }
|
|
00118 { 00119 return (!(*this == other)); 00120 }
|
|
00118 { 00119 return (!(*this == other)); 00120 }
|
|
00090 { 00091 return ((m_time < other.m_time) || 00092 ((m_time == other.m_time) && (m_nano < other.m_nano))); 00093 }
|
|
00090 { 00091 return ((m_time < other.m_time) || 00092 ((m_time == other.m_time) && (m_nano < other.m_nano))); 00093 }
|
|
00100 { 00101 return ( !(other < (*this)) ); 00102 }
|
|
00100 { 00101 return ( !(other < (*this)) ); 00102 }
|
|
|
|
|
|
|
|
|
|
00095 { 00096 return (other < (*this)); 00097 00098 }
|
|
00095 { 00096 return (other < (*this)); 00097 00098 }
|
|
00104 {
00105 return ( !( (*this) < other ) );
00106 }
|
|
00104 {
00105 return ( !( (*this) < other ) );
00106 }
|
|
Return "standard" binary time: count in seconds since 1 jan 1970.
|
|
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 }
|
|
Assemble string rep. from count of seconds.
|
|
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 }
|
|
|
|
|
|
|
|
Save fractional seconds separately (associated with m_time).
|
|
internal binary rep of time; count seconds from Jan 1, 1970
|
|
|
|
|
|
|