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

Last change on this file since 6812 was 6812, checked in by stoecker, 9 years ago

integrate RTCM3 parsing into BNC and directly fill target structures, add doxygen documentation

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