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

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