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

Last change on this file since 2917 was 2917, checked in by mervart, 13 years ago
File size: 5.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 ) return 1;
87 return 0;
88}
89
90//
91//////////////////////////////////////////////////////////////////////////////
92bool bncTime::operator==(const bncTime &time1) const {
93 if ( ((*this) - time1) == 0 ) return 1;
94 return 0;
95}
96
97//
98//////////////////////////////////////////////////////////////////////////////
99bncTime bncTime::operator+(double sec) const {
100 int mjd = this->mjd();
101 double daysec = this->daysec();
102 daysec+=sec;
103 return bncTime().setmjd(daysec, mjd);
104}
105
106//
107//////////////////////////////////////////////////////////////////////////////
108bncTime bncTime::operator-(double sec) const {
109 return (*this) + (-sec);
110}
111
112//
113//////////////////////////////////////////////////////////////////////////////
114double bncTime::operator-(const bncTime &time1) const {
115 int mjdDiff = this->_mjd - time1._mjd;
116 if ( mjdDiff != 0 ) {
117 return mjdDiff * 86400.0 + this->_sec - time1._sec;
118 }
119 else {
120 return this->_sec - time1._sec;
121 }
122}
123
124//
125//////////////////////////////////////////////////////////////////////////////
126void bncTime::civil_date (unsigned int& year, unsigned int& month,
127 unsigned int& day) const {
128 double day_d;
129 long int yy, mm;
130 jmt(_mjd, yy, mm, day_d);
131 year = yy;
132 month = mm;
133 day = static_cast<unsigned int>(day_d);
134}
135
136//
137//////////////////////////////////////////////////////////////////////////////
138void bncTime::civil_time(unsigned int &hour, unsigned int &min,
139 double &sec) const {
140 hour = static_cast<unsigned int>(_sec/3600.0);
141 min = static_cast<unsigned int>((_sec - hour*3600)/60.0);
142 sec = _sec - min*60 - hour*3600;
143 if (sec==60.0) {
144 min++;
145 sec=0;
146 }
147 if (min==60) {
148 hour++;
149 min=0;
150 }
151}
152
153//
154//////////////////////////////////////////////////////////////////////////////
155string bncTime::timestr(unsigned numdec, char sep) const {
156 ostringstream str;
157 unsigned int hour, minute;
158 double sec;
159 this->civil_time(hour, minute, sec);
160 unsigned sw;
161 if (numdec == 0) {
162 sw = 2;
163 }
164 else {
165 sw = numdec + 3;
166 }
167 double chk = 0.5;
168 for (unsigned int ii=0; ii<numdec; ii++) chk *= 0.1;
169 if (sec > (60.0-chk)) {
170 sec = 0;
171 minute++;
172 if (minute == 60) {
173 minute = 0;
174 hour++;
175 }
176 }
177 str.setf(ios::fixed);
178 str << setfill('0');
179 str << setw(2) << hour;
180 if (sep) str << sep;
181 str << setw(2) << minute;
182 if (sep) str << sep;
183 str << setw(sw) << setprecision(numdec) << sec;
184 return str.str();
185}
186
187//
188//////////////////////////////////////////////////////////////////////////////
189string bncTime::datestr(char sep) const {
190 unsigned int year, month, day;
191 civil_date(year,month,day);
192 ostringstream str;
193 str.setf(ios::fixed);
194 str << setfill('0');
195 str << setw(4) << year;
196 if (sep) str << sep;
197 str << setw(2) << month;
198 if (sep) str << sep;
199 str << setw(2) << day;
200 return str.str();
201}
202
203//
204//////////////////////////////////////////////////////////////////////////////
205bncTime& bncTime::set(int year, int month, int day,
206 int hour, int min, double sec) {
207 return set(year, month, day, hour*3600 + min*60 + sec);
208}
209
210//
211//////////////////////////////////////////////////////////////////////////////
212bncTime& bncTime::set(int year, int month, int day, double daysec) {
213 _sec = daysec;
214
215 _mjd = (unsigned int)djul(year, month, day);
216
217 while ( _sec >= 86400 ) {
218 _sec-=86400;
219 _mjd++;
220 }
221 while ( _sec < 0 ) {
222 _sec+=86400;
223 _mjd--;
224 }
225
226 return *this;
227}
Note: See TracBrowser for help on using the repository browser.