BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
Timestamp.cxx
Go to the documentation of this file.
1// $Header: /bes/bes/BossCvs/Calibration/facilities/src/Timestamp.cxx,v 1.1.1.1 2005/10/17
2// 06:11:40 maqm Exp $
3
4#include "facilities/Timestamp.h"
5#include <cmath>
6#include <cstdio>
7#include <cstdlib>
8#include <ctime>
9
10namespace facilities {
11 const double Timestamp::julian1970 = 2440587.5;
12 const int Timestamp::secPerDay = ( 24 * 60 * 60 );
13 const double Timestamp::inverseNano = 1000 * 1000 * 1000;
14 const int Timestamp::inverseNanoInt = 1000 * 1000 * 1000;
15 const long int Timestamp::maxInt = 0x7fffffff;
16
17 Timestamp::TZOffset Timestamp::s_tz;
18
19 // return current time (resolution of 1 second)
21
22 // time specified as seconds since 1970 in the timezone corresponding
23 // to tzOffset (measured in seconds) west of GMT. So, for example,
24 // a local time PST should have tzOffset = 28800; PDT would be 25200;
25 // EST is 18000 and so forth. This offset can be read from the
26 // system variable __timezone
27 Timestamp::Timestamp( long int seconds, int nano, int tzOffset )
28 : m_time( (time_t)seconds ), m_nano( nano ) {
29 if ( ( nano >= inverseNanoInt ) || ( nano < 0 ) || ( seconds < 0 ) )
30 throw BadTimeInput( "facilities::Timestamp bad nano argument" );
31 seconds += tzOffset;
32 }
33
34 // time specified as Julian date
35 Timestamp::Timestamp( double julian ) {
36 double secs;
37 secs = ( julian - julian1970 ) * secPerDay;
38
39 if ( ( fabs( secs ) > maxInt ) || ( secs < 0 ) )
40 throw BadTimeInput( "Julian time not in range [1970, 2037]" );
41
42 m_time = (long int)secs;
43
44 // In case time is negative, truncation will go the "wrong way".
45 // Want m_time always <= secs
46 if ( m_time > secs ) m_time--;
47 m_nano = (int)( ( secs - m_time ) / inverseNano );
48 }
49
50 // Time specified as string
51 Timestamp::Timestamp( const std::string& str, int tzOffset ) : m_nano( 0 ) {
52 m_time = toBinary( str );
53 m_time += tzOffset;
54 }
55
56 // Time specified with independent fields
57 Timestamp::Timestamp( int year, int month, int day, int hour, int minute, int second,
58 int nano )
59 : m_nano( nano ) {
60 struct tm fields;
61
62 // check input
63 // for now don't bother checking whether, e.g., someone
64 // specified April 31
65 if ( ( month < 1 ) || ( month > 12 ) || ( day < 1 ) || ( day > 31 ) || ( hour < 0 ) ||
66 ( hour > 23 ) || ( minute < 0 ) || ( minute > 59 ) || ( second < 0 ) ||
67 ( second >= 60 ) || ( year < 1970 ) || ( year > 2037 ) || ( nano < 0 ) ||
68 ( nano >= inverseNanoInt ) )
69 throw BadTimeInput( "facilities::Timestamp Bad subfield" );
70
71 fields.tm_year = year - 1900;
72 fields.tm_mon = month - 1;
73 fields.tm_mday = day;
74 fields.tm_hour = hour;
75 fields.tm_min = minute;
76 fields.tm_sec = (long int)second;
77 fields.tm_wday = -1;
78 fields.tm_yday = -1;
79
80 // let system figure out whether daylight savings time is in effect
81 fields.tm_isdst = 0;
82
83 // m_time = timegm(&fields);
84 m_time = mktime( &fields ) - Timestamp::s_tz.m_tzseconds;
85 }
86
87 std::string Timestamp::getString() const {
88 std::string str;
89
90 toString( m_time, str );
91 return str;
92 }
93
94 double Timestamp::getJulian() const {
95 double julian = ( m_time + m_nano / inverseNano ) / secPerDay;
96 julian += julian1970;
97 return julian;
98 }
99
100 time_t Timestamp::toBinary( const std::string& strTime ) {
101 // Break out fields
102 struct tm fields;
103
104 unsigned int pos;
105 unsigned int oldPos = 0;
106
107 // Three delimiter characters may occur in ascii time representation.
108 // First is hyphen.
109 char delim = '-';
110
111 pos = strTime.find( delim, oldPos );
112
113 // Must have two occurrences of hyphen delimiter
114 if ( pos >= strTime.size() ) return 0;
115
116 fields.tm_year = atoi( ( strTime.substr( oldPos, pos ) ).c_str() ) - 1900;
117 if ( ( fields.tm_year < 70 ) || ( fields.tm_year > 137 ) )
118 throw BadTimeInput( "facilities::Timestamp bad year" );
119 oldPos = pos + 1;
120 pos = strTime.find( delim, oldPos );
121
122 // Should have at least two occurrences of delim
123 if ( pos >= strTime.size() )
124 throw BadTimeInput( "Bad string format for facilities::Timestamp" );
125
126 fields.tm_mon = atoi( ( strTime.substr( oldPos, pos ) ).c_str() ) - 1;
127 if ( ( fields.tm_mon < 0 ) || ( fields.tm_mon > 11 ) )
128 throw BadTimeInput( "facilities::Timestamp bad month" );
129
130 // day
131 oldPos = pos + 1;
132
133 // A space separates time from date (if time is present at all)
134 delim = ' ';
135 pos = strTime.find( delim, oldPos );
136
137 fields.tm_mday = atoi( ( strTime.substr( oldPos, pos ) ).c_str() );
138
139 if ( ( fields.tm_mday < 1 ) || ( fields.tm_mday > 31 ) )
140 throw BadTimeInput( "facilities::Timestamp bad day of month" );
141
142 // Remaining fields in string representation default to 0.
143 fields.tm_hour = fields.tm_min = fields.tm_sec = 0;
144
145 if ( pos < strTime.size() )
146 { // more fields to process
147 delim = ':';
148 oldPos = pos + 1;
149
150 pos = strTime.find( delim, oldPos );
151
152 fields.tm_hour = atoi( ( strTime.substr( oldPos, pos ) ).c_str() );
153 if ( ( fields.tm_hour > 23 ) || ( fields.tm_hour < 0 ) )
154 throw BadTimeInput( "facilities::Timestamp bad hour" );
155
156 if ( pos < strTime.size() )
157 {
158 oldPos = pos + 1;
159 pos = strTime.find( delim, oldPos );
160 fields.tm_min = atoi( ( strTime.substr( oldPos, pos ) ).c_str() );
161 if ( ( fields.tm_min > 59 ) || ( fields.tm_hour < 0 ) )
162 throw BadTimeInput( "facilities::Timestamp bad minutes" );
163
164 if ( pos < strTime.size() )
165 {
166 oldPos = pos + 1;
167 pos = strTime.find( delim, oldPos );
168 fields.tm_sec = atoi( ( strTime.substr( oldPos, pos ) ).c_str() );
169 if ( ( fields.tm_sec > 59 ) || ( fields.tm_hour < 0 ) )
170 throw BadTimeInput( "facilities::Timestamp bad seconds" );
171 }
172 }
173 }
174
175 fields.tm_wday = -1;
176 fields.tm_yday = -1;
177 fields.tm_isdst = 0;
178 return mktime( &fields ) - Timestamp::s_tz.m_tzseconds;
179 }
180
181 void Timestamp::toString( time_t bin, std::string& strTime ) {
182 struct tm* fields = gmtime( &bin );
183
184 strTime.resize( 0 );
185
186 char buf[20];
187 char* bufPtr = &buf[0];
188 sprintf( buf, "%i", fields->tm_year + 1900 );
189 strTime += bufPtr;
190 strTime += "-";
191 sprintf( buf, "%02i", fields->tm_mon + 1 );
192 strTime += bufPtr;
193 strTime += "-";
194 sprintf( buf, "%02i", fields->tm_mday );
195 strTime += bufPtr;
196 strTime += " ";
197 sprintf( buf, "%02i", fields->tm_hour );
198 strTime += bufPtr;
199 strTime += ":";
200 sprintf( buf, "%02i", fields->tm_min );
201 strTime += bufPtr;
202 strTime += ":";
203 sprintf( buf, "%02i", fields->tm_sec );
204 strTime += bufPtr;
205 }
206
207 Timestamp::TZOffset::TZOffset() {
208 struct tm fields;
209
210 // Set it up for Jan 1, 1970 at 12:00
211 fields.tm_year = 70;
212 fields.tm_mon = 0;
213 fields.tm_mday = 1;
214 fields.tm_hour = 12;
215 fields.tm_min = 0;
216 fields.tm_sec = 0;
217 fields.tm_isdst = 0;
218
219 m_tzseconds = mktime( &fields ) - 12 * 60 * 60;
220 m_isDst = fields.tm_isdst;
221 }
222} // namespace facilities
sprintf(cut, "kal_costheta0_em>-0.93&&kal_costheta0_em<0.93&&kal_pxy0_em>=0.05+%d*0.1&&kal_" "pxy0_em<0.15+%d*0.1&&NGch>=2", j, j)
Double_t time
string toString(const T &t)
*******INTEGER m_nBinMax INTEGER m_NdiMax !No of bins in histogram for cell exploration division $ !Last vertex $ !Last active cell $ !Last cell in buffer $ !No of sampling when dividing cell $ !No of function total $ !Flag for random ceel for $ !Flag for type of for WtMax $ !Flag which decides whether vertices are included in the sampling $ entire domain is hyp !Maximum effective eevents per bin
Definition FoamA.h:85
std::string getString() const
Return string representation of time, not including nanoseconds;.
Definition Timestamp.cxx:87
double getJulian() const
Return julian date.
Definition Timestamp.cxx:94
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