BOSS 7.0.9
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 06:11:40 maqm Exp $
2
3#include <ctime>
4#include <cstdlib>
5#include <cstdio>
6#include <cmath>
8
9namespace facilities {
10 const double Timestamp::julian1970 = 2440587.5;
11 const int Timestamp::secPerDay = (24*60*60);
12 const double Timestamp::inverseNano = 1000 * 1000 * 1000;
13 const int Timestamp::inverseNanoInt = 1000 * 1000 * 1000;
14 const long int Timestamp::maxInt = 0x7fffffff;
15
16 Timestamp::TZOffset Timestamp::s_tz;
17
18 // return current time (resolution of 1 second)
19 Timestamp::Timestamp() : m_nano(0) {
20 m_time = time(0);
21 }
22
23 // time specified as seconds since 1970 in the timezone corresponding
24 // to tzOffset (measured in seconds) west of GMT. So, for example,
25 // a local time PST should have tzOffset = 28800; PDT would be 25200;
26 // EST is 18000 and so forth. This offset can be read from the
27 // system variable __timezone
28 Timestamp::Timestamp(long int seconds, int nano, int tzOffset)
29 : m_time((time_t) seconds), m_nano(nano)
30 {
31 if ((nano >= inverseNanoInt) || (nano < 0) || (seconds < 0))
32 throw BadTimeInput("facilities::Timestamp bad nano argument");
33 seconds += tzOffset;
34 }
35
36 // time specified as Julian date
37 Timestamp::Timestamp(double julian) {
38 double secs;
39 secs = (julian - julian1970) * secPerDay;
40
41 if ((fabs(secs) > maxInt) || (secs < 0) )
42 throw BadTimeInput("Julian time not in range [1970, 2037]");
43
44 m_time = (long int) secs;
45
46 // In case time is negative, truncation will go the "wrong way".
47 // Want m_time always <= secs
48 if (m_time > secs) m_time--;
49 m_nano = (int) ((secs - m_time)/inverseNano);
50 }
51
52 // Time specified as string
53 Timestamp::Timestamp(const std::string& str, int tzOffset) : m_nano(0) {
54 m_time = toBinary(str);
55 m_time += tzOffset;
56 }
57
58 // Time specified with independent fields
59 Timestamp::Timestamp(int year, int month,
60 int day, int hour,
61 int minute, int second,
62 int nano) : m_nano(nano) {
63 struct tm fields;
64
65 // check input
66 // for now don't bother checking whether, e.g., someone
67 // specified April 31
68 if ((month < 1 ) || (month > 12) || (day < 1) || (day > 31) ||
69 (hour < 0) || (hour > 23) ||
70 (minute < 0) || (minute > 59) ||
71 (second < 0 ) || (second >= 60) ||
72 (year < 1970) || (year > 2037) ||
73 (nano < 0 ) || (nano >= inverseNanoInt) )
74 throw BadTimeInput("facilities::Timestamp Bad subfield");
75
76 fields.tm_year = year - 1900;
77 fields.tm_mon = month - 1;
78 fields.tm_mday = day;
79 fields.tm_hour = hour;
80 fields.tm_min = minute;
81 fields.tm_sec = (long int) second;
82 fields.tm_wday = -1;
83 fields.tm_yday = -1;
84
85 // let system figure out whether daylight savings time is in effect
86 fields.tm_isdst = 0;
87
88 // m_time = timegm(&fields);
89 m_time = mktime(&fields) - Timestamp::s_tz.m_tzseconds;
90 }
91
92 std::string Timestamp::getString() const {
93 std::string str;
94
95 toString(m_time, str);
96 return str;
97 }
98
99 double Timestamp::getJulian() const {
100 double julian = (m_time + m_nano/inverseNano)/secPerDay;
101 julian += julian1970;
102 return julian;
103 }
104
105
106 time_t Timestamp::toBinary(const std::string& strTime) {
107 // Break out fields
108 struct tm fields;
109
110 unsigned int pos;
111 unsigned int oldPos = 0;
112
113 // Three delimiter characters may occur in ascii time representation.
114 // First is hyphen.
115 char delim = '-';
116
117 pos = strTime.find(delim, oldPos);
118
119 // Must have two occurrences of hyphen delimiter
120 if (pos >= strTime.size()) return 0;
121
122 fields.tm_year = atoi((strTime.substr(oldPos, pos)).c_str()) - 1900;
123 if ((fields.tm_year < 70) || (fields.tm_year > 137))
124 throw BadTimeInput("facilities::Timestamp bad year");
125 oldPos = pos + 1;
126 pos = strTime.find(delim, oldPos);
127
128 // Should have at least two occurrences of delim
129 if (pos >= strTime.size())
130 throw BadTimeInput("Bad string format for facilities::Timestamp");
131
132 fields.tm_mon = atoi((strTime.substr(oldPos, pos)).c_str()) - 1;
133 if ((fields.tm_mon < 0) || (fields.tm_mon > 11))
134 throw BadTimeInput("facilities::Timestamp bad month");
135
136 // day
137 oldPos = pos + 1;
138
139 // A space separates time from date (if time is present at all)
140 delim = ' ';
141 pos = strTime.find(delim, oldPos);
142
143 fields.tm_mday = atoi((strTime.substr(oldPos, pos)).c_str());
144
145 if ((fields.tm_mday < 1) || (fields.tm_mday > 31))
146 throw BadTimeInput("facilities::Timestamp bad day of month");
147
148 // Remaining fields in string representation default to 0.
149 fields.tm_hour = fields.tm_min = fields.tm_sec = 0;
150
151 if (pos < strTime.size() ) { // more fields to process
152 delim = ':';
153 oldPos = pos + 1;
154
155 pos = strTime.find(delim, oldPos);
156
157 fields.tm_hour = atoi((strTime.substr(oldPos, pos)).c_str());
158 if ((fields.tm_hour > 23) || (fields.tm_hour < 0))
159 throw BadTimeInput("facilities::Timestamp bad hour");
160
161
162 if (pos < strTime.size() ) {
163 oldPos = pos + 1;
164 pos = strTime.find(delim, oldPos);
165 fields.tm_min = atoi((strTime.substr(oldPos, pos)).c_str());
166 if ((fields.tm_min > 59) || (fields.tm_hour < 0))
167 throw BadTimeInput("facilities::Timestamp bad minutes");
168
169 if (pos < strTime.size() ) {
170 oldPos = pos + 1;
171 pos = strTime.find(delim, oldPos);
172 fields.tm_sec = atoi((strTime.substr(oldPos, pos)).c_str());
173 if ((fields.tm_sec > 59) || (fields.tm_hour < 0))
174 throw BadTimeInput("facilities::Timestamp bad seconds");
175 }
176 }
177 }
178
179 fields.tm_wday = -1;
180 fields.tm_yday = -1;
181 fields.tm_isdst = 0;
182 return mktime(&fields) - Timestamp::s_tz.m_tzseconds;
183 }
184
185 void Timestamp::toString(time_t bin, std::string& strTime) {
186 struct tm * fields = gmtime(&bin);
187
188 strTime.resize(0);
189
190 char buf[20];
191 char* bufPtr = &buf[0];
192 sprintf(buf, "%i", fields->tm_year + 1900);
193 strTime += bufPtr; strTime += "-";
194 sprintf(buf, "%02i", fields->tm_mon +1);
195 strTime += bufPtr; strTime += "-";
196 sprintf(buf, "%02i", fields->tm_mday);
197 strTime += bufPtr; strTime += " ";
198 sprintf(buf, "%02i", fields->tm_hour);
199 strTime += bufPtr; strTime += ":";
200 sprintf(buf, "%02i", fields->tm_min);
201 strTime += bufPtr; strTime += ":";
202 sprintf(buf, "%02i", fields->tm_sec);
203 strTime += bufPtr;
204 }
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}
223
224
225
226
227
228
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
*******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:92
double getJulian() const
Return julian date.
Definition: Timestamp.cxx:99
int m_nano
Save fractional seconds separately (associated with m_time)
Definition: Timestamp.h:156
time_t m_time
internal binary rep of time; count seconds from Jan 1, 1970
Definition: Timestamp.h:153