source: ntrip/branches/BNC_2.12/src/bnctime.cpp@ 9671

Last change on this file since 9671 was 8717, checked in by stuerze, 5 years ago

minor changes

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