Line data Source code
1 : /****************************************************************************** 2 : * Project: APP ENVISAT Support 3 : * Purpose: time difference class for handling of Envisat MJD time 4 : * Author: Martin Paces, martin.paces@eox.at 5 : * 6 : ****************************************************************************** 7 : * Copyright (c) 2013, EOX IT Services, GmbH 8 : * 9 : * SPDX-License-Identifier: MIT 10 : *****************************************************************************/ 11 : 12 : #ifndef timedelta_hpp 13 : #define timedelta_hpp 14 : 15 : /* 16 : * TimeDelta class represents the time difference. It is used to 17 : * hold the Envisat MJD (Modified Julian Date - which is time 18 : * since 2000-01-01T00:00:00.000000Z) 19 : */ 20 : 21 : class TimeDelta 22 : { 23 : 24 : private: 25 : int days; /* number of days */ 26 : int secs; /* number of seconds since day start */ 27 : int usecs; /* number of micro sec. since second start */ 28 : 29 : /* SETTERS */ 30 : 31 : /* set object using number of days, seconds and micro-seconds */ 32 0 : inline void set(int daysIn, int secsIn, int usecsIn) 33 : { 34 : int tmp0, tmp1; 35 : /* overflow check with proper handling of negative values */ 36 : /* note that division and modulo for negative values is impl.dependent 37 : */ 38 : 39 0 : secsIn += (tmp0 = usecsIn >= 0 ? usecsIn / 1000000 40 0 : : -1 - ((-usecsIn) / 1000000)); 41 0 : daysIn += 42 0 : (tmp1 = secsIn >= 0 ? secsIn / 86400 : -1 - ((-secsIn) / 86400)); 43 : 44 0 : this->usecs = usecsIn - 1000000 * tmp0; 45 0 : this->secs = secsIn - 86400 * tmp1; 46 0 : this->days = daysIn; 47 0 : } 48 : 49 : /* set object from floating point number of seconds */ 50 0 : inline void fromSeconds(double secsIn) 51 : { 52 0 : int _days = (int)(secsIn / 86400); 53 0 : int _secs = (int)(secsIn - 86400 * _days); 54 0 : int _uscs = (int)((secsIn - ((int)secsIn)) * 1e6); 55 : 56 0 : this->set(_days, _secs, _uscs); 57 0 : } 58 : 59 : public: 60 : /* CONSTRUCTORS */ 61 : TimeDelta(void) : days(0), secs(0), usecs(0) 62 : { 63 : } 64 : 65 : /* construct object using number of days, seconds and micro-seconds */ 66 0 : TimeDelta(int daysIn, int secsIn, int usecsIn) 67 0 : { 68 0 : this->set(daysIn, secsIn, usecsIn); 69 0 : } 70 : 71 : /* construct object from floating point number of seconds */ 72 0 : explicit TimeDelta(double secsIn) 73 0 : { 74 0 : this->fromSeconds(secsIn); 75 0 : } 76 : 77 : /* GETTERS */ 78 : 79 : inline int getDays(void) const 80 : { 81 : return this->days; 82 : } 83 : 84 : inline int getSeconds(void) const 85 : { 86 : return this->secs; 87 : } 88 : 89 : inline int getMicroseconds(void) const 90 : { 91 : return this->usecs; 92 : } 93 : 94 : /* convert to seconds - can handle safely at least 250 years dif. */ 95 : /* ... before losing the microsecond precision */ 96 0 : inline operator double(void) const 97 : { 98 0 : return (this->days * 86400.0) + this->secs + (this->usecs * 1e-6); 99 : } 100 : 101 : /* OPERATORS */ 102 : 103 : /* difference */ 104 0 : inline TimeDelta operator-(const TimeDelta &that) const 105 : { 106 0 : return TimeDelta(this->days - that.days, this->secs - that.secs, 107 0 : this->usecs - that.usecs); 108 : } 109 : 110 : /* addition */ 111 0 : inline TimeDelta operator+(const TimeDelta &that) const 112 : { 113 0 : return TimeDelta(this->days + that.days, this->secs + that.secs, 114 0 : this->usecs + that.usecs); 115 : } 116 : 117 : /* division */ 118 0 : inline double operator/(const TimeDelta &that) const 119 : { 120 0 : return ((double)*this / (double)that); 121 : } 122 : 123 : /* integer multiplication */ 124 : inline TimeDelta operator*(const int i) const 125 : { 126 : return TimeDelta(i * this->days, i * this->secs, i * this->usecs); 127 : } 128 : 129 : /* float multiplication */ 130 0 : inline TimeDelta operator*(const double f) const 131 : { 132 0 : return TimeDelta(f * (double)*this); 133 : } 134 : 135 : /* comparisons operators */ 136 : 137 : inline bool operator==(const TimeDelta &that) const 138 : { 139 : return ((this->usecs == that.usecs) && (this->secs == that.secs) && 140 : (this->days == that.days)); 141 : } 142 : 143 0 : inline bool operator>(const TimeDelta &that) const 144 : { 145 0 : return (this->days > that.days) || 146 0 : ((this->days == that.days) && 147 0 : ((this->secs > that.secs) || 148 0 : ((this->secs == that.secs) && (this->usecs > that.usecs)))); 149 : } 150 : 151 0 : inline bool operator<(const TimeDelta &that) const 152 : { 153 0 : return (this->days < that.days) || 154 0 : ((this->days == that.days) && 155 0 : ((this->secs < that.secs) || 156 0 : ((this->secs == that.secs) && (this->usecs < that.usecs)))); 157 : } 158 : 159 : inline bool operator!=(const TimeDelta &that) const 160 : { 161 : return !(*this == that); 162 : } 163 : 164 0 : inline bool operator>=(const TimeDelta &that) const 165 : { 166 0 : return !(*this < that); 167 : } 168 : 169 0 : inline bool operator<=(const TimeDelta &that) const 170 : { 171 0 : return !(*this > that); 172 : } 173 : }; 174 : 175 : /* 176 : #include <iostream> 177 : 178 : std::ostream & operator<<( std::ostream & out, const TimeDelta & td ) 179 : { 180 : 181 : out << "TimeDelta(" << td.getDays() 182 : << "," << td.getSeconds() 183 : << "," << td.getMicroseconds() << ")" ; 184 : 185 : return out ; 186 : } 187 : */ 188 : 189 : #endif /*timedelta_hpp*/