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

Last change on this file since 10132 was 8718, checked in by stuerze, 6 years ago

minor changes

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);
201 if(gsec <= 14.0)
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);
212 if(gsec <= 14.0)
213 gsec += 7.0*24.0*60.0*60.0-14.0;
214 else
215 gsec -= 14.0;
216 return gsec + _sec;
217}
218
[8372]219//
[6812]220//////////////////////////////////////////////////////////////////////////////
[2123]221bool bncTime::operator!=(const bncTime &time1) const {
[6812]222 if ( fabs((*this) - time1) > 0.000000000001 ) {
[2923]223 return true;
224 }
225 else {
226 return false;
227 }
[2123]228}
229
[8372]230//
[2123]231//////////////////////////////////////////////////////////////////////////////
[2917]232bool bncTime::operator==(const bncTime &time1) const {
[6812]233 if ( fabs((*this) - time1) < 0.000000000001 ) {
[2923]234 return true;
235 }
236 else {
237 return false;
238 }
[2917]239}
240
[8372]241//
[2917]242//////////////////////////////////////////////////////////////////////////////
[2923]243bool bncTime::operator>(const bncTime &time1) const {
244 if ( ((*this) - time1) > 0.0 ) {
245 return true;
246 }
247 else {
248 return false;
249 }
250}
251
[8372]252//
[2923]253//////////////////////////////////////////////////////////////////////////////
254bool bncTime::operator>=(const bncTime &time1) const {
255 if ( ((*this) - time1) >= 0.0 ) {
256 return true;
257 }
258 else {
259 return false;
260 }
261}
262
[8372]263//
[2923]264//////////////////////////////////////////////////////////////////////////////
265bool bncTime::operator<(const bncTime &time1) const {
266 if ( ((*this) - time1) < 0.0 ) {
267 return true;
268 }
269 else {
270 return false;
271 }
272}
273
[8372]274//
[2923]275//////////////////////////////////////////////////////////////////////////////
276bool bncTime::operator<=(const bncTime &time1) const {
277 if ( ((*this) - time1) <= 0.0 ) {
278 return true;
279 }
280 else {
281 return false;
282 }
283}
284
[8372]285//
[2923]286//////////////////////////////////////////////////////////////////////////////
[2123]287bncTime bncTime::operator+(double sec) const {
288 int mjd = this->mjd();
289 double daysec = this->daysec();
290 daysec+=sec;
291 return bncTime().setmjd(daysec, mjd);
292}
293
[8372]294//
[2123]295//////////////////////////////////////////////////////////////////////////////
296bncTime bncTime::operator-(double sec) const {
297 return (*this) + (-sec);
298}
299
[8372]300//
[2123]301//////////////////////////////////////////////////////////////////////////////
302double bncTime::operator-(const bncTime &time1) const {
303 int mjdDiff = this->_mjd - time1._mjd;
304 if ( mjdDiff != 0 ) {
305 return mjdDiff * 86400.0 + this->_sec - time1._sec;
306 }
307 else {
308 return this->_sec - time1._sec;
309 }
310}
311
[5756]312bncTime& bncTime::operator+=(double sec) {
313 _sec+=sec;
314
[8718]315 while ( _sec >= 86400.0 ) {
316 _sec-=86400.0;
[5756]317 _mjd++;
318 }
[8718]319 while ( _sec < 0.0 ) {
320 _sec+=86400.0;
[5756]321 _mjd--;
322 }
323
324 return *this;
325}
326
[8372]327//
[2123]328//////////////////////////////////////////////////////////////////////////////
[2566]329void bncTime::civil_date (unsigned int& year, unsigned int& month,
330 unsigned int& day) const {
331 double day_d;
332 long int yy, mm;
333 jmt(_mjd, yy, mm, day_d);
334 year = yy;
335 month = mm;
336 day = static_cast<unsigned int>(day_d);
337}
338
[8372]339//
[2566]340//////////////////////////////////////////////////////////////////////////////
[8372]341void bncTime::civil_time(unsigned int &hour, unsigned int &min,
[2123]342 double &sec) const {
343 hour = static_cast<unsigned int>(_sec/3600.0);
344 min = static_cast<unsigned int>((_sec - hour*3600)/60.0);
345 sec = _sec - min*60 - hour*3600;
346 if (sec==60.0) {
347 min++;
348 sec=0;
349 }
350 if (min==60) {
351 hour++;
352 min=0;
353 }
354}
355
[8372]356//
[2123]357//////////////////////////////////////////////////////////////////////////////
358string bncTime::timestr(unsigned numdec, char sep) const {
359 ostringstream str;
360 unsigned int hour, minute;
361 double sec;
362 this->civil_time(hour, minute, sec);
363 unsigned sw;
364 if (numdec == 0) {
365 sw = 2;
366 }
367 else {
368 sw = numdec + 3;
369 }
370 double chk = 0.5;
371 for (unsigned int ii=0; ii<numdec; ii++) chk *= 0.1;
372 if (sec > (60.0-chk)) {
373 sec = 0;
374 minute++;
375 if (minute == 60) {
376 minute = 0;
377 hour++;
378 }
379 }
380 str.setf(ios::fixed);
381 str << setfill('0');
382 str << setw(2) << hour;
383 if (sep) str << sep;
384 str << setw(2) << minute;
385 if (sep) str << sep;
386 str << setw(sw) << setprecision(numdec) << sec;
387 return str.str();
388}
389
[8372]390//
[2566]391//////////////////////////////////////////////////////////////////////////////
392string bncTime::datestr(char sep) const {
393 unsigned int year, month, day;
394 civil_date(year,month,day);
395 ostringstream str;
396 str.setf(ios::fixed);
397 str << setfill('0');
398 str << setw(4) << year;
399 if (sep) str << sep;
400 str << setw(2) << month;
401 if (sep) str << sep;
402 str << setw(2) << day;
403 return str.str();
404}
[2251]405
[8372]406//
[2251]407//////////////////////////////////////////////////////////////////////////////
[6812]408bncTime::operator std::string() const {
[5756]409 return datestr() + '_' + timestr();
410}
411
[8372]412//
[5756]413//////////////////////////////////////////////////////////////////////////////
[8372]414bncTime& bncTime::set(int year, int month, int day,
[2251]415 int hour, int min, double sec) {
416 return set(year, month, day, hour*3600 + min*60 + sec);
417}
418
[8372]419//
[2251]420//////////////////////////////////////////////////////////////////////////////
[8372]421bncTime& bncTime::setBDS(int year, int month, int day,
[6812]422 int hour, int min, double sec) {
423 return set(year, month, day, hour*3600 + min*60 + sec+14.0);
424}
425
[8372]426//
[6812]427//////////////////////////////////////////////////////////////////////////////
[2251]428bncTime& bncTime::set(int year, int month, int day, double daysec) {
429 _sec = daysec;
[8372]430
[2251]431 _mjd = (unsigned int)djul(year, month, day);
[8372]432
[2251]433 while ( _sec >= 86400 ) {
434 _sec-=86400;
435 _mjd++;
436 }
437 while ( _sec < 0 ) {
438 _sec+=86400;
439 _mjd--;
440 }
441
442 return *this;
443}
Note: See TracBrowser for help on using the repository browser.