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

Last change on this file since 10524 was 8718, checked in by stuerze, 6 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//
142//////////////////////////////////////////////////////////////////////////////
143bncTime& bncTime::setmjd(double daysec, int mjd) {
144 _sec = daysec;
145 _mjd = mjd;
146 while ( _sec >= 86400.0 ) {
147 _sec-=86400.0;
148 _mjd++;
149 }
150 while ( _sec < 0.0 ) {
151 _sec+=86400.0;
152 _mjd--;
153 }
154 return *this;
155}
156
157//
158//////////////////////////////////////////////////////////////////////////////
159bncTime& bncTime::setmjd(double mjddec) {
160 _mjd = static_cast<unsigned int>(mjddec);
161 _sec = (mjddec - _mjd)*86400.0;
162 return *this;
163}
164
165//
166//////////////////////////////////////////////////////////////////////////////
167unsigned int bncTime::mjd() const {
168 return _mjd;
169}
170
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
186//
187//////////////////////////////////////////////////////////////////////////////
188double bncTime::gpssec() const {
189 double gsec;
190 long gpsw;
191 jdgp(_mjd, gsec, gpsw);
192 return gsec + _sec;
193}
194
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
206//
207//////////////////////////////////////////////////////////////////////////////
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
219//
220//////////////////////////////////////////////////////////////////////////////
221bool bncTime::operator!=(const bncTime &time1) const {
222 if ( fabs((*this) - time1) > 0.000000000001 ) {
223 return true;
224 }
225 else {
226 return false;
227 }
228}
229
230//
231//////////////////////////////////////////////////////////////////////////////
232bool bncTime::operator==(const bncTime &time1) const {
233 if ( fabs((*this) - time1) < 0.000000000001 ) {
234 return true;
235 }
236 else {
237 return false;
238 }
239}
240
241//
242//////////////////////////////////////////////////////////////////////////////
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
252//
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
263//
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
274//
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
285//
286//////////////////////////////////////////////////////////////////////////////
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
294//
295//////////////////////////////////////////////////////////////////////////////
296bncTime bncTime::operator-(double sec) const {
297 return (*this) + (-sec);
298}
299
300//
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
312bncTime& bncTime::operator+=(double sec) {
313 _sec+=sec;
314
315 while ( _sec >= 86400.0 ) {
316 _sec-=86400.0;
317 _mjd++;
318 }
319 while ( _sec < 0.0 ) {
320 _sec+=86400.0;
321 _mjd--;
322 }
323
324 return *this;
325}
326
327//
328//////////////////////////////////////////////////////////////////////////////
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
339//
340//////////////////////////////////////////////////////////////////////////////
341void bncTime::civil_time(unsigned int &hour, unsigned int &min,
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
356//
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
390//
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}
405
406//
407//////////////////////////////////////////////////////////////////////////////
408bncTime::operator std::string() const {
409 return datestr() + '_' + timestr();
410}
411
412//
413//////////////////////////////////////////////////////////////////////////////
414bncTime& bncTime::set(int year, int month, int day,
415 int hour, int min, double sec) {
416 return set(year, month, day, hour*3600 + min*60 + sec);
417}
418
419//
420//////////////////////////////////////////////////////////////////////////////
421bncTime& bncTime::setBDS(int year, int month, int day,
422 int hour, int min, double sec) {
423 return set(year, month, day, hour*3600 + min*60 + sec+14.0);
424}
425
426//
427//////////////////////////////////////////////////////////////////////////////
428bncTime& bncTime::set(int year, int month, int day, double daysec) {
429 _sec = daysec;
430
431 _mjd = (unsigned int)djul(year, month, day);
432
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.