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 |
|
---|
12 | using namespace std;
|
---|
13 |
|
---|
14 | // Constructor
|
---|
15 | //////////////////////////////////////////////////////////////////////////////
|
---|
16 | bncTime::bncTime(int gpsw, double gpssec) {
|
---|
17 | this->set(gpsw, gpssec);
|
---|
18 | }
|
---|
19 |
|
---|
20 | // Constructor (from ISO String yyyy-mm-ddThh:mm:ss)
|
---|
21 | //////////////////////////////////////////////////////////////////////////////
|
---|
22 | bncTime::bncTime(const std::string& isoString) {
|
---|
23 | if (!isoString.empty()) {
|
---|
24 | QDateTime dateTime = QDateTime::fromString(isoString.c_str(), Qt::ISODate);
|
---|
25 | }
|
---|
26 | else {
|
---|
27 | this->reset();
|
---|
28 | }
|
---|
29 | }
|
---|
30 |
|
---|
31 | //
|
---|
32 | //////////////////////////////////////////////////////////////////////////////
|
---|
33 | bncTime& bncTime::set(int gpsw, double gpssec) {
|
---|
34 | int deltad;
|
---|
35 | int dow = 0;
|
---|
36 | while ( gpssec >= 86400 ) {
|
---|
37 | gpssec-=86400;
|
---|
38 | dow++;
|
---|
39 | }
|
---|
40 | while ( gpssec < 0 ) {
|
---|
41 | gpssec+=86400;
|
---|
42 | dow--;
|
---|
43 | }
|
---|
44 | deltad = gpsw*7 + dow;
|
---|
45 | _mjd = 44244 + deltad;
|
---|
46 | _sec = gpssec;
|
---|
47 | return *this;
|
---|
48 | }
|
---|
49 |
|
---|
50 | //
|
---|
51 | //////////////////////////////////////////////////////////////////////////////
|
---|
52 | bncTime& bncTime::setmjd(double daysec, int mjd) {
|
---|
53 | _sec = daysec;
|
---|
54 | _mjd = mjd;
|
---|
55 | while ( _sec >= 86400 ) {
|
---|
56 | _sec-=86400;
|
---|
57 | _mjd++;
|
---|
58 | }
|
---|
59 | while ( _sec < 0 ) {
|
---|
60 | _sec+=86400;
|
---|
61 | _mjd--;
|
---|
62 | }
|
---|
63 | return *this;
|
---|
64 | }
|
---|
65 |
|
---|
66 | //
|
---|
67 | //////////////////////////////////////////////////////////////////////////////
|
---|
68 | unsigned int bncTime::mjd() const {
|
---|
69 | return _mjd;
|
---|
70 | }
|
---|
71 |
|
---|
72 | //
|
---|
73 | //////////////////////////////////////////////////////////////////////////////
|
---|
74 | double bncTime::daysec() const {
|
---|
75 | return _sec;
|
---|
76 | }
|
---|
77 |
|
---|
78 | //
|
---|
79 | //////////////////////////////////////////////////////////////////////////////
|
---|
80 | unsigned int bncTime::gpsw() const {
|
---|
81 | double gsec;
|
---|
82 | long gpsw;
|
---|
83 | jdgp(_mjd, gsec, gpsw);
|
---|
84 | return (int)gpsw;
|
---|
85 | }
|
---|
86 |
|
---|
87 | //
|
---|
88 | //////////////////////////////////////////////////////////////////////////////
|
---|
89 | double bncTime::gpssec() const {
|
---|
90 | double gsec;
|
---|
91 | long gpsw;
|
---|
92 | jdgp(_mjd, gsec, gpsw);
|
---|
93 | return gsec + _sec;
|
---|
94 | }
|
---|
95 |
|
---|
96 | //
|
---|
97 | //////////////////////////////////////////////////////////////////////////////
|
---|
98 | bool bncTime::operator!=(const bncTime &time1) const {
|
---|
99 | if ( ((*this) - time1) != 0.0 ) {
|
---|
100 | return true;
|
---|
101 | }
|
---|
102 | else {
|
---|
103 | return false;
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | //
|
---|
108 | //////////////////////////////////////////////////////////////////////////////
|
---|
109 | bool 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 | //////////////////////////////////////////////////////////////////////////////
|
---|
120 | bool 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 | //////////////////////////////////////////////////////////////////////////////
|
---|
131 | bool 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 | //////////////////////////////////////////////////////////////////////////////
|
---|
142 | bool 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 | //////////////////////////////////////////////////////////////////////////////
|
---|
153 | bool 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 | //////////////////////////////////////////////////////////////////////////////
|
---|
164 | bncTime bncTime::operator+(double sec) const {
|
---|
165 | int mjd = this->mjd();
|
---|
166 | double daysec = this->daysec();
|
---|
167 | daysec+=sec;
|
---|
168 | return bncTime().setmjd(daysec, mjd);
|
---|
169 | }
|
---|
170 |
|
---|
171 | //
|
---|
172 | //////////////////////////////////////////////////////////////////////////////
|
---|
173 | bncTime bncTime::operator-(double sec) const {
|
---|
174 | return (*this) + (-sec);
|
---|
175 | }
|
---|
176 |
|
---|
177 | //
|
---|
178 | //////////////////////////////////////////////////////////////////////////////
|
---|
179 | double bncTime::operator-(const bncTime &time1) const {
|
---|
180 | int mjdDiff = this->_mjd - time1._mjd;
|
---|
181 | if ( mjdDiff != 0 ) {
|
---|
182 | return mjdDiff * 86400.0 + this->_sec - time1._sec;
|
---|
183 | }
|
---|
184 | else {
|
---|
185 | return this->_sec - time1._sec;
|
---|
186 | }
|
---|
187 | }
|
---|
188 |
|
---|
189 | //
|
---|
190 | //////////////////////////////////////////////////////////////////////////////
|
---|
191 | void bncTime::civil_date (unsigned int& year, unsigned int& month,
|
---|
192 | unsigned int& day) const {
|
---|
193 | double day_d;
|
---|
194 | long int yy, mm;
|
---|
195 | jmt(_mjd, yy, mm, day_d);
|
---|
196 | year = yy;
|
---|
197 | month = mm;
|
---|
198 | day = static_cast<unsigned int>(day_d);
|
---|
199 | }
|
---|
200 |
|
---|
201 | //
|
---|
202 | //////////////////////////////////////////////////////////////////////////////
|
---|
203 | void bncTime::civil_time(unsigned int &hour, unsigned int &min,
|
---|
204 | double &sec) const {
|
---|
205 | hour = static_cast<unsigned int>(_sec/3600.0);
|
---|
206 | min = static_cast<unsigned int>((_sec - hour*3600)/60.0);
|
---|
207 | sec = _sec - min*60 - hour*3600;
|
---|
208 | if (sec==60.0) {
|
---|
209 | min++;
|
---|
210 | sec=0;
|
---|
211 | }
|
---|
212 | if (min==60) {
|
---|
213 | hour++;
|
---|
214 | min=0;
|
---|
215 | }
|
---|
216 | }
|
---|
217 |
|
---|
218 | //
|
---|
219 | //////////////////////////////////////////////////////////////////////////////
|
---|
220 | string bncTime::timestr(unsigned numdec, char sep) const {
|
---|
221 | ostringstream str;
|
---|
222 | unsigned int hour, minute;
|
---|
223 | double sec;
|
---|
224 | this->civil_time(hour, minute, sec);
|
---|
225 | unsigned sw;
|
---|
226 | if (numdec == 0) {
|
---|
227 | sw = 2;
|
---|
228 | }
|
---|
229 | else {
|
---|
230 | sw = numdec + 3;
|
---|
231 | }
|
---|
232 | double chk = 0.5;
|
---|
233 | for (unsigned int ii=0; ii<numdec; ii++) chk *= 0.1;
|
---|
234 | if (sec > (60.0-chk)) {
|
---|
235 | sec = 0;
|
---|
236 | minute++;
|
---|
237 | if (minute == 60) {
|
---|
238 | minute = 0;
|
---|
239 | hour++;
|
---|
240 | }
|
---|
241 | }
|
---|
242 | str.setf(ios::fixed);
|
---|
243 | str << setfill('0');
|
---|
244 | str << setw(2) << hour;
|
---|
245 | if (sep) str << sep;
|
---|
246 | str << setw(2) << minute;
|
---|
247 | if (sep) str << sep;
|
---|
248 | str << setw(sw) << setprecision(numdec) << sec;
|
---|
249 | return str.str();
|
---|
250 | }
|
---|
251 |
|
---|
252 | //
|
---|
253 | //////////////////////////////////////////////////////////////////////////////
|
---|
254 | string bncTime::datestr(char sep) const {
|
---|
255 | unsigned int year, month, day;
|
---|
256 | civil_date(year,month,day);
|
---|
257 | ostringstream str;
|
---|
258 | str.setf(ios::fixed);
|
---|
259 | str << setfill('0');
|
---|
260 | str << setw(4) << year;
|
---|
261 | if (sep) str << sep;
|
---|
262 | str << setw(2) << month;
|
---|
263 | if (sep) str << sep;
|
---|
264 | str << setw(2) << day;
|
---|
265 | return str.str();
|
---|
266 | }
|
---|
267 |
|
---|
268 | //
|
---|
269 | //////////////////////////////////////////////////////////////////////////////
|
---|
270 | bncTime& bncTime::set(int year, int month, int day,
|
---|
271 | int hour, int min, double sec) {
|
---|
272 | return set(year, month, day, hour*3600 + min*60 + sec);
|
---|
273 | }
|
---|
274 |
|
---|
275 | //
|
---|
276 | //////////////////////////////////////////////////////////////////////////////
|
---|
277 | bncTime& bncTime::set(int year, int month, int day, double daysec) {
|
---|
278 | _sec = daysec;
|
---|
279 |
|
---|
280 | _mjd = (unsigned int)djul(year, month, day);
|
---|
281 |
|
---|
282 | while ( _sec >= 86400 ) {
|
---|
283 | _sec-=86400;
|
---|
284 | _mjd++;
|
---|
285 | }
|
---|
286 | while ( _sec < 0 ) {
|
---|
287 | _sec+=86400;
|
---|
288 | _mjd--;
|
---|
289 | }
|
---|
290 |
|
---|
291 | return *this;
|
---|
292 | }
|
---|