-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdatetime.cpp
355 lines (285 loc) · 9.69 KB
/
datetime.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
// datetime.cpp - This is a modified version of the Date/Time class
// originally created by Charles Schwarz and Gordon Adams
// -- Steve Hilla, 1 May 2000.
//
//0005.01, SAH, Change code to use mjd, fmjd as the private data. The
// difference between two DateTime objects will be in "days".
//0012.27, SAH, Change LeapMonths[] and NormalMonths[] to hold 13 elements.
//0103.13, SAH, modify the >, >=, <, <= manipulator member functions.
//0104.11, SAH, Add an output operator function ( << ).
//0105.08, SAH, add normalize function to prevent HMS output where sec = 60.0
//0606.19, SAH, add code to DateTime::operator + ( const double days ), to
// handle cases where the input value days is negative.
//1102.03, SAH, added static_cast calls to prevent compiler warnings.
//1105.13, SAH, modify GetYMDHMS() and operator<< to avoid sec = 60.0 or -0.0
//1105.18, SAH, change operator == and != to account for roundoff.
//1107.11, SAH, change "datetime.h" to "datetime.hpp".
//1304.15, SAH, for just rinex_ho, change "datetime.hpp" to "datetime.h".
//#include "datetime.hpp"
#include "datetime.h"
#if !defined(MATH_)
#include <cmath>
#define MATH_
#endif
#if !defined(IOSTREAM_)
#include <iostream>
#define IOSTREAM_
#endif
#if !defined(IOMANIP_)
#include <iomanip>
#define IOMANIP_
#endif
namespace NGSdatetime {
using namespace std;
const long JAN61980 = 44244;
const long JAN11901 = 15385;
const double SECPERDAY = 86400.0;
const double EPS_FMJD = 1.0E-8/86400.0; // ten nanosec in days
const long LeapMonths[13] = { 0, 31, 60, 91, 121, 152, 182,
213, 244, 274, 305, 335, 366 };
const long NormalMonths[13] = { 0, 31, 59, 90, 120, 151, 181,
212, 243, 273, 304, 334, 365 };
// Constructors
DateTime::DateTime() { mjd = 0; fractionOfDay = 0.0; }
DateTime::DateTime( GPSTime gpstime )
{
mjd = static_cast< long >(gpstime.GPSWeek*7
+ gpstime.secsOfWeek/SECPERDAY + JAN61980);
fractionOfDay = fmod(gpstime.secsOfWeek,SECPERDAY)/SECPERDAY;
}
DateTime::DateTime( MJD MJD2 )
{
mjd = MJD2.mjd;
fractionOfDay = MJD2.fracOfDay;
}
DateTime::DateTime( YDOYHMS yearDoyHMS )
{
mjd = ((yearDoyHMS.year - 1901)/4)*1461 + ((yearDoyHMS.year - 1901)%4)*365
+ yearDoyHMS.dayOfYear - 1 + JAN11901;
fractionOfDay = ((yearDoyHMS.sec/60.0 + yearDoyHMS.min)/60.0 + yearDoyHMS.hour)/24.0;
}
DateTime::DateTime( YMDHMS yearMonthDayHMS )
{
long doy;
if( yearMonthDayHMS.year%4 == 0 ) // good until the year 2100
doy = LeapMonths[yearMonthDayHMS.month - 1] + yearMonthDayHMS.day;
else
doy = NormalMonths[yearMonthDayHMS.month - 1] + yearMonthDayHMS.day;
mjd = ((yearMonthDayHMS.year - 1901)/4)*1461 + ((yearMonthDayHMS.year -
1901)%4)*365 + doy - 1 + JAN11901;
fractionOfDay = ((yearMonthDayHMS.sec/60.0 + yearMonthDayHMS.min)/60.0 +
yearMonthDayHMS.hour)/24.0;
}
DateTime::DateTime( long year, long month, long day,
long hour, long min, double sec )
{
long doy;
if( year%4 == 0 ) // good until the year 2100
doy = LeapMonths[month - 1] + day;
else
doy = NormalMonths[month - 1] + day;
mjd = ((year - 1901)/4)*1461 + ((year -
1901)%4)*365 + doy - 1 + JAN11901;
fractionOfDay = ((sec/60.0 + min)/60.0 + hour)/24.0;
}
// Destructor
DateTime::~DateTime()
{
// no need to delete mjd and fractionOfDay
// since they were not created using new.
}
// Initializers - used to set the value of an existing variable.
void DateTime::SetGPSTime( GPSTime gpstime2 )
{
*this = DateTime( gpstime2 );
}
void DateTime::SetMJD( MJD inputMJD )
{
*this = DateTime( inputMJD );
}
void DateTime::SetYDOYHMS( YDOYHMS yearDoyHMS )
{
*this = DateTime( yearDoyHMS );
}
void DateTime::SetYMDHMS( YMDHMS yearMonthDayHMS )
{
*this = DateTime( yearMonthDayHMS );
}
void DateTime::SetYMDHMS( long year, long month, long monthday,
long hour, long min, double sec )
{
*this = DateTime( year, month, monthday, hour, min, sec );
}
// Selectors - return the contents of a variable.
GPSTime DateTime::GetGPSTime()
{
GPSTime tt;
tt.GPSWeek = (mjd - JAN61980)/7;
tt.secsOfWeek = ( (mjd - JAN61980) - tt.GPSWeek*7 +
fractionOfDay)*SECPERDAY;
return tt;
}
MJD DateTime::GetMJD()
{
MJD mm;
mm.mjd = mjd;
mm.fracOfDay = fractionOfDay;
return mm;
}
YDOYHMS DateTime::GetYDOYHMS()
{
YDOYHMS yy;
long daysFromJan11901, deltaYears, numberFourYears;
long yearsSoFar, daysLeft;
daysFromJan11901 = mjd - JAN11901;
numberFourYears = daysFromJan11901/1461;
yearsSoFar = 1901 + 4*numberFourYears;
daysLeft = daysFromJan11901 - 1461*numberFourYears;
deltaYears = daysLeft/365 - daysLeft/1460;
yy.year = yearsSoFar + deltaYears;
yy.dayOfYear = daysLeft - 365*deltaYears + 1;
yy.hour = static_cast< long >(fractionOfDay*24.0);
yy.min = static_cast< long >(fractionOfDay*1440.0 - yy.hour*60.0);
yy.sec = fractionOfDay*86400.0 - yy.hour*3600.0 - yy.min*60.0;
return yy;
}
YMDHMS DateTime::GetYMDHMS()
{
YMDHMS ymd;
long daysFromJan11901, deltaYears, numberFourYears;
long more, guess, doy, yearsSoFar, daysLeft;
double tenthNanosec = 0.0000000001/86400.0;
daysFromJan11901 = mjd - JAN11901;
numberFourYears = daysFromJan11901/1461;
yearsSoFar = 1901 + 4*numberFourYears;
daysLeft = daysFromJan11901 - 1461*numberFourYears;
deltaYears = daysLeft/365 - daysLeft/1460;
ymd.year = yearsSoFar + deltaYears;
doy = daysLeft - 365*deltaYears + 1;
// Add 0.1 nanosec to fractionOfDay to avoid roundoff error
fractionOfDay = fractionOfDay + tenthNanosec;
ymd.hour = static_cast< long >(fractionOfDay*24.0);
ymd.min = static_cast< long >(fractionOfDay*1440.0 - ymd.hour*60.0);
ymd.sec = fractionOfDay*86400.0 - ymd.hour*3600.0 - ymd.min*60.0;
// Subtract 0.1 nanosec after computing ymd.sec
ymd.sec = ymd.sec - tenthNanosec;
guess = static_cast< long >(doy*0.032);
more = 0;
if( ymd.year%4 == 0 ) // good until the year 2100
{
if( (doy - LeapMonths[guess+1]) > 0 ) more = 1;
ymd.month = guess + more + 1;
ymd.day = doy - LeapMonths[guess+more];
}
else
{
if( (doy - NormalMonths[guess+1]) > 0 ) more = 1;
ymd.month = guess + more + 1;
ymd.day = doy - NormalMonths[guess+more];
}
return ymd;
}
// Operators
// This operator may not be needed. This code generates
// a warning message on the SUN C++ compiler.
/*
DateTime* DateTime::operator & (DateTime input)
{
return &input;
}
*/
// const return avoids: (a1 = a2 ) = a3
const DateTime &DateTime::operator=(const DateTime &DT2)
{
if( &DT2 != this ) // avoids self assignment
{
mjd = DT2.mjd;
fractionOfDay = DT2.fractionOfDay;
}
return *this;
}
DateTime DateTime::operator + ( const double days ) // DT + "d.fff"
{
DateTime DT;
DT.mjd = mjd;
DT.fractionOfDay = fractionOfDay + days;
if( DT.fractionOfDay > 1.0 )
{
double wholeDays = floor( DT.fractionOfDay ); // use normjd *******
DT.mjd = DT.mjd + (long) wholeDays;
DT.fractionOfDay = DT.fractionOfDay - wholeDays;
}
if( DT.fractionOfDay < 0.0 ) // allow for DT - "d.fff" (i.e., 'days' variable is negative)
{
double wholeDays = ceil( DT.fractionOfDay );
DT.mjd = DT.mjd - (long) wholeDays - 1;
DT.fractionOfDay = 1.0 + (DT.fractionOfDay - wholeDays);
}
return DT;
}
double DateTime::operator - ( const DateTime &DT2 ) // find difference in days
// this method will allow a negative difference
{
return ( ( ((double)mjd - (double)DT2.mjd) + fractionOfDay ) -
DT2.fractionOfDay );
}
bool DateTime::operator == ( const DateTime &DT2 )
{
return ( mjd == DT2.mjd &&
fabs(fractionOfDay - DT2.fractionOfDay) < EPS_FMJD );
}
bool DateTime::operator != ( const DateTime &DT2 )
{
return ( mjd != DT2.mjd ||
fabs(fractionOfDay - DT2.fractionOfDay) > EPS_FMJD );
}
bool DateTime::operator > ( const DateTime &DT2 )
{
if ( mjd > DT2.mjd ||
(mjd == DT2.mjd && fractionOfDay > DT2.fractionOfDay) )
{
return true;
}
return false;
}
bool DateTime::operator >= ( const DateTime &DT2 )
{
if ( mjd > DT2.mjd ||
(mjd == DT2.mjd && fractionOfDay >= DT2.fractionOfDay) )
{
return true;
}
return false;
}
bool DateTime::operator < ( const DateTime &DT2 )
{
if ( mjd < DT2.mjd ||
(mjd == DT2.mjd && fractionOfDay < DT2.fractionOfDay) )
{
return true;
}
return false;
}
bool DateTime::operator <= ( const DateTime &DT2 )
{
if ( mjd < DT2.mjd ||
(mjd == DT2.mjd && fractionOfDay <= DT2.fractionOfDay) )
{
return true;
}
return false;
}
ostream &operator<<( ostream &output, DateTime &dt )
{
YMDHMS ymdhms = dt.GetYMDHMS();
// Check for roundoff error that might cause sec = -0.0000000001
// Note: rcvr clk offset in rinex obs files is written to 9 decimal places
if( fabs(ymdhms.sec) < 0.0000000001 ) ymdhms.sec = 0.0;
output.setf( ios::fixed, ios::floatfield );
output << setw(4) << ymdhms.year << " " << setw(2) << ymdhms.month << " "
<< setw(2) << ymdhms.day << " " << setw(2) << ymdhms.hour << ":"
<< setw(2) << ymdhms.min << ":" << setw(13) << setprecision(9)
<< ymdhms.sec;
return output; // enables chaining
}
} // namespace NGSdatetime