1 | #include <time.h>
|
---|
2 | #include <cmath>
|
---|
3 | #include <cstdio>
|
---|
4 | #include <sstream>
|
---|
5 | #include <iomanip>
|
---|
6 |
|
---|
7 | #include "t_time.h"
|
---|
8 |
|
---|
9 | using namespace std;
|
---|
10 |
|
---|
11 | double t_time::djul(long jj, long mm, double tt) {
|
---|
12 | long ii, kk;
|
---|
13 | double djul ;
|
---|
14 | if( mm <= 2 ) {
|
---|
15 | jj = jj - 1;
|
---|
16 | mm = mm + 12;
|
---|
17 | }
|
---|
18 | ii = jj/100;
|
---|
19 | kk = 2 - ii + ii/4;
|
---|
20 | djul = (365.25*jj - fmod( 365.25*jj, 1.0 )) - 679006.0;
|
---|
21 | djul = djul + floor( 30.6001*(mm + 1) ) + tt + kk;
|
---|
22 | return djul;
|
---|
23 | }
|
---|
24 |
|
---|
25 | double t_time::gpjd(double second, int nweek) {
|
---|
26 | double deltat;
|
---|
27 | deltat = nweek*7.0 + second/86400.0 ;
|
---|
28 | return( 44244.0 + deltat) ;
|
---|
29 | }
|
---|
30 |
|
---|
31 | void t_time::jdgp(double tjul, double & second, long & nweek) {
|
---|
32 | double deltat;
|
---|
33 | deltat = tjul - 44244.0 ;
|
---|
34 | nweek = (long) floor(deltat/7.0);
|
---|
35 | second = (deltat - (nweek)*7.0)*86400.0;
|
---|
36 | }
|
---|
37 |
|
---|
38 | void t_time::jmt(double djul, long& jj, long& mm, double& dd) {
|
---|
39 | long ih, ih1, ih2 ;
|
---|
40 | double t1, t2, t3, t4;
|
---|
41 | t1 = 1.0 + djul - fmod( djul, 1.0 ) + 2400000.0;
|
---|
42 | t4 = fmod( djul, 1.0 );
|
---|
43 | ih = long( (t1 - 1867216.25)/36524.25 );
|
---|
44 | t2 = t1 + 1 + ih - ih/4;
|
---|
45 | t3 = t2 - 1720995.0;
|
---|
46 | ih1 = long( (t3 - 122.1)/365.25 );
|
---|
47 | t1 = 365.25*ih1 - fmod( 365.25*ih1, 1.0 );
|
---|
48 | ih2 = long( (t3 - t1)/30.6001 );
|
---|
49 | dd = t3 - t1 - (int)( 30.6001*ih2 ) + t4;
|
---|
50 | mm = ih2 - 1;
|
---|
51 | if ( ih2 > 13 ) mm = ih2 - 13;
|
---|
52 | jj = ih1;
|
---|
53 | if ( mm <= 2 ) jj = jj + 1;
|
---|
54 | }
|
---|
55 |
|
---|
56 | t_time::t_time(int gpsw, int dow, double daysec) {
|
---|
57 | this->set(gpsw, dow, daysec);
|
---|
58 | }
|
---|
59 |
|
---|
60 | t_time::t_time(int gpsw, double gpssec) {
|
---|
61 | this->set(gpsw, gpssec);
|
---|
62 | }
|
---|
63 |
|
---|
64 | t_time::t_time(int hour, int min, double sec,
|
---|
65 | int day, int month, int year) {
|
---|
66 | double daysec = hour*3600.0 + min*60.0 + sec;
|
---|
67 | this->set(daysec, day, month, year);
|
---|
68 | }
|
---|
69 |
|
---|
70 | t_time::t_time(double daysec, int day, int month, int year) {
|
---|
71 | this->set(daysec, day, month, year);
|
---|
72 | }
|
---|
73 |
|
---|
74 | t_time::t_time(double daysec, int doy, unsigned int year) {
|
---|
75 | this->set(daysec, doy, year);
|
---|
76 | }
|
---|
77 |
|
---|
78 | t_time& t_time::set(int gpsw, int dow, double daysec) {
|
---|
79 | int deltad;
|
---|
80 | while (daysec >= 86400) {
|
---|
81 | daysec-=86400;
|
---|
82 | dow++;
|
---|
83 | }
|
---|
84 | while (daysec < 0) {
|
---|
85 | daysec+=86400;
|
---|
86 | dow--;
|
---|
87 | }
|
---|
88 | deltad = gpsw*7 + dow;
|
---|
89 | _mjd = 44244 + deltad;
|
---|
90 | _sec = daysec;
|
---|
91 | return *this;
|
---|
92 | }
|
---|
93 |
|
---|
94 | t_time& t_time::set(int gpsw, double gpssec) {
|
---|
95 | int deltad;
|
---|
96 | int dow = 0;
|
---|
97 | while ( gpssec >= 86400 ) {
|
---|
98 | gpssec-=86400;
|
---|
99 | dow++;
|
---|
100 | }
|
---|
101 | while ( gpssec < 0 ) {
|
---|
102 | gpssec+=86400;
|
---|
103 | dow--;
|
---|
104 | }
|
---|
105 | deltad = gpsw*7 + dow;
|
---|
106 | _mjd = 44244 + deltad;
|
---|
107 | _sec = gpssec;
|
---|
108 | return *this;
|
---|
109 | }
|
---|
110 |
|
---|
111 | t_time& t_time::set(int hour, int min, double sec, int day, int month, int year) {
|
---|
112 | return this->set(hour*3600 + min*60 + sec, day, month, year);
|
---|
113 | }
|
---|
114 |
|
---|
115 | t_time& t_time::set(double daysec, int day, int month, int year) {
|
---|
116 | _sec = daysec;
|
---|
117 | _mjd = (unsigned int)djul(year, month, day);
|
---|
118 | while ( _sec >= 86400 ) {
|
---|
119 | _sec-=86400;
|
---|
120 | _mjd++;
|
---|
121 | }
|
---|
122 | while ( _sec < 0 ) {
|
---|
123 | _sec+=86400;
|
---|
124 | _mjd--;
|
---|
125 | }
|
---|
126 | return *this;
|
---|
127 | }
|
---|
128 |
|
---|
129 | t_time& t_time::set(double daysec, int doy, int year) {
|
---|
130 | _mjd = (unsigned int)djul(year, 1, 1) + (doy - 1);
|
---|
131 | _sec = daysec;
|
---|
132 | while ( _sec >= 86400 ) {
|
---|
133 | _sec-=86400;
|
---|
134 | _mjd++;
|
---|
135 | }
|
---|
136 | while ( _sec < 0 ) {
|
---|
137 | _sec+=86400;
|
---|
138 | _mjd--;
|
---|
139 | }
|
---|
140 | return *this;
|
---|
141 | }
|
---|
142 |
|
---|
143 | t_time& t_time::setmjd(double daysec, int mjd) {
|
---|
144 | _sec = daysec;
|
---|
145 | _mjd = mjd;
|
---|
146 | while ( _sec >= 86400 ) {
|
---|
147 | _sec-=86400;
|
---|
148 | _mjd++;
|
---|
149 | }
|
---|
150 | while ( _sec < 0 ) {
|
---|
151 | _sec+=86400;
|
---|
152 | _mjd--;
|
---|
153 | }
|
---|
154 | return *this;
|
---|
155 | }
|
---|
156 |
|
---|
157 | t_time& t_time::setmjd(double mjddec) {
|
---|
158 | _mjd = static_cast<unsigned int>(mjddec);
|
---|
159 | _sec = (mjddec - _mjd)*86400.0;
|
---|
160 | return *this;
|
---|
161 | }
|
---|
162 |
|
---|
163 | t_time& t_time::operator++() { //prefix
|
---|
164 | return (*this) += 1.0;
|
---|
165 | }
|
---|
166 |
|
---|
167 | t_time t_time::operator++(int) { //postfix
|
---|
168 | t_time tmp = (*this);
|
---|
169 | (*this) += 1.0;
|
---|
170 | return tmp;
|
---|
171 | }
|
---|
172 |
|
---|
173 | t_time& t_time::operator--() { //prefix
|
---|
174 | return (*this) -= 1.0;
|
---|
175 | }
|
---|
176 |
|
---|
177 | t_time t_time::operator--(int) { //postfix
|
---|
178 | t_time tmp = *this;
|
---|
179 | (*this) -= 1.0;
|
---|
180 | return tmp;
|
---|
181 | }
|
---|
182 |
|
---|
183 | t_time t_time::operator+(double sec) const {
|
---|
184 | int mjd = this->mjd();
|
---|
185 | double daysec = this->daysec();
|
---|
186 | daysec+=sec;
|
---|
187 | return t_time().setmjd(daysec, mjd);
|
---|
188 | }
|
---|
189 |
|
---|
190 | t_time t_time::operator-(double sec) const {
|
---|
191 | return (*this) + (-sec);
|
---|
192 | }
|
---|
193 |
|
---|
194 | t_time& t_time::operator+=(double sec) {
|
---|
195 | _sec+=sec;
|
---|
196 | while ( _sec >= 86400 ) {
|
---|
197 | _sec-=86400;
|
---|
198 | _mjd++;
|
---|
199 | }
|
---|
200 | while ( _sec < 0 ) {
|
---|
201 | _sec+=86400;
|
---|
202 | _mjd--;
|
---|
203 | }
|
---|
204 | return *this;
|
---|
205 | }
|
---|
206 |
|
---|
207 | t_time& t_time::operator-=(double sec) {
|
---|
208 | return (*this)+=(-sec);
|
---|
209 | }
|
---|
210 |
|
---|
211 | double t_time::operator-(const t_time &time1) const {
|
---|
212 | int mjdDiff = this->_mjd - time1._mjd;
|
---|
213 | if ( mjdDiff != 0 ) {
|
---|
214 | return mjdDiff * 86400.0 + this->_sec - time1._sec;
|
---|
215 | }
|
---|
216 | else {
|
---|
217 | return this->_sec - time1._sec;
|
---|
218 | }
|
---|
219 | }
|
---|
220 |
|
---|
221 | bool t_time::operator<(const t_time &time1) const {
|
---|
222 | if ( ((*this) - time1) < 0 ) return 1;
|
---|
223 | return 0;
|
---|
224 | }
|
---|
225 |
|
---|
226 | bool t_time::operator<=(const t_time &time1) const {
|
---|
227 | if ( ((*this) - time1) <= 0 ) return 1;
|
---|
228 | return 0;
|
---|
229 | }
|
---|
230 |
|
---|
231 | bool t_time::operator>(const t_time &time1) const {
|
---|
232 | if ( ((*this) - time1) > 0 ) return 1;
|
---|
233 | return 0;
|
---|
234 | }
|
---|
235 |
|
---|
236 | bool t_time::operator>=(const t_time &time1) const {
|
---|
237 | if ( ((*this) - time1) >= 0 ) return 1;
|
---|
238 | return 0;
|
---|
239 | }
|
---|
240 |
|
---|
241 | bool t_time::operator==(const t_time &time1) const {
|
---|
242 | if ( ((*this) - time1) == 0 ) return 1;
|
---|
243 | return 0;
|
---|
244 | }
|
---|
245 |
|
---|
246 | bool t_time::operator!=(const t_time &time1) const {
|
---|
247 | if ( ((*this) - time1) != 0 ) return 1;
|
---|
248 | return 0;
|
---|
249 | }
|
---|
250 |
|
---|
251 | void t_time::civil_datum(unsigned int &year,
|
---|
252 | unsigned int &month, unsigned int &day) const {
|
---|
253 | double day_d;
|
---|
254 | long int yy, mm;
|
---|
255 | jmt(_mjd, yy, mm, day_d);
|
---|
256 | year = yy;
|
---|
257 | month = mm;
|
---|
258 | day = static_cast<unsigned int>(day_d);
|
---|
259 | }
|
---|
260 |
|
---|
261 | void t_time::civil_datum(int &year, int &month, int &day) const {
|
---|
262 | unsigned int yy = year;
|
---|
263 | unsigned int mm = month;
|
---|
264 | unsigned int dd = day;
|
---|
265 |
|
---|
266 | civil_datum(yy, mm, dd);
|
---|
267 |
|
---|
268 | year = yy; month = mm; day = dd;
|
---|
269 | }
|
---|
270 |
|
---|
271 | void t_time::civil_time(unsigned int &hour, unsigned int &min,
|
---|
272 | double &sec) const {
|
---|
273 | hour = static_cast<unsigned int>(_sec/3600.0);
|
---|
274 | min = static_cast<unsigned int>((_sec - hour*3600)/60.0);
|
---|
275 | sec = _sec - min*60 - hour*3600;
|
---|
276 | if (sec==60.0) {
|
---|
277 | min++;
|
---|
278 | sec=0;
|
---|
279 | }
|
---|
280 | if (min==60) {
|
---|
281 | hour++;
|
---|
282 | min=0;
|
---|
283 | }
|
---|
284 | }
|
---|
285 |
|
---|
286 | void t_time::civil_time(int &hour, int &min, double &sec) const {
|
---|
287 | unsigned int hh, mm;
|
---|
288 | hh = hour;
|
---|
289 | mm = min;
|
---|
290 |
|
---|
291 | civil_time(hh, mm, sec);
|
---|
292 |
|
---|
293 | hour = hh; min = mm;
|
---|
294 | }
|
---|
295 |
|
---|
296 | unsigned int t_time::year() const {
|
---|
297 | unsigned int year, month, day;
|
---|
298 | this->civil_datum(year, month, day);
|
---|
299 | return year;
|
---|
300 | }
|
---|
301 |
|
---|
302 | unsigned int t_time::month() const {
|
---|
303 | unsigned int year, month, day;
|
---|
304 | this->civil_datum(year, month, day);
|
---|
305 | return month;
|
---|
306 | }
|
---|
307 |
|
---|
308 | unsigned int t_time::day() const {
|
---|
309 | unsigned int year, month, day;
|
---|
310 | this->civil_datum(year, month, day);
|
---|
311 | return (unsigned int)day;
|
---|
312 | }
|
---|
313 |
|
---|
314 | unsigned int t_time::hour() const {
|
---|
315 | unsigned int hour, minute;
|
---|
316 | double sec;
|
---|
317 | this->civil_time(hour, minute, sec);
|
---|
318 | return hour;
|
---|
319 | }
|
---|
320 |
|
---|
321 | unsigned int t_time::minute() const {
|
---|
322 | unsigned int hour, minute;
|
---|
323 | double sec;
|
---|
324 | this->civil_time(hour, minute, sec);
|
---|
325 | return minute;
|
---|
326 | }
|
---|
327 |
|
---|
328 | double t_time::sec() const {
|
---|
329 | unsigned int hour, minute;
|
---|
330 | double sec;
|
---|
331 | this->civil_time(hour, minute, sec);
|
---|
332 | return sec;
|
---|
333 | }
|
---|
334 |
|
---|
335 | unsigned int t_time::doy() const {
|
---|
336 | return _mjd - (int)djul(this->year(), 1, 1) + 1;
|
---|
337 | }
|
---|
338 |
|
---|
339 | unsigned int t_time::mjd() const {
|
---|
340 | return _mjd;
|
---|
341 | }
|
---|
342 |
|
---|
343 | double t_time::mjddec() const {
|
---|
344 | return _mjd + _sec / 86400.0;
|
---|
345 | }
|
---|
346 |
|
---|
347 | double t_time::daysec() const {
|
---|
348 | return _sec;
|
---|
349 | }
|
---|
350 |
|
---|
351 | unsigned int t_time::gpsw() const {
|
---|
352 | double gsec;
|
---|
353 | long gpsw;
|
---|
354 | jdgp(_mjd, gsec, gpsw);
|
---|
355 | return (int)gpsw;
|
---|
356 | }
|
---|
357 |
|
---|
358 | unsigned int t_time::dow() const {
|
---|
359 | double gsec;
|
---|
360 | long gpsw;
|
---|
361 | jdgp(_mjd+_sec/86400.0, gsec, gpsw);
|
---|
362 | return (unsigned int)(gsec/86400.0);
|
---|
363 | }
|
---|
364 |
|
---|
365 | double t_time::gpssec() const {
|
---|
366 | double gsec;
|
---|
367 | long gpsw;
|
---|
368 | jdgp(_mjd, gsec, gpsw);
|
---|
369 | return gsec + _sec;
|
---|
370 | }
|
---|
371 |
|
---|
372 | t_time::operator string() const {
|
---|
373 | return datestr() + ' ' + timestr();
|
---|
374 | }
|
---|
375 |
|
---|
376 | string t_time::timestr(unsigned numdec, char sep) const {
|
---|
377 | ostringstream str;
|
---|
378 | unsigned int hour, minute;
|
---|
379 | double sec;
|
---|
380 | this->civil_time(hour, minute, sec);
|
---|
381 | unsigned sw;
|
---|
382 | if (numdec == 0) {
|
---|
383 | sw = 2;
|
---|
384 | }
|
---|
385 | else {
|
---|
386 | sw = numdec + 3;
|
---|
387 | }
|
---|
388 | double chk = 0.5;
|
---|
389 | for (unsigned int ii=0; ii<numdec; ii++) chk *= 0.1;
|
---|
390 | if (sec > (60.0-chk)) {
|
---|
391 | sec = 0;
|
---|
392 | minute++;
|
---|
393 | if (minute == 60) {
|
---|
394 | minute = 0;
|
---|
395 | hour++;
|
---|
396 | }
|
---|
397 | }
|
---|
398 | str.setf(ios::fixed);
|
---|
399 | str << setfill('0');
|
---|
400 | str << setw(2) << hour;
|
---|
401 | if (sep) str << sep;
|
---|
402 | str << setw(2) << minute;
|
---|
403 | if (sep) str << sep;
|
---|
404 | str << setw(sw) << setprecision(numdec) << sec;
|
---|
405 | return str.str();
|
---|
406 | }
|
---|
407 |
|
---|
408 | string t_time::datestr(bool digit2year, char sep) const {
|
---|
409 | ostringstream str;
|
---|
410 | str.setf(ios::fixed);
|
---|
411 | str << setfill('0');
|
---|
412 | if (digit2year) {
|
---|
413 | str << setw(2) << (year() % 100);
|
---|
414 | }
|
---|
415 | else {
|
---|
416 | str << setw(4) << year();
|
---|
417 | }
|
---|
418 | if (sep) str << sep;
|
---|
419 | str << setw(2) << month();
|
---|
420 | if (sep) str << sep;
|
---|
421 | str << setw(2) << day();
|
---|
422 | return str.str();
|
---|
423 | }
|
---|