source: ntrip/trunk/BNS/bnctime.cpp@ 4981

Last change on this file since 4981 was 2258, checked in by mervart, 15 years ago

* empty log message *

File size: 4.3 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_time(unsigned int &hour, unsigned int &min,
120 double &sec) const {
121 hour = static_cast<unsigned int>(_sec/3600.0);
122 min = static_cast<unsigned int>((_sec - hour*3600)/60.0);
123 sec = _sec - min*60 - hour*3600;
124 if (sec==60.0) {
125 min++;
126 sec=0;
127 }
128 if (min==60) {
129 hour++;
130 min=0;
131 }
132}
133
134//
135//////////////////////////////////////////////////////////////////////////////
136string bncTime::timestr(unsigned numdec, char sep) const {
137 ostringstream str;
138 unsigned int hour, minute;
139 double sec;
140 this->civil_time(hour, minute, sec);
141 unsigned sw;
142 if (numdec == 0) {
143 sw = 2;
144 }
145 else {
146 sw = numdec + 3;
147 }
148 double chk = 0.5;
149 for (unsigned int ii=0; ii<numdec; ii++) chk *= 0.1;
150 if (sec > (60.0-chk)) {
151 sec = 0;
152 minute++;
153 if (minute == 60) {
154 minute = 0;
155 hour++;
156 }
157 }
158 str.setf(ios::fixed);
159 str << setfill('0');
160 str << setw(2) << hour;
161 if (sep) str << sep;
162 str << setw(2) << minute;
163 if (sep) str << sep;
164 str << setw(sw) << setprecision(numdec) << sec;
165 return str.str();
166}
167
168
169//
170//////////////////////////////////////////////////////////////////////////////
171bncTime& bncTime::set(int year, int month, int day,
172 int hour, int min, double sec) {
173 return set(year, month, day, hour*3600 + min*60 + sec);
174}
175
176//
177//////////////////////////////////////////////////////////////////////////////
178bncTime& bncTime::set(int year, int month, int day, double daysec) {
179 _sec = daysec;
180
181 _mjd = (unsigned int)djul(year, month, day);
182
183 while ( _sec >= 86400 ) {
184 _sec-=86400;
185 _mjd++;
186 }
187 while ( _sec < 0 ) {
188 _sec+=86400;
189 _mjd--;
190 }
191
192 return *this;
193}
Note: See TracBrowser for help on using the repository browser.