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