source: ntrip/trunk/BNC/bnctime.cpp@ 3986

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