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

Last change on this file since 7288 was 7138, checked in by stuerze, 9 years ago

method added to set TOE from BDS week and sec

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