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