Analysis Software
Documentation for sPHENIX simulation 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 const unsigned long long PHTimeStamp::PHFarFuture = ULLONG_MAX;
18 
19 #ifdef WIN32
20 const phtime_t ticOffset = 35067168000000000UL;
21 #else
22 const phtime_t ticOffset = 35067168000000000ULL;
23 #endif
24 
25 const phtime_t ticFactor = 10000000;
26 
28 {
29  setTics(time(nullptr));
30 
31  setenv("TZ", "EST5EDT", 1);
32 }
33 
34 PHTimeStamp::PHTimeStamp(const int year, const int month, const int day, const int hour, const int minute, const int second, const int fraction)
35 {
36  set(year, month, day, hour, minute, second, fraction);
37  setenv("TZ", "EST5EDT", 1);
38 }
39 
41 {
42  setTics(t);
43  setenv("TZ", "EST5EDT", 1);
44 }
45 
46 void PHTimeStamp::set(const int year, const int month, const int day,
47  const int hour, const int minute,
48  const int second, const int fraction)
49 {
50  if (year < 1900)
51  {
52  setTics(0);
53  return;
54  }
55  tm newTime;
56  newTime.tm_year = year - 1900;
57  newTime.tm_mon = month - 1;
58  newTime.tm_mday = day;
59  newTime.tm_hour = hour;
60  newTime.tm_min = minute;
61  newTime.tm_sec = second;
62 
63  // This tells mktime that it's not known whether it's daylight
64  // savings time or not.
65  newTime.tm_isdst = -1;
66 
67  setTics(mktime(&newTime));
68  binaryTime += fraction;
69 }
70 
71 void PHTimeStamp::set(const char *timeString)
72 {
73 #ifndef WIN32
74  tm newTime;
75  strptime(timeString, "%A %h %d %H:%M:%S %Y", &newTime);
76  setTics(mktime(&newTime));
77 #endif
78 }
79 
81 {
82  setTics(time(nullptr));
83 }
84 
85 time_t PHTimeStamp::getTics() const
86 {
88 }
89 
90 void PHTimeStamp::setTics(const time_t tics)
91 {
93 }
94 
96 {
97  binaryTime = t;
98 }
99 
101 {
102  return tics * ticFactor + ticOffset;
103 }
104 
106 {
107  return (bt - ticOffset) / ticFactor;
108 }
109 
111 {
112  return (binaryTime > t1.getBinaryTime() && binaryTime < t2.getBinaryTime());
113 }
114 
116 {
117  std::cout << *this;
118 }
119 
120 //
121 // Operators
122 //
124 {
125  return binaryTime == t.getBinaryTime();
126 }
127 
129 {
130  return binaryTime != t.getBinaryTime();
131 }
132 
134 {
135  return binaryTime > t.getBinaryTime();
136 }
137 
139 {
140  return binaryTime < t.getBinaryTime();
141 }
142 
144 {
145  return binaryTime >= t.getBinaryTime();
146 }
147 
149 {
150  return binaryTime <= t.getBinaryTime();
151 }
152 
154 {
155  binaryTime += t * ticFactor;
156  return *this;
157 }
158 
160 {
161  binaryTime -= t * ticFactor;
162  return *this;
163 }
164 
165 void PHTimeStamp::print() const
166 {
167  std::cout << *this << std::endl;
168 }
170 {
171  // this one gives, for naming purposes, the time string
172  // without blanks
173 
174  time_t tics = getTics();
175  char timeString[25];
176  timeString[24] = '\0';
177  strncpy(timeString, ctime(&tics), 24);
178  char *line = new char[25];
179 
180  char *u = strtok(timeString, " ");
181 
182  if (u) strcpy(line, u);
183 
184  while ((u = strtok(nullptr, " ")))
185  {
186  strcat(line, "_");
187  strcat(line, u);
188  }
189  return line;
190 }
191 
192 //
193 // Non member operators and functions
194 //
195 
197 {
198  PHTimeStamp newTime = t1;
199  newTime += t2;
200  return newTime;
201 }
202 
204 {
205  PHTimeStamp newTime = t1;
206  newTime -= t2;
207  return newTime;
208 }
209 
210 time_t operator-(const PHTimeStamp &t1, const PHTimeStamp &t2)
211 {
212  return t1.getTics() - t2.getTics();
213 }
214 
215 std::ostream &operator<<(std::ostream &s, const PHTimeStamp &t)
216 {
217  time_t tics = t.getTics();
218  char timeString[25];
219  timeString[24] = '\0';
220  strncpy(timeString, ctime(&tics), 24);
221  return s << timeString;
222 }
223 
224 std::istream &operator>>(std::istream &s, PHTimeStamp &t)
225 {
226  char timeString[25];
227  s.getline(timeString, 25);
228  t.set(timeString);
229  return s;
230 }