Class Reference for E1039 Core & Analysis Software
PHTimeStamp.cc
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2 // $Header: /afs/rhic.bnl.gov/phenix/PHENIX_CVS/online_distribution/newbasic/PHTimeStamp.C,v 1.23 2009/08/18 23:03:01 pinkenbu Exp $
3 //
4 // The PHOOL's Software
5 // Copyright (C) PHENIX collaboration, 1999
6 //
7 // Implementation of class PHTimeStamp
8 //
9 // Author: Matthias Messer
10 //-----------------------------------------------------------------------------
11 #include "PHTimeStamp.h"
12 
13 #include <cstdlib>
14 #include <cstring>
15 #include <iostream>
16 
17 using namespace std;
18 
19 #ifndef HAVE_STRPTIME_PROTOTYPE
20 extern "C"
21 {
22  char *strptime(const char *s, const char *format, struct
23  tm *tm);
24 }
25 #endif
26 
27 
28 #ifdef WIN32
29 const phtime_t ticOffset = 35067168000000000UL;
30 #else
31 const phtime_t ticOffset = 35067168000000000ULL;
32 #endif
33 
34 const phtime_t ticFactor = 10000000;
35 
37 {
38  setTics(time(0));
39 
40  setenv("TZ","EST5EDT",1);
41 }
42 
43 PHTimeStamp::PHTimeStamp(const int year, const int month, const int day, const int hour, const int minute, const int second, const int fraction)
44 {
45  set(year, month, day, hour, minute, second, fraction);
46  setenv("TZ","EST5EDT",1);
47 
48 }
49 
50 PHTimeStamp::PHTimeStamp(const time_t t)
51 {
52  setTics(t);
53  setenv("TZ","EST5EDT",1);
54 }
55 
56 
57 void
58 PHTimeStamp::set(const int year, const int month, const int day,
59  const int hour, const int minute,
60  const int second, const int fraction)
61 {
62  if (year < 1900)
63  {
64  setTics(0);
65  return;
66  }
67  tm newTime;
68  newTime.tm_year = year - 1900;
69  newTime.tm_mon = month - 1;
70  newTime.tm_mday = day;
71  newTime.tm_hour = hour;
72  newTime.tm_min = minute;
73  newTime.tm_sec = second;
74 
75  // This tells mktime that it's not known whether it's daylight
76  // savings time or not.
77  newTime.tm_isdst = -1;
78 
79  setTics(mktime(&newTime));
80  binaryTime += fraction;
81 }
82 
83 void PHTimeStamp::set(const char * timeString)
84 {
85  tm newTime;
86 #ifndef WIN32
87  strptime(timeString, "%A %h %d %H:%M:%S %Y", &newTime);
88  setTics(mktime(&newTime));
89 #endif
90 }
91 
93 {
94  setTics(time(0));
95 }
96 
97 time_t PHTimeStamp::getTics() const
98 {
99  return binaryTimeToTics(binaryTime);
100 }
101 
102 void PHTimeStamp::setTics(const time_t tics)
103 {
104  binaryTime = ticsToBinaryTime(tics);
105 }
106 
108 {
109  binaryTime = t;
110 }
111 
112 phtime_t PHTimeStamp::ticsToBinaryTime(time_t tics) const
113 {
114  return tics * ticFactor + ticOffset;
115 }
116 
117 time_t PHTimeStamp::binaryTimeToTics(phtime_t bt) const
118 {
119  return (bt - ticOffset) / ticFactor;
120 }
121 
122 int PHTimeStamp::isInRange(const PHTimeStamp & t1, const PHTimeStamp & t2)
123 {
124  return ( binaryTime > t1.getBinaryTime() && binaryTime < t2.getBinaryTime());
125 }
126 
128 {
129  cout << *this;
130 }
131 
132 //
133 // Operators
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  return binaryTime >= t.getBinaryTime();
158 }
159 
161 {
162  return binaryTime <= t.getBinaryTime();
163 }
164 
166 {
167  binaryTime = t.getBinaryTime();
168  return *this;
169 }
170 
172 {
173  binaryTime += t * ticFactor;
174  return *this;
175 }
176 
178 {
179  binaryTime -= t * ticFactor;
180  return *this;
181 }
182 
183 void PHTimeStamp::print() const
184 {
185  cout << *this << endl;
186 }
188 {
189 
190  // this one gives, for naming purposes, the time string
191  // without blanks
192 
193  time_t tics = getTics();
194  char timeString[25];
195  timeString[24] = '\0';
196  strncpy(timeString, ctime(&tics), 24);
197  char *line = new char[25];
198 
199  char *u = strtok (timeString, " ");
200 
201  if (u) strcpy (line,u);
202 
203  while ( (u=strtok(0," ")) )
204  {
205  strcat (line, "_");
206  strcat (line, u);
207  }
208  return line;
209 }
210 
211 //
212 // Non member operators and functions
213 //
214 
215 PHTimeStamp operator + (const PHTimeStamp & t1, time_t t2)
216 {
217  PHTimeStamp newTime = t1;
218  newTime += t2;
219  return newTime;
220 }
221 
222 PHTimeStamp operator - (const PHTimeStamp & t1, time_t t2)
223 {
224  PHTimeStamp newTime = t1;
225  newTime -= t2;
226  return newTime;
227 }
228 
229 time_t operator - (const PHTimeStamp & t1, const PHTimeStamp & t2)
230 {
231  return t1.getTics() - t2.getTics();
232 }
233 
234 ostream & operator << (ostream & s, const PHTimeStamp & t)
235 {
236  time_t tics = t.getTics();
237  char timeString[25];
238  timeString[24] = '\0';
239  strncpy(timeString, ctime(&tics), 24);
240  return s << timeString;
241 }
242 
243 istream & operator >> (istream & s, PHTimeStamp & t)
244 {
245  char timeString[25];
246  s.getline(timeString, 25);
247  t.set(timeString);
248  return s;
249 }
ostream & operator<<(ostream &s, const PHTimeStamp &t)
Definition: PHTimeStamp.cc:234
const phtime_t ticFactor
Definition: PHTimeStamp.cc:34
const phtime_t ticOffset
Definition: PHTimeStamp.cc:31
PHTimeStamp operator-(const PHTimeStamp &t1, time_t t2)
Definition: PHTimeStamp.cc:222
char * strptime(const char *s, const char *format, struct tm *tm)
istream & operator>>(istream &s, PHTimeStamp &t)
Definition: PHTimeStamp.cc:243
PHTimeStamp operator+(const PHTimeStamp &t1, time_t t2)
Definition: PHTimeStamp.cc:215
unsigned long long phtime_t
Definition: PHTimeStamp.h:19
PHTimeStamp operator+=(time_t)
Definition: PHTimeStamp.cc:171
char * formatTimeString() const
Definition: PHTimeStamp.cc:187
PHTimeStamp & operator=(const PHTimeStamp &)
Definition: PHTimeStamp.cc:165
void setToSystemTime()
Definition: PHTimeStamp.cc:92
phtime_t getBinaryTime() const
Definition: PHTimeStamp.h:43
int operator<(const PHTimeStamp &) const
Definition: PHTimeStamp.cc:150
void print()
Definition: PHTimeStamp.cc:127
void set(const int, const int, const int, const int, const int, const int, const int=0)
Definition: PHTimeStamp.cc:58
PHTimeStamp operator-=(time_t)
Definition: PHTimeStamp.cc:177
int isInRange(const PHTimeStamp &, const PHTimeStamp &)
Definition: PHTimeStamp.cc:122
int operator>=(const PHTimeStamp &) const
Definition: PHTimeStamp.cc:155
void setTics(const time_t)
Definition: PHTimeStamp.cc:102
int operator==(const PHTimeStamp &) const
Definition: PHTimeStamp.cc:135
int operator<=(const PHTimeStamp &) const
Definition: PHTimeStamp.cc:160
int operator!=(const PHTimeStamp &) const
Definition: PHTimeStamp.cc:140
time_t getTics() const
Definition: PHTimeStamp.cc:97
int operator>(const PHTimeStamp &) const
Definition: PHTimeStamp.cc:145
void setBinTics(const phtime_t t)
Definition: PHTimeStamp.cc:107