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

Last change on this file since 2123 was 2123, checked in by mervart, 14 years ago

* empty log message *

File size: 3.9 KB
Line 
1#include <time.h>
2#include <cmath>
3#include <cstdio>
4#include <sstream>
5#include <iomanip>
6
7#include "bnctime.h"
8
9using namespace std;
10
11// Constructor
12//////////////////////////////////////////////////////////////////////////////
13bncTime::bncTime(int gpsw, double gpssec) {
14 this->set(gpsw, gpssec);
15}
16
17//
18//////////////////////////////////////////////////////////////////////////////
19bncTime& bncTime::set(int gpsw, double gpssec) {
20 int deltad;
21 int dow = 0;
22 while ( gpssec >= 86400 ) {
23 gpssec-=86400;
24 dow++;
25 }
26 while ( gpssec < 0 ) {
27 gpssec+=86400;
28 dow--;
29 }
30 deltad = gpsw*7 + dow;
31 _mjd = 44244 + deltad;
32 _sec = gpssec;
33 return *this;
34}
35
36//
37//////////////////////////////////////////////////////////////////////////////
38bncTime& bncTime::setmjd(double daysec, int mjd) {
39 _sec = daysec;
40 _mjd = mjd;
41 while ( _sec >= 86400 ) {
42 _sec-=86400;
43 _mjd++;
44 }
45 while ( _sec < 0 ) {
46 _sec+=86400;
47 _mjd--;
48 }
49 return *this;
50}
51
52//
53//////////////////////////////////////////////////////////////////////////////
54void bncTime::jdgp(double tjul, double & second, long & nweek) {
55 double deltat;
56 deltat = tjul - 44244.0 ;
57 nweek = (long) floor(deltat/7.0);
58 second = (deltat - (nweek)*7.0)*86400.0;
59}
60
61//
62//////////////////////////////////////////////////////////////////////////////
63unsigned int bncTime::mjd() const {
64 return _mjd;
65}
66
67//
68//////////////////////////////////////////////////////////////////////////////
69double bncTime::daysec() const {
70 return _sec;
71}
72
73//
74//////////////////////////////////////////////////////////////////////////////
75unsigned int bncTime::gpsw() const {
76 double gsec;
77 long gpsw;
78 jdgp(_mjd, gsec, gpsw);
79 return (int)gpsw;
80}
81
82//
83//////////////////////////////////////////////////////////////////////////////
84double bncTime::gpssec() const {
85 double gsec;
86 long gpsw;
87 jdgp(_mjd, gsec, gpsw);
88 return gsec + _sec;
89}
90
91//
92//////////////////////////////////////////////////////////////////////////////
93bool bncTime::operator!=(const bncTime &time1) const {
94 if ( ((*this) - time1) != 0 ) return 1;
95 return 0;
96}
97
98//
99//////////////////////////////////////////////////////////////////////////////
100bncTime bncTime::operator+(double sec) const {
101 int mjd = this->mjd();
102 double daysec = this->daysec();
103 daysec+=sec;
104 return bncTime().setmjd(daysec, mjd);
105}
106
107//
108//////////////////////////////////////////////////////////////////////////////
109bncTime bncTime::operator-(double sec) const {
110 return (*this) + (-sec);
111}
112
113//
114//////////////////////////////////////////////////////////////////////////////
115double bncTime::operator-(const bncTime &time1) const {
116 int mjdDiff = this->_mjd - time1._mjd;
117 if ( mjdDiff != 0 ) {
118 return mjdDiff * 86400.0 + this->_sec - time1._sec;
119 }
120 else {
121 return this->_sec - time1._sec;
122 }
123}
124
125//
126//////////////////////////////////////////////////////////////////////////////
127void bncTime::civil_time(unsigned int &hour, unsigned int &min,
128 double &sec) const {
129 hour = static_cast<unsigned int>(_sec/3600.0);
130 min = static_cast<unsigned int>((_sec - hour*3600)/60.0);
131 sec = _sec - min*60 - hour*3600;
132 if (sec==60.0) {
133 min++;
134 sec=0;
135 }
136 if (min==60) {
137 hour++;
138 min=0;
139 }
140}
141
142//
143//////////////////////////////////////////////////////////////////////////////
144string bncTime::timestr(unsigned numdec, char sep) const {
145 ostringstream str;
146 unsigned int hour, minute;
147 double sec;
148 this->civil_time(hour, minute, sec);
149 unsigned sw;
150 if (numdec == 0) {
151 sw = 2;
152 }
153 else {
154 sw = numdec + 3;
155 }
156 double chk = 0.5;
157 for (unsigned int ii=0; ii<numdec; ii++) chk *= 0.1;
158 if (sec > (60.0-chk)) {
159 sec = 0;
160 minute++;
161 if (minute == 60) {
162 minute = 0;
163 hour++;
164 }
165 }
166 str.setf(ios::fixed);
167 str << setfill('0');
168 str << setw(2) << hour;
169 if (sep) str << sep;
170 str << setw(2) << minute;
171 if (sep) str << sep;
172 str << setw(sw) << setprecision(numdec) << sec;
173 return str.str();
174}
175
Note: See TracBrowser for help on using the repository browser.