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

Last change on this file since 5203 was 4586, checked in by mervart, 12 years ago
File size: 6.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
9#include "bnctime.h"
10#include "timeutils.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
200//
201//////////////////////////////////////////////////////////////////////////////
202void bncTime::civil_date (unsigned int& year, unsigned int& month,
203 unsigned int& day) const {
204 double day_d;
205 long int yy, mm;
206 jmt(_mjd, yy, mm, day_d);
207 year = yy;
208 month = mm;
209 day = static_cast<unsigned int>(day_d);
210}
211
212//
213//////////////////////////////////////////////////////////////////////////////
214void bncTime::civil_time(unsigned int &hour, unsigned int &min,
215 double &sec) const {
216 hour = static_cast<unsigned int>(_sec/3600.0);
217 min = static_cast<unsigned int>((_sec - hour*3600)/60.0);
218 sec = _sec - min*60 - hour*3600;
219 if (sec==60.0) {
220 min++;
221 sec=0;
222 }
223 if (min==60) {
224 hour++;
225 min=0;
226 }
227}
228
229//
230//////////////////////////////////////////////////////////////////////////////
231string bncTime::timestr(unsigned numdec, char sep) const {
232 ostringstream str;
233 unsigned int hour, minute;
234 double sec;
235 this->civil_time(hour, minute, sec);
236 unsigned sw;
237 if (numdec == 0) {
238 sw = 2;
239 }
240 else {
241 sw = numdec + 3;
242 }
243 double chk = 0.5;
244 for (unsigned int ii=0; ii<numdec; ii++) chk *= 0.1;
245 if (sec > (60.0-chk)) {
246 sec = 0;
247 minute++;
248 if (minute == 60) {
249 minute = 0;
250 hour++;
251 }
252 }
253 str.setf(ios::fixed);
254 str << setfill('0');
255 str << setw(2) << hour;
256 if (sep) str << sep;
257 str << setw(2) << minute;
258 if (sep) str << sep;
259 str << setw(sw) << setprecision(numdec) << sec;
260 return str.str();
261}
262
263//
264//////////////////////////////////////////////////////////////////////////////
265string bncTime::datestr(char sep) const {
266 unsigned int year, month, day;
267 civil_date(year,month,day);
268 ostringstream str;
269 str.setf(ios::fixed);
270 str << setfill('0');
271 str << setw(4) << year;
272 if (sep) str << sep;
273 str << setw(2) << month;
274 if (sep) str << sep;
275 str << setw(2) << day;
276 return str.str();
277}
278
279//
280//////////////////////////////////////////////////////////////////////////////
281bncTime& bncTime::set(int year, int month, int day,
282 int hour, int min, double sec) {
283 return set(year, month, day, hour*3600 + min*60 + sec);
284}
285
286//
287//////////////////////////////////////////////////////////////////////////////
288bncTime& bncTime::set(int year, int month, int day, double daysec) {
289 _sec = daysec;
290
291 _mjd = (unsigned int)djul(year, month, day);
292
293 while ( _sec >= 86400 ) {
294 _sec-=86400;
295 _mjd++;
296 }
297 while ( _sec < 0 ) {
298 _sec+=86400;
299 _mjd--;
300 }
301
302 return *this;
303}
Note: See TracBrowser for help on using the repository browser.