/src/gdal/frmts/envisat/timedelta.hpp
Line | Count | Source (jump to first uncovered line) |
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 | | inline void set(int daysIn, int secsIn, int usecsIn) |
33 | 0 | { |
34 | 0 | 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 | | inline void fromSeconds(double secsIn) |
51 | 0 | { |
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 | 0 | { |
63 | 0 | } |
64 | | |
65 | | /* construct object using number of days, seconds and micro-seconds */ |
66 | | 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 | | explicit TimeDelta(double secsIn) |
73 | 0 | { |
74 | 0 | this->fromSeconds(secsIn); |
75 | 0 | } |
76 | | |
77 | | /* GETTERS */ |
78 | | |
79 | | inline int getDays(void) const |
80 | 0 | { |
81 | 0 | return this->days; |
82 | 0 | } |
83 | | |
84 | | inline int getSeconds(void) const |
85 | 0 | { |
86 | 0 | return this->secs; |
87 | 0 | } |
88 | | |
89 | | inline int getMicroseconds(void) const |
90 | 0 | { |
91 | 0 | return this->usecs; |
92 | 0 | } |
93 | | |
94 | | /* convert to seconds - can handle safely at least 250 years dif. */ |
95 | | /* ... before losing the microsecond precision */ |
96 | | inline operator double(void) const |
97 | 0 | { |
98 | 0 | return (this->days * 86400.0) + this->secs + (this->usecs * 1e-6); |
99 | 0 | } |
100 | | |
101 | | /* OPERATORS */ |
102 | | |
103 | | /* difference */ |
104 | | inline TimeDelta operator-(const TimeDelta &that) const |
105 | 0 | { |
106 | 0 | return TimeDelta(this->days - that.days, this->secs - that.secs, |
107 | 0 | this->usecs - that.usecs); |
108 | 0 | } |
109 | | |
110 | | /* addition */ |
111 | | inline TimeDelta operator+(const TimeDelta &that) const |
112 | 0 | { |
113 | 0 | return TimeDelta(this->days + that.days, this->secs + that.secs, |
114 | 0 | this->usecs + that.usecs); |
115 | 0 | } |
116 | | |
117 | | /* division */ |
118 | | inline double operator/(const TimeDelta &that) const |
119 | 0 | { |
120 | 0 | return ((double)*this / (double)that); |
121 | 0 | } |
122 | | |
123 | | /* integer multiplication */ |
124 | | inline TimeDelta operator*(const int i) const |
125 | 0 | { |
126 | 0 | return TimeDelta(i * this->days, i * this->secs, i * this->usecs); |
127 | 0 | } |
128 | | |
129 | | /* float multiplication */ |
130 | | inline TimeDelta operator*(const double f) const |
131 | 0 | { |
132 | 0 | return TimeDelta(f * (double)*this); |
133 | 0 | } |
134 | | |
135 | | /* comparisons operators */ |
136 | | |
137 | | inline bool operator==(const TimeDelta &that) const |
138 | 0 | { |
139 | 0 | return ((this->usecs == that.usecs) && (this->secs == that.secs) && |
140 | 0 | (this->days == that.days)); |
141 | 0 | } |
142 | | |
143 | | inline bool operator>(const TimeDelta &that) const |
144 | 0 | { |
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 | 0 | } |
150 | | |
151 | | inline bool operator<(const TimeDelta &that) const |
152 | 0 | { |
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 | 0 | } |
158 | | |
159 | | inline bool operator!=(const TimeDelta &that) const |
160 | 0 | { |
161 | 0 | return !(*this == that); |
162 | 0 | } |
163 | | |
164 | | inline bool operator>=(const TimeDelta &that) const |
165 | 0 | { |
166 | 0 | return !(*this < that); |
167 | 0 | } |
168 | | |
169 | | inline bool operator<=(const TimeDelta &that) const |
170 | 0 | { |
171 | 0 | return !(*this > that); |
172 | 0 | } |
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*/ |