source: ntrip/trunk/BNC/src/bnctime.cpp@ 10590

Last change on this file since 10590 was 10566, checked in by stuerze, 2 months ago

bug fixed in BDT methods

File size: 9.9 KB
RevLine 
[3986]1
2#include <qdatetime.h>
[2123]3#include <time.h>
4#include <cmath>
5#include <cstdio>
6#include <sstream>
7#include <iomanip>
[8417]8#include <iostream>
[2123]9
10#include "bnctime.h"
[5886]11#include "bncutils.h"
[2123]12
13using namespace std;
14
15// Constructor
16//////////////////////////////////////////////////////////////////////////////
17bncTime::bncTime(int gpsw, double gpssec) {
18 this->set(gpsw, gpssec);
19}
[8372]20
[3986]21// Constructor (from ISO String yyyy-mm-ddThh:mm:ss)
22//////////////////////////////////////////////////////////////////////////////
23bncTime::bncTime(const std::string& isoString) {
24 if (!isoString.empty()) {
[3988]25 QDateTime dt = QDateTime::fromString(isoString.c_str(), Qt::ISODate);
26 this->set(dt.date().year(), dt.date().month(), dt.date().day(),
[8372]27 dt.time().hour(), dt.time().minute(),
[3988]28 dt.time().second() + dt.time().msec()/1000.0);
[3986]29 }
[3987]30 else {
31 this->reset();
32 }
[3986]33}
34
[8372]35//
[2123]36//////////////////////////////////////////////////////////////////////////////
37bncTime& bncTime::set(int gpsw, double gpssec) {
38 int deltad;
39 int dow = 0;
[8372]40 while ( gpssec >= 86400.0 ) {
41 gpssec-=86400.0;
[2123]42 dow++;
43 }
[8372]44 while ( gpssec < 0.0 ) {
45 gpssec+=86400.0;
[2123]46 dow--;
47 }
48 deltad = gpsw*7 + dow;
49 _mjd = 44244 + deltad;
50 _sec = gpssec;
51 return *this;
52}
53
[7138]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;
[8372]62 while ( gpssec >= 86400.0 ) {
63 gpssec-=86400.0;
[7138]64 dow++;
65 }
[8372]66 while ( gpssec < 0.0 ) {
67 gpssec+=86400.0;
[7138]68 dow--;
69 }
70 deltad = gpsw*7 + dow;
71 _mjd = 44244 + deltad;
72 _sec = gpssec;
73 return *this;
74}
75
[8372]76//
[2123]77//////////////////////////////////////////////////////////////////////////////
[6812]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;
[8417]85 return set(week, double(msec/1000.0));
[6812]86}
87
[8372]88//
[6812]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
[8372]111//
[6812]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 */
[8417]121 sec = intsec+double((msec%1000)/1000.0);
[6812]122 return set(week, sec);
123}
124
[8372]125//
[6812]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);
[8417]135 if((msec/1000.0) < (sec - 86400.0)) {
[6812]136 ++week;
[8417]137 }
138 return set(week, double(msec/1000.0));
[6812]139}
140
[8372]141//
[6812]142//////////////////////////////////////////////////////////////////////////////
[2123]143bncTime& bncTime::setmjd(double daysec, int mjd) {
144 _sec = daysec;
145 _mjd = mjd;
[8372]146 while ( _sec >= 86400.0 ) {
147 _sec-=86400.0;
[2123]148 _mjd++;
149 }
[8372]150 while ( _sec < 0.0 ) {
151 _sec+=86400.0;
[2123]152 _mjd--;
153 }
154 return *this;
155}
156
[8372]157//
[2123]158//////////////////////////////////////////////////////////////////////////////
[4586]159bncTime& bncTime::setmjd(double mjddec) {
160 _mjd = static_cast<unsigned int>(mjddec);
161 _sec = (mjddec - _mjd)*86400.0;
162 return *this;
163}
164
[8372]165//
[4586]166//////////////////////////////////////////////////////////////////////////////
[2123]167unsigned int bncTime::mjd() const {
168 return _mjd;
169}
[8372]170
[2123]171//
172//////////////////////////////////////////////////////////////////////////////
173double bncTime::daysec() const {
174 return _sec;
175}
176
177//
178//////////////////////////////////////////////////////////////////////////////
179unsigned int bncTime::gpsw() const {
180 double gsec;
181 long gpsw;
182 jdgp(_mjd, gsec, gpsw);
183 return (int)gpsw;
184}
185
[8372]186//
[2123]187//////////////////////////////////////////////////////////////////////////////
188double bncTime::gpssec() const {
189 double gsec;
190 long gpsw;
191 jdgp(_mjd, gsec, gpsw);
192 return gsec + _sec;
193}
194
[6812]195//
196//////////////////////////////////////////////////////////////////////////////
197unsigned int bncTime::bdsw() const {
198 double gsec;
199 long gpsw;
200 jdgp(_mjd, gsec, gpsw);
[10566]201 if(_sec < 14.0)
[6812]202 gpsw -= 1;
203 return (int)gpsw-1356;
204}
205
[8372]206//
[2123]207//////////////////////////////////////////////////////////////////////////////
[6812]208double bncTime::bdssec() const {
209 double gsec;
210 long gpsw;
211 jdgp(_mjd, gsec, gpsw);
[10566]212 if(_sec < 14.0)
213 gsec += 7.0*24.0*60.0*60.0;
214 gsec -= 14.0;
[6812]215 return gsec + _sec;
216}
217
[8372]218//
[6812]219//////////////////////////////////////////////////////////////////////////////
[2123]220bool bncTime::operator!=(const bncTime &time1) const {
[6812]221 if ( fabs((*this) - time1) > 0.000000000001 ) {
[2923]222 return true;
223 }
224 else {
225 return false;
226 }
[2123]227}
228
[8372]229//
[2123]230//////////////////////////////////////////////////////////////////////////////
[2917]231bool bncTime::operator==(const bncTime &time1) const {
[6812]232 if ( fabs((*this) - time1) < 0.000000000001 ) {
[2923]233 return true;
234 }
235 else {
236 return false;
237 }
[2917]238}
239
[8372]240//
[2917]241//////////////////////////////////////////////////////////////////////////////
[2923]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
[8372]251//
[2923]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
[8372]262//
[2923]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
[8372]273//
[2923]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
[8372]284//
[2923]285//////////////////////////////////////////////////////////////////////////////
[2123]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
[8372]293//
[2123]294//////////////////////////////////////////////////////////////////////////////
295bncTime bncTime::operator-(double sec) const {
296 return (*this) + (-sec);
297}
298
[8372]299//
[2123]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
[5756]311bncTime& bncTime::operator+=(double sec) {
312 _sec+=sec;
313
[8718]314 while ( _sec >= 86400.0 ) {
315 _sec-=86400.0;
[5756]316 _mjd++;
317 }
[8718]318 while ( _sec < 0.0 ) {
319 _sec+=86400.0;
[5756]320 _mjd--;
321 }
322
323 return *this;
324}
325
[8372]326//
[2123]327//////////////////////////////////////////////////////////////////////////////
[2566]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
[8372]338//
[2566]339//////////////////////////////////////////////////////////////////////////////
[8372]340void bncTime::civil_time(unsigned int &hour, unsigned int &min,
[2123]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
[8372]355//
[2123]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
[8372]389//
[2566]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}
[2251]404
[8372]405//
[2251]406//////////////////////////////////////////////////////////////////////////////
[6812]407bncTime::operator std::string() const {
[5756]408 return datestr() + '_' + timestr();
409}
410
[8372]411//
[5756]412//////////////////////////////////////////////////////////////////////////////
[8372]413bncTime& bncTime::set(int year, int month, int day,
[2251]414 int hour, int min, double sec) {
415 return set(year, month, day, hour*3600 + min*60 + sec);
416}
417
[8372]418//
[2251]419//////////////////////////////////////////////////////////////////////////////
[8372]420bncTime& bncTime::setBDS(int year, int month, int day,
[6812]421 int hour, int min, double sec) {
422 return set(year, month, day, hour*3600 + min*60 + sec+14.0);
423}
424
[8372]425//
[6812]426//////////////////////////////////////////////////////////////////////////////
[2251]427bncTime& bncTime::set(int year, int month, int day, double daysec) {
428 _sec = daysec;
[8372]429
[2251]430 _mjd = (unsigned int)djul(year, month, day);
[8372]431
[2251]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.