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

Last change on this file since 6600 was 5886, checked in by mervart, 10 years ago
File size: 7.2 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::setmjd(double daysec, int mjd) {
56 _sec = daysec;
57 _mjd = mjd;
58 while ( _sec >= 86400 ) {
59 _sec-=86400;
60 _mjd++;
61 }
62 while ( _sec < 0 ) {
63 _sec+=86400;
64 _mjd--;
65 }
66 return *this;
67}
68
69//
70//////////////////////////////////////////////////////////////////////////////
71bncTime& bncTime::setmjd(double mjddec) {
72 _mjd = static_cast<unsigned int>(mjddec);
73 _sec = (mjddec - _mjd)*86400.0;
74 return *this;
75}
76
77//
78//////////////////////////////////////////////////////////////////////////////
79unsigned int bncTime::mjd() const {
80 return _mjd;
81}
82
83//
84//////////////////////////////////////////////////////////////////////////////
85double bncTime::daysec() const {
86 return _sec;
87}
88
89//
90//////////////////////////////////////////////////////////////////////////////
91unsigned int bncTime::gpsw() const {
92 double gsec;
93 long gpsw;
94 jdgp(_mjd, gsec, gpsw);
95 return (int)gpsw;
96}
97
98//
99//////////////////////////////////////////////////////////////////////////////
100double bncTime::gpssec() const {
101 double gsec;
102 long gpsw;
103 jdgp(_mjd, gsec, gpsw);
104 return gsec + _sec;
105}
106
107//
108//////////////////////////////////////////////////////////////////////////////
109bool bncTime::operator!=(const bncTime &time1) const {
110 if ( ((*this) - time1) != 0.0 ) {
111 return true;
112 }
113 else {
114 return false;
115 }
116}
117
118//
119//////////////////////////////////////////////////////////////////////////////
120bool bncTime::operator==(const bncTime &time1) const {
121 if ( ((*this) - time1) == 0.0 ) {
122 return true;
123 }
124 else {
125 return false;
126 }
127}
128
129//
130//////////////////////////////////////////////////////////////////////////////
131bool bncTime::operator>(const bncTime &time1) const {
132 if ( ((*this) - time1) > 0.0 ) {
133 return true;
134 }
135 else {
136 return false;
137 }
138}
139
140//
141//////////////////////////////////////////////////////////////////////////////
142bool bncTime::operator>=(const bncTime &time1) const {
143 if ( ((*this) - time1) >= 0.0 ) {
144 return true;
145 }
146 else {
147 return false;
148 }
149}
150
151//
152//////////////////////////////////////////////////////////////////////////////
153bool bncTime::operator<(const bncTime &time1) const {
154 if ( ((*this) - time1) < 0.0 ) {
155 return true;
156 }
157 else {
158 return false;
159 }
160}
161
162//
163//////////////////////////////////////////////////////////////////////////////
164bool bncTime::operator<=(const bncTime &time1) const {
165 if ( ((*this) - time1) <= 0.0 ) {
166 return true;
167 }
168 else {
169 return false;
170 }
171}
172
173//
174//////////////////////////////////////////////////////////////////////////////
175bncTime bncTime::operator+(double sec) const {
176 int mjd = this->mjd();
177 double daysec = this->daysec();
178 daysec+=sec;
179 return bncTime().setmjd(daysec, mjd);
180}
181
182//
183//////////////////////////////////////////////////////////////////////////////
184bncTime bncTime::operator-(double sec) const {
185 return (*this) + (-sec);
186}
187
188//
189//////////////////////////////////////////////////////////////////////////////
190double bncTime::operator-(const bncTime &time1) const {
191 int mjdDiff = this->_mjd - time1._mjd;
192 if ( mjdDiff != 0 ) {
193 return mjdDiff * 86400.0 + this->_sec - time1._sec;
194 }
195 else {
196 return this->_sec - time1._sec;
197 }
198}
199
200bncTime& bncTime::operator+=(double sec) {
201 _sec+=sec;
202
203 while ( _sec >= 86400 ) {
204 _sec-=86400;
205 _mjd++;
206 }
207 while ( _sec < 0 ) {
208 _sec+=86400;
209 _mjd--;
210 }
211
212 return *this;
213}
214
215//
216//////////////////////////////////////////////////////////////////////////////
217void bncTime::civil_date (unsigned int& year, unsigned int& month,
218 unsigned int& day) const {
219 double day_d;
220 long int yy, mm;
221 jmt(_mjd, yy, mm, day_d);
222 year = yy;
223 month = mm;
224 day = static_cast<unsigned int>(day_d);
225}
226
227//
228//////////////////////////////////////////////////////////////////////////////
229void bncTime::civil_time(unsigned int &hour, unsigned int &min,
230 double &sec) const {
231 hour = static_cast<unsigned int>(_sec/3600.0);
232 min = static_cast<unsigned int>((_sec - hour*3600)/60.0);
233 sec = _sec - min*60 - hour*3600;
234 if (sec==60.0) {
235 min++;
236 sec=0;
237 }
238 if (min==60) {
239 hour++;
240 min=0;
241 }
242}
243
244//
245//////////////////////////////////////////////////////////////////////////////
246string bncTime::timestr(unsigned numdec, char sep) const {
247 ostringstream str;
248 unsigned int hour, minute;
249 double sec;
250 this->civil_time(hour, minute, sec);
251 unsigned sw;
252 if (numdec == 0) {
253 sw = 2;
254 }
255 else {
256 sw = numdec + 3;
257 }
258 double chk = 0.5;
259 for (unsigned int ii=0; ii<numdec; ii++) chk *= 0.1;
260 if (sec > (60.0-chk)) {
261 sec = 0;
262 minute++;
263 if (minute == 60) {
264 minute = 0;
265 hour++;
266 }
267 }
268 str.setf(ios::fixed);
269 str << setfill('0');
270 str << setw(2) << hour;
271 if (sep) str << sep;
272 str << setw(2) << minute;
273 if (sep) str << sep;
274 str << setw(sw) << setprecision(numdec) << sec;
275 return str.str();
276}
277
278//
279//////////////////////////////////////////////////////////////////////////////
280string bncTime::datestr(char sep) const {
281 unsigned int year, month, day;
282 civil_date(year,month,day);
283 ostringstream str;
284 str.setf(ios::fixed);
285 str << setfill('0');
286 str << setw(4) << year;
287 if (sep) str << sep;
288 str << setw(2) << month;
289 if (sep) str << sep;
290 str << setw(2) << day;
291 return str.str();
292}
293
294//
295//////////////////////////////////////////////////////////////////////////////
296bncTime::operator string() const {
297 return datestr() + '_' + timestr();
298}
299
300//
301//////////////////////////////////////////////////////////////////////////////
302bncTime& bncTime::set(int year, int month, int day,
303 int hour, int min, double sec) {
304 return set(year, month, day, hour*3600 + min*60 + sec);
305}
306
307//
308//////////////////////////////////////////////////////////////////////////////
309bncTime& bncTime::set(int year, int month, int day, double daysec) {
310 _sec = daysec;
311
312 _mjd = (unsigned int)djul(year, month, day);
313
314 while ( _sec >= 86400 ) {
315 _sec-=86400;
316 _mjd++;
317 }
318 while ( _sec < 0 ) {
319 _sec+=86400;
320 _mjd--;
321 }
322
323 return *this;
324}
Note: See TracBrowser for help on using the repository browser.