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

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