ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PHTimeStamp.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file PHTimeStamp.cc
1 //-----------------------------------------------------------------------------
2 //
3 // The PHOOL's Software
4 // Copyright (C) PHENIX collaboration, 1999
5 //
6 // Implementation of class PHTimeStamp
7 //
8 // Author: Matthias Messer
9 //-----------------------------------------------------------------------------
10 #include "PHTimeStamp.h"
11 
12 #include <climits>
13 #include <cstdlib>
14 #include <cstring>
15 #include <iostream>
16 
17 using namespace std;
18 
19 const unsigned long long PHTimeStamp::PHFarFuture = ULLONG_MAX;
20 
21 #ifdef WIN32
22 const phtime_t ticOffset = 35067168000000000UL;
23 #else
24 const phtime_t ticOffset = 35067168000000000ULL;
25 #endif
26 
27 const phtime_t ticFactor = 10000000;
28 
30 {
31  setTics(time(0));
32 
33  setenv("TZ", "EST5EDT", 1);
34 }
35 
36 PHTimeStamp::PHTimeStamp(const int year, const int month, const int day, const int hour, const int minute, const int second, const int fraction)
37 {
38  set(year, month, day, hour, minute, second, fraction);
39  setenv("TZ", "EST5EDT", 1);
40 }
41 
43 {
44  setTics(t);
45  setenv("TZ", "EST5EDT", 1);
46 }
47 
48 void PHTimeStamp::set(const int year, const int month, const int day,
49  const int hour, const int minute,
50  const int second, const int fraction)
51 {
52  if (year < 1900)
53  {
54  setTics(0);
55  return;
56  }
57  tm newTime;
58  newTime.tm_year = year - 1900;
59  newTime.tm_mon = month - 1;
60  newTime.tm_mday = day;
61  newTime.tm_hour = hour;
62  newTime.tm_min = minute;
63  newTime.tm_sec = second;
64 
65  // This tells mktime that it's not known whether it's daylight
66  // savings time or not.
67  newTime.tm_isdst = -1;
68 
69  setTics(mktime(&newTime));
70  binaryTime += fraction;
71 }
72 
73 void PHTimeStamp::set(const char *timeString)
74 {
75 #ifndef WIN32
76  tm newTime;
77  strptime(timeString, "%A %h %d %H:%M:%S %Y", &newTime);
78  setTics(mktime(&newTime));
79 #endif
80 }
81 
83 {
84  setTics(time(0));
85 }
86 
87 time_t PHTimeStamp::getTics() const
88 {
89  return binaryTimeToTics(binaryTime);
90 }
91 
92 void PHTimeStamp::setTics(const time_t tics)
93 {
94  binaryTime = ticsToBinaryTime(tics);
95 }
96 
98 {
99  binaryTime = t;
100 }
101 
103 {
104  return tics * ticFactor + ticOffset;
105 }
106 
108 {
109  return (bt - ticOffset) / ticFactor;
110 }
111 
113 {
114  return (binaryTime > t1.getBinaryTime() && binaryTime < t2.getBinaryTime());
115 }
116 
118 {
119  cout << *this;
120 }
121 
122 //
123 // Operators
124 //
126 {
127  return binaryTime == t.getBinaryTime();
128 }
129 
131 {
132  return binaryTime != t.getBinaryTime();
133 }
134 
136 {
137  return binaryTime > t.getBinaryTime();
138 }
139 
141 {
142  return binaryTime < t.getBinaryTime();
143 }
144 
146 {
147  return binaryTime >= t.getBinaryTime();
148 }
149 
151 {
152  return binaryTime <= t.getBinaryTime();
153 }
154 
156 {
157  binaryTime += t * ticFactor;
158  return *this;
159 }
160 
162 {
163  binaryTime -= t * ticFactor;
164  return *this;
165 }
166 
167 void PHTimeStamp::print() const
168 {
169  cout << *this << endl;
170 }
172 {
173  // this one gives, for naming purposes, the time string
174  // without blanks
175 
176  time_t tics = getTics();
177  char timeString[25];
178  timeString[24] = '\0';
179  strncpy(timeString, ctime(&tics), 24);
180  char *line = new char[25];
181 
182  char *u = strtok(timeString, " ");
183 
184  if (u) strcpy(line, u);
185 
186  while ((u = strtok(0, " ")))
187  {
188  strcat(line, "_");
189  strcat(line, u);
190  }
191  return line;
192 }
193 
194 //
195 // Non member operators and functions
196 //
197 
199 {
200  PHTimeStamp newTime = t1;
201  newTime += t2;
202  return newTime;
203 }
204 
206 {
207  PHTimeStamp newTime = t1;
208  newTime -= t2;
209  return newTime;
210 }
211 
212 time_t operator-(const PHTimeStamp &t1, const PHTimeStamp &t2)
213 {
214  return t1.getTics() - t2.getTics();
215 }
216 
217 ostream &operator<<(ostream &s, const PHTimeStamp &t)
218 {
219  time_t tics = t.getTics();
220  char timeString[25];
221  timeString[24] = '\0';
222  strncpy(timeString, ctime(&tics), 24);
223  return s << timeString;
224 }
225 
226 istream &operator>>(istream &s, PHTimeStamp &t)
227 {
228  char timeString[25];
229  s.getline(timeString, 25);
230  t.set(timeString);
231  return s;
232 }