Logo Coherent WaveBurst  
Reference Guide
Logo
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
time.hh
Go to the documentation of this file.
1 #ifndef TIME_H
2 #define TIME_H
3 
4 #include <time.h>
5 #include <stdlib.h>
6 #include <math.h>
7 #include <ctype.h>
8 #include <string>
9 #include <iostream>
10 #include <fstream>
11 #include "TROOT.h"
12 #include "TObjArray.h"
13 #include "TObjString.h"
14 #include "TString.h"
15 
16 #define INT_4S int
17 #define INT_4U unsigned int
18 
19 #define UTC_UNIX_SPAN 315964800
20 #define TAI_GPS_LEAPS_SPAN 19
21 
22 #define GPS_LEAPS_TABLE_SIZE 19
23 
24 using namespace std;
25 
26 struct gps_leap {
27  int gps; /* GPS time when leap sec was introduced */
28  int gps_utc; /* GPS-UTC at GPS time gps */
29 };
30 
31 // UPDATE-ME -- to update, add the new (GPS, GPS - UTC) entry at the end
32 // see http://hpiers.obspm.fr/eoppc/bul/bulc/bulletinc.dat */
33 // Table of GPS-UTC */
35 {
36  {0, 0}, /* 1980-Jan-06 */
37  {46828801, 1}, /* 1981-Jul-01 */
38  {78364802, 2}, /* 1982-Jul-01 */
39  {109900803, 3}, /* 1983-Jul-01 */
40  {173059204, 4}, /* 1985-Jul-01 */
41  {252028805, 5}, /* 1988-Jan-01 */
42  {315187206, 6}, /* 1990-Jan-01 */
43  {346723207, 7}, /* 1991-Jan-01 */
44  {393984008, 8}, /* 1992-Jul-01 */
45  {425520009, 9}, /* 1993-Jul-01 */
46  {457056010, 10}, /* 1994-Jul-01 */
47  {504489611, 11}, /* 1996-Jan-01 */
48  {551750412, 12}, /* 1997-Jul-01 */
49  {599184013, 13}, /* 1999-Jan-01 */
50  {820108814, 14}, /* 2006-Jan-01 */
51  {914803215, 15}, /* 2009-Jan-01 */
52  {1025136016,16}, /* 2012-Jul-01 */
53  {1119744017,17}, /* 2015-Jul-01 */
54  {1167264018,18} /* 2017-Jan-01 */
55 };
56 
57 namespace wat {
58 
59 //-----------------------------------------------------------------------------
60 //
61 // A class storing seconds & nanoseconds.
62 // This is a very simple class storing time as integer seconds and nanoseconds.
63 // A time type storing seconds and nanoseconds as integers with full precision.
64 
65 class Time {
66 
67 public:
68 
69  // Constructor
70  explicit Time(INT_4S sec = 0, INT_4U nsec = 0);
71 
72  Time(TString date) {tzset();setenv("TZ", ":UTC", 1);SetDateString(date);}
73 
74  // Copy constructor
75  Time(Time& time);
76 
77  // Double constructor
78  Time(double dtime);
79 
80  // = Overload
81  Time& operator=(Time& time);
82 
83  // Addition & assignment.
84  // param: Time& time -
85  // return: Time& time -
86 
87  Time& operator+=(Time& time);
88  Time& operator-=(Time& time);
89 
90 
91  // Multiplication and assignment.
92  // param: double& d -
93  // return: Time& time -
94  // todo: What happens if d is negative?
95 
96  Time& operator*=(double& d);
97  Time& operator/=(double& d);
98  double operator/(Time& time );
99  bool operator==(Time& time);
100  bool operator<=(Time& time);
101  bool operator>=(Time& time);
102  bool operator!=(Time& time);
103  bool operator<(Time& time);
104  bool operator>(Time& time);
105 
106  // Access gps seconds
107  INT_4S GetGPS() {return GetSec();}
108 
109  // Access seconds
110  INT_4S GetSec();
111 
112  // Access nanoseconds
113  INT_4U GetNSec();
114 
115  // Mutators
116 
117  //Set seconds
118  INT_4S SetSec(INT_4S s) {return mSec = s;}
119 
120  // Set nanosecond residual
121  INT_4U SetNSec(INT_4U nsec) {return mNSec = nsec;}
122 
123  //set Double conversion method
124  void SetDouble(double dt);
125 
126  // get Double conversion method
127  double GetDouble();
128 
129  // set Date conversion method
130  void SetDate(int ss, int mm, int hh, int DD, int MM, int YY, int nsec = 0);
131 
132  // set Date from String conversion method
133  void SetDateString(TString date);
134  void SetString(char* date, int nsec = 0);
135  void SetYear(unsigned int year) {
136  TString str;str.Form("%04d",year);
137  SetDateString(GetDateString().Replace(0,4,str,4)(0,19));}
138  void SetMonth(unsigned int month) {
139  TString str;str.Form("%02d",month);
140  SetDateString(GetDateString().Replace(5,2,str,2)(0,19));}
141  void SetDay(unsigned int day) {
142  TString str;str.Form("%02d",day);
143  SetDateString(GetDateString().Replace(8,2,str,2)(0,19));}
144  void SetHour(unsigned int hour) {
145  TString str;str.Form("%02d",hour);
146  SetDateString(GetDateString().Replace(11,2,str,2)(0,19));}
147  void SetMinute(unsigned int min) {
148  TString str;str.Form("%02d",min);
149  SetDateString(GetDateString().Replace(14,2,str,2)(0,19));}
150  void SetSecond(unsigned int sec) {
151  TString str;str.Form("%02d",sec);
152  SetDateString(GetDateString().Replace(17,2,str,2)(0,19));}
153 
154  // get Date String conversion method
155  TString GetDateString();
156  int GetDayOfYear() {
157  TString date = GetDateString();
158  TString begin_of_year = date(0,4)+TString("-01-01 00:00:00");
159  return 1+(GetSec()-Time(begin_of_year).GetSec())/86400.;}
160  int GetYear() {
161  TString date = GetDateString();
162  TString year = date(0,4);
163  return year.Atoi();}
164  int GetMonth() {
165  TString date = GetDateString();
166  TString month = date(5,2);
167  return month.Atoi();}
168  int GetDay() {
169  TString date = GetDateString();
170  TString day = date(8,2);
171  return day.Atoi();}
172  int GetHour() {
173  TString date = GetDateString();
174  TString hour = date(11,2);
175  return hour.Atoi();}
176  int GetMinute() {
177  TString date = GetDateString();
178  TString minute = date(14,2);
179  return minute.Atoi();}
180  int GetSecond() {
181  TString date = GetDateString();
182  TString second = date(17,2);
183  return second.Atoi();}
184 
185  int GetLeapSecs() {return GpsToGpsLeaps(mSec);}
186  void PrintLeapSecs() {
187  for(int i=0;i<GPS_LEAPS_TABLE_SIZE;i++)
188  cout << Time(gps_leaps_table[i].gps).GetDateString().Data() << " "
189  << gps_leaps_table[i].gps << " "
190  << gps_leaps_table[i].gps_utc << endl;}
191 
192  // Dumps date
193  void Print();
194 
195  int GetJulianDay() {return floor(GetJulianDate());}
196  int GetModJulianDay() {return floor(GetModJulianDate());}
197  double GetJulianDate();
198  double GetModJulianDate();
199 
200  int GpsToGpsLeaps(int gps);
201  int UnixToGpsLeaps(int unix_time);
202  int GpsToGpsLeaps() {return GpsToGpsLeaps(mSec);}
203  int UnixToGpsLeaps() {return UnixToGpsLeaps(mSec);}
204 
206  int UnixToTaiLeaps(int unix_time) {return UnixToGpsLeaps(unix_time)+TAI_GPS_LEAPS_SPAN;}
208  int UnixToTaiLeaps() {return UnixToGpsLeaps()+TAI_GPS_LEAPS_SPAN;}
209 
210  // UNIX --> GPS (frame) time conversion
211  // UNIX = UTC from 1/1/1970 (unix time()) ; GPS = GPS from 6/1/1980
212 
213  void UnixToGps() {
214  mSec = mSec - UTC_UNIX_SPAN + UnixToGpsLeaps(); }
215 
216  // GPS (frame) --> UNIX time conversion
217  // UNIX = UTC from 1/1/1970 (unix time()) ; GPS = GPS from 6/1/1980
218 
219  void GpsToUnix() {
220  mSec = mSec + UTC_UNIX_SPAN - GpsToGpsLeaps(); }
221 
222  // return unix time from GPS
224  return mSec + UTC_UNIX_SPAN - GpsToGpsLeaps(); }
225 
226  // cout adapter
227  friend istream& operator>>( istream&, Time& );
228 
229 private:
230 
231  void Error(char* msg) {cout << "CWB::Time:Error " << msg << endl;exit(1);}
232 
235 
236  // used by THtml doc
237  ClassDef(Time,1)
238 };
239 
240 static Time Time_MAX(0x7FFFFFFF,999999999);
241 
242 Time operator+(Time& t1, Time& t2);
243 Time operator-(Time& t1, Time& t2);
244 Time operator*(Time& t, double& d);
245 Time operator/(Time& t, double& d);
246 Time operator*(double& d, Time& t);
247 
248 istream& operator>>(istream& in, Time& time);
249 ostream& operator<<(ostream& out, Time& time);
250 
251 } // end namespace
252 
253 #endif
wavearray< double > t(hp.size())
void Error(char *msg)
Definition: time.hh:231
int GpsToGpsLeaps(int gpsSec)
INT_4S mSec
Definition: time.hh:233
void SetMonth(unsigned int month)
Definition: time.hh:138
int GetModJulianDay()
Definition: time.hh:196
INT_4S GetGPS()
Definition: time.hh:107
TString GetDateString()
Definition: time.cc:443
double min(double x, double y)
Definition: eBBH.cc:13
int GpsToTaiLeaps()
Definition: time.hh:207
int GetSecond()
Definition: time.hh:180
int GetDayOfYear()
Definition: time.hh:156
TString("c")
Time(TString date)
Definition: time.hh:72
void SetDay(unsigned int day)
Definition: time.hh:141
Definition: time.hh:26
int GetHour()
Definition: time.hh:172
int gps
Definition: time.hh:27
STL namespace.
Time operator-(Time &t1, Time &t2)
Definition: time.cc:240
int GpsToTaiLeaps(int gps)
Definition: time.hh:205
void SetYear(unsigned int year)
Definition: time.hh:135
Time operator+(Time &t1, Time &t2)
Definition: time.cc:228
static Time Time_MAX(0x7FFFFFFF, 999999999)
i drho i
int UnixToTaiLeaps(int unix_time)
Definition: time.hh:206
Definition: alm.hh:20
INT_4U SetNSec(INT_4U nsec)
Definition: time.hh:121
cout<< "Selected Pixels : "<< nPix<< endl;wc.cluster(1, 1);SSeries< double > ss
int gps_utc
Definition: time.hh:28
istream & operator>>(istream &in, Time &time)
Definition: time.cc:285
void UnixToGps()
Definition: time.hh:213
ofstream out
Definition: cwb_merge.C:196
MDC Print()
char str[1024]
double time[6]
Definition: cbc_plots.C:435
int GpsToUnixTime()
Definition: time.hh:223
#define GPS_LEAPS_TABLE_SIZE
Definition: time.hh:22
#define INT_4U
Definition: time.hh:17
ostream & operator<<(ostream &out, Time &time)
Definition: time.cc:614
int GetMonth()
Definition: time.hh:164
void SetSecond(unsigned int sec)
Definition: time.hh:150
void PrintLeapSecs()
Definition: time.hh:186
Time operator*(double &d, Time &t)
Definition: time.cc:273
static const gps_leap gps_leaps_table[GPS_LEAPS_TABLE_SIZE]
Definition: time.hh:34
INT_4U mNSec
Definition: time.hh:234
Time operator/(Time &t, double &d)
Definition: time.cc:262
double dt
s s
Definition: cwb_net.C:137
int UnixToGpsLeaps()
Definition: time.hh:203
double gps
ifstream in
int GetMinute()
Definition: time.hh:176
#define TAI_GPS_LEAPS_SPAN
Definition: time.hh:20
INT_4S SetSec(INT_4S s)
Definition: time.hh:118
int GetJulianDay()
Definition: time.hh:195
void SetHour(unsigned int hour)
Definition: time.hh:144
INT_4S GetSec()
Definition: time.cc:628
#define INT_4S
Definition: time.hh:16
TH1 * t1
#define UTC_UNIX_SPAN
Definition: time.hh:19
void GpsToUnix()
Definition: time.hh:219
int GetLeapSecs()
Definition: time.hh:185
int GpsToGpsLeaps()
Definition: time.hh:202
int GetDay()
Definition: time.hh:168
int GetYear()
Definition: time.hh:160
int UnixToTaiLeaps()
Definition: time.hh:208
exit(0)
void SetMinute(unsigned int min)
Definition: time.hh:147