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

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