|
4 | 4 | #include "DayTime.hpp" |
5 | 5 |
|
6 | 6 | /////////////////////////////////// |
7 | | -// DayTime (and Declination below) |
8 | | -// |
9 | | -// A class to handle hours, minutes, seconds in a unified manner, allowing |
10 | | -// addition of hours, minutes, seconds, other times and conversion to string. |
| 7 | +// DayTime — Arduino-dependent overlay methods |
| 8 | +// Pure arithmetic is in core::DayTime (src/core/types/DayTime.hpp/.cpp) |
11 | 9 |
|
12 | 10 | // Parses the RA or DEC from a string that has an optional sign, a two digit degree, a seperator, a two digit minute, a seperator and a two digit second. |
13 | 11 | // Does not correct for hemisphere (derived class Declination takes care of that) |
@@ -59,139 +57,6 @@ DayTime DayTime::ParseFromMeade(String const &s) |
59 | 57 | return result; |
60 | 58 | } |
61 | 59 |
|
62 | | -DayTime::DayTime() |
63 | | -{ |
64 | | - totalSeconds = 0; |
65 | | -} |
66 | | - |
67 | | -DayTime::DayTime(const DayTime &other) |
68 | | -{ |
69 | | - totalSeconds = other.totalSeconds; |
70 | | -} |
71 | | - |
72 | | -DayTime::DayTime(int h, int m, int s) |
73 | | -{ |
74 | | - long sgn = sign(h); |
75 | | - h = abs(h); |
76 | | - totalSeconds = sgn * ((60L * h + m) * 60L + s); |
77 | | -} |
78 | | - |
79 | | -DayTime::DayTime(float timeInHours) |
80 | | -{ |
81 | | - long sgn = fsign(timeInHours); |
82 | | - timeInHours = fabsf(timeInHours); |
83 | | - totalSeconds = sgn * static_cast<long>(roundf(timeInHours * 60.0f * 60.0f)); |
84 | | -} |
85 | | - |
86 | | -int DayTime::getHours() const |
87 | | -{ |
88 | | - int h, m, s; |
89 | | - getTime(h, m, s); |
90 | | - return h; |
91 | | -} |
92 | | - |
93 | | -int DayTime::getMinutes() const |
94 | | -{ |
95 | | - int h, m, s; |
96 | | - getTime(h, m, s); |
97 | | - return m; |
98 | | -} |
99 | | - |
100 | | -int DayTime::getSeconds() const |
101 | | -{ |
102 | | - int h, m, s; |
103 | | - getTime(h, m, s); |
104 | | - return s; |
105 | | -} |
106 | | - |
107 | | -float DayTime::getTotalHours() const |
108 | | -{ |
109 | | - return 1.0f * totalSeconds / 3600.0f; |
110 | | -} |
111 | | - |
112 | | -float DayTime::getTotalMinutes() const |
113 | | -{ |
114 | | - return 1.0f * totalSeconds / 60.0f; |
115 | | -} |
116 | | - |
117 | | -long DayTime::getTotalSeconds() const |
118 | | -{ |
119 | | - return totalSeconds; |
120 | | -} |
121 | | - |
122 | | -void DayTime::getTime(int &h, int &m, int &s) const |
123 | | -{ |
124 | | - long seconds = labs(totalSeconds); |
125 | | - |
126 | | - h = (int) (seconds / 3600L); |
127 | | - seconds = seconds - (h * 3600L); |
128 | | - m = (int) (seconds / 60L); |
129 | | - s = (int) (seconds - (m * 60L)); |
130 | | - |
131 | | - h *= sign(totalSeconds); |
132 | | -} |
133 | | - |
134 | | -void DayTime::set(int h, int m, int s) |
135 | | -{ |
136 | | - DayTime dt(h, m, s); |
137 | | - totalSeconds = dt.totalSeconds; |
138 | | - checkHours(); |
139 | | -} |
140 | | - |
141 | | -void DayTime::set(const DayTime &other) |
142 | | -{ |
143 | | - totalSeconds = other.totalSeconds; |
144 | | - checkHours(); |
145 | | -} |
146 | | - |
147 | | -// Add hours, wrapping days (which are not tracked) |
148 | | -void DayTime::addHours(float deltaHours) |
149 | | -{ |
150 | | - totalSeconds += long(deltaHours * 3600L); |
151 | | - checkHours(); |
152 | | -} |
153 | | - |
154 | | -void DayTime::checkHours() |
155 | | -{ |
156 | | - while (totalSeconds >= secondsPerDay) |
157 | | - { |
158 | | - totalSeconds -= secondsPerDay; |
159 | | - } |
160 | | - |
161 | | - while (totalSeconds < 0) |
162 | | - { |
163 | | - totalSeconds += secondsPerDay; |
164 | | - } |
165 | | -} |
166 | | - |
167 | | -// Add minutes, wrapping hours if needed |
168 | | -void DayTime::addMinutes(int deltaMins) |
169 | | -{ |
170 | | - totalSeconds += deltaMins * 60; |
171 | | - checkHours(); |
172 | | -} |
173 | | - |
174 | | -// Add seconds, wrapping minutes and hours if needed |
175 | | -void DayTime::addSeconds(long deltaSecs) |
176 | | -{ |
177 | | - totalSeconds += deltaSecs; |
178 | | - checkHours(); |
179 | | -} |
180 | | - |
181 | | -// Add another time, wrapping seconds, minutes and hours if needed |
182 | | -void DayTime::addTime(const DayTime &other) |
183 | | -{ |
184 | | - totalSeconds += other.totalSeconds; |
185 | | - checkHours(); |
186 | | -} |
187 | | - |
188 | | -// Subtract another time, wrapping seconds, minutes and hours if needed |
189 | | -void DayTime::subtractTime(const DayTime &other) |
190 | | -{ |
191 | | - totalSeconds -= other.totalSeconds; |
192 | | - checkHours(); |
193 | | -} |
194 | | - |
195 | 60 | char achBuf[32]; |
196 | 61 |
|
197 | 62 | // Convert to a standard string (like 14:45:06) |
@@ -245,112 +110,7 @@ const char *DayTime::ToString() const |
245 | 110 | snprintf(p, remaining, " (%s)", floatStr.c_str()); |
246 | 111 | return achBuf; |
247 | 112 | } |
248 | | -void DayTime::printTwoDigits(char *achDegs, int num) const |
249 | | -{ |
250 | | - achDegs[0] = '0' + (num / 10); |
251 | | - achDegs[1] = '0' + (num % 10); |
252 | | - achDegs[2] = 0; |
253 | | -} |
254 | 113 |
|
255 | | -const char *DayTime::formatStringImpl(char *targetBuffer, const char *format, char sgn, long degs, long mins, long secs) const |
256 | | -{ |
257 | | - char achDegs[5]; |
258 | | - char achMins[3]; |
259 | | - char achSecs[3]; |
260 | | - const char *f = format; |
261 | | - char *p = targetBuffer; |
262 | | - |
263 | | - int i = 0; |
264 | | - if (sgn != '\0') |
265 | | - { |
266 | | - achDegs[0] = sgn; |
267 | | - i++; |
268 | | - } |
269 | | - |
270 | | - long absdegs = labs(degs); |
271 | | - if (absdegs >= 100) |
272 | | - { |
273 | | - achDegs[i++] = '0' + min(9L, (absdegs / 100)); |
274 | | - absdegs = absdegs % 100; |
275 | | - } |
276 | | - |
277 | | - printTwoDigits(achDegs + i, absdegs); |
278 | | - printTwoDigits(achMins, mins); |
279 | | - printTwoDigits(achSecs, secs); |
280 | | - |
281 | | - char macro = '\0'; |
282 | | - bool inMacro = false; |
283 | | - while (*f) |
284 | | - { |
285 | | - switch (*f) |
286 | | - { |
287 | | - case '{': |
288 | | - { |
289 | | - inMacro = true; |
290 | | - } |
291 | | - break; |
292 | | - case '}': |
293 | | - { |
294 | | - if (inMacro) |
295 | | - { |
296 | | - switch (macro) |
297 | | - { |
298 | | - case '+': |
299 | | - { |
300 | | - *p++ = (degs < 0 ? '-' : '+'); |
301 | | - } |
302 | | - break; |
303 | | - case 'd': |
304 | | - { |
305 | | - strcpy(p, achDegs); |
306 | | - p += strlen(achDegs); |
307 | | - } |
308 | | - break; |
309 | | - case 'm': |
310 | | - { |
311 | | - strcpy(p, achMins); |
312 | | - p += 2; |
313 | | - } |
314 | | - break; |
315 | | - case 's': |
316 | | - { |
317 | | - strcpy(p, achSecs); |
318 | | - p += 2; |
319 | | - } |
320 | | - break; |
321 | | - } |
322 | | - inMacro = false; |
323 | | - } |
324 | | - } |
325 | | - break; |
326 | | - default: |
327 | | - { |
328 | | - if (inMacro) |
329 | | - { |
330 | | - macro = *f; |
331 | | - } |
332 | | - else |
333 | | - { |
334 | | - *p++ = *f; |
335 | | - } |
336 | | - } |
337 | | - } |
338 | | - f++; |
339 | | - } |
340 | | - |
341 | | - *p = '\0'; |
342 | | - return targetBuffer; |
343 | | -} |
344 | | - |
345 | | -const char *DayTime::formatString(char *targetBuffer, const char *format, long *pSecs) const |
346 | | -{ |
347 | | - long secs = pSecs == nullptr ? totalSeconds : *pSecs; |
348 | | - char sgn = secs < 0 ? '-' : '+'; |
349 | | - secs = labs(secs); |
350 | | - long degs = secs / 3600; |
351 | | - secs = secs - degs * 3600; |
352 | | - long mins = secs / 60; |
353 | | - secs = secs - mins * 60; |
354 | | - |
355 | | - return formatStringImpl(targetBuffer, format, sgn, degs, mins, secs); |
356 | | -} |
| 114 | +// ParseFromMeade, formatString, formatStringImpl, printTwoDigits |
| 115 | +// were previously here. formatString/formatStringImpl/printTwoDigits |
| 116 | +// are now in core::DayTime (pure). |
0 commit comments