[2123] | 1 | #include <time.h>
|
---|
| 2 | #include <cmath>
|
---|
| 3 | #include <cstdio>
|
---|
| 4 | #include <sstream>
|
---|
| 5 | #include <iomanip>
|
---|
| 6 |
|
---|
| 7 | #include "bnctime.h"
|
---|
| 8 |
|
---|
| 9 | using namespace std;
|
---|
| 10 |
|
---|
[2251] | 11 | double djul(long jj, long mm, double tt) {
|
---|
| 12 | long ii, kk;
|
---|
| 13 | double djul ;
|
---|
| 14 | if( mm <= 2 ) {
|
---|
| 15 | jj = jj - 1;
|
---|
| 16 | mm = mm + 12;
|
---|
| 17 | }
|
---|
| 18 | ii = jj/100;
|
---|
| 19 | kk = 2 - ii + ii/4;
|
---|
| 20 | djul = (365.25*jj - fmod( 365.25*jj, 1.0 )) - 679006.0;
|
---|
| 21 | djul = djul + floor( 30.6001*(mm + 1) ) + tt + kk;
|
---|
| 22 | return djul;
|
---|
| 23 | }
|
---|
| 24 |
|
---|
[2123] | 25 | // Constructor
|
---|
| 26 | //////////////////////////////////////////////////////////////////////////////
|
---|
| 27 | bncTime::bncTime(int gpsw, double gpssec) {
|
---|
| 28 | this->set(gpsw, gpssec);
|
---|
| 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 | void bncTime::jdgp(double tjul, double & second, long & nweek) {
|
---|
| 69 | double deltat;
|
---|
| 70 | deltat = tjul - 44244.0 ;
|
---|
| 71 | nweek = (long) floor(deltat/7.0);
|
---|
| 72 | second = (deltat - (nweek)*7.0)*86400.0;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | //
|
---|
| 76 | //////////////////////////////////////////////////////////////////////////////
|
---|
| 77 | unsigned int bncTime::mjd() const {
|
---|
| 78 | return _mjd;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | //
|
---|
| 82 | //////////////////////////////////////////////////////////////////////////////
|
---|
| 83 | double bncTime::daysec() const {
|
---|
| 84 | return _sec;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | //
|
---|
| 88 | //////////////////////////////////////////////////////////////////////////////
|
---|
| 89 | unsigned int bncTime::gpsw() const {
|
---|
| 90 | double gsec;
|
---|
| 91 | long gpsw;
|
---|
| 92 | jdgp(_mjd, gsec, gpsw);
|
---|
| 93 | return (int)gpsw;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | //
|
---|
| 97 | //////////////////////////////////////////////////////////////////////////////
|
---|
| 98 | double bncTime::gpssec() const {
|
---|
| 99 | double gsec;
|
---|
| 100 | long gpsw;
|
---|
| 101 | jdgp(_mjd, gsec, gpsw);
|
---|
| 102 | return gsec + _sec;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | //
|
---|
| 106 | //////////////////////////////////////////////////////////////////////////////
|
---|
| 107 | bool bncTime::operator!=(const bncTime &time1) const {
|
---|
| 108 | if ( ((*this) - time1) != 0 ) return 1;
|
---|
| 109 | return 0;
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | //
|
---|
| 113 | //////////////////////////////////////////////////////////////////////////////
|
---|
| 114 | bncTime bncTime::operator+(double sec) const {
|
---|
| 115 | int mjd = this->mjd();
|
---|
| 116 | double daysec = this->daysec();
|
---|
| 117 | daysec+=sec;
|
---|
| 118 | return bncTime().setmjd(daysec, mjd);
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | //
|
---|
| 122 | //////////////////////////////////////////////////////////////////////////////
|
---|
| 123 | bncTime bncTime::operator-(double sec) const {
|
---|
| 124 | return (*this) + (-sec);
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | //
|
---|
| 128 | //////////////////////////////////////////////////////////////////////////////
|
---|
| 129 | double bncTime::operator-(const bncTime &time1) const {
|
---|
| 130 | int mjdDiff = this->_mjd - time1._mjd;
|
---|
| 131 | if ( mjdDiff != 0 ) {
|
---|
| 132 | return mjdDiff * 86400.0 + this->_sec - time1._sec;
|
---|
| 133 | }
|
---|
| 134 | else {
|
---|
| 135 | return this->_sec - time1._sec;
|
---|
| 136 | }
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | //
|
---|
| 140 | //////////////////////////////////////////////////////////////////////////////
|
---|
| 141 | void bncTime::civil_time(unsigned int &hour, unsigned int &min,
|
---|
| 142 | double &sec) const {
|
---|
| 143 | hour = static_cast<unsigned int>(_sec/3600.0);
|
---|
| 144 | min = static_cast<unsigned int>((_sec - hour*3600)/60.0);
|
---|
| 145 | sec = _sec - min*60 - hour*3600;
|
---|
| 146 | if (sec==60.0) {
|
---|
| 147 | min++;
|
---|
| 148 | sec=0;
|
---|
| 149 | }
|
---|
| 150 | if (min==60) {
|
---|
| 151 | hour++;
|
---|
| 152 | min=0;
|
---|
| 153 | }
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | //
|
---|
| 157 | //////////////////////////////////////////////////////////////////////////////
|
---|
| 158 | string bncTime::timestr(unsigned numdec, char sep) const {
|
---|
| 159 | ostringstream str;
|
---|
| 160 | unsigned int hour, minute;
|
---|
| 161 | double sec;
|
---|
| 162 | this->civil_time(hour, minute, sec);
|
---|
| 163 | unsigned sw;
|
---|
| 164 | if (numdec == 0) {
|
---|
| 165 | sw = 2;
|
---|
| 166 | }
|
---|
| 167 | else {
|
---|
| 168 | sw = numdec + 3;
|
---|
| 169 | }
|
---|
| 170 | double chk = 0.5;
|
---|
| 171 | for (unsigned int ii=0; ii<numdec; ii++) chk *= 0.1;
|
---|
| 172 | if (sec > (60.0-chk)) {
|
---|
| 173 | sec = 0;
|
---|
| 174 | minute++;
|
---|
| 175 | if (minute == 60) {
|
---|
| 176 | minute = 0;
|
---|
| 177 | hour++;
|
---|
| 178 | }
|
---|
| 179 | }
|
---|
| 180 | str.setf(ios::fixed);
|
---|
| 181 | str << setfill('0');
|
---|
| 182 | str << setw(2) << hour;
|
---|
| 183 | if (sep) str << sep;
|
---|
| 184 | str << setw(2) << minute;
|
---|
| 185 | if (sep) str << sep;
|
---|
| 186 | str << setw(sw) << setprecision(numdec) << sec;
|
---|
| 187 | return str.str();
|
---|
| 188 | }
|
---|
| 189 |
|
---|
[2251] | 190 |
|
---|
| 191 | //
|
---|
| 192 | //////////////////////////////////////////////////////////////////////////////
|
---|
| 193 | bncTime& bncTime::set(int year, int month, int day,
|
---|
| 194 | int hour, int min, double sec) {
|
---|
| 195 | return set(year, month, day, hour*3600 + min*60 + sec);
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 | //
|
---|
| 199 | //////////////////////////////////////////////////////////////////////////////
|
---|
| 200 | bncTime& bncTime::set(int year, int month, int day, double daysec) {
|
---|
| 201 | _sec = daysec;
|
---|
| 202 |
|
---|
| 203 | _mjd = (unsigned int)djul(year, month, day);
|
---|
| 204 |
|
---|
| 205 | while ( _sec >= 86400 ) {
|
---|
| 206 | _sec-=86400;
|
---|
| 207 | _mjd++;
|
---|
| 208 | }
|
---|
| 209 | while ( _sec < 0 ) {
|
---|
| 210 | _sec+=86400;
|
---|
| 211 | _mjd--;
|
---|
| 212 | }
|
---|
| 213 |
|
---|
| 214 | return *this;
|
---|
| 215 | }
|
---|