|
| 1 | +#include "Utility.h" |
| 2 | +#include "DayTime.hpp" |
| 3 | +// A class to handle hours, minutes, seconds in a unified manner, allowing |
| 4 | +// addition of hours, minutes, seconds, other times and conversion to string. |
| 5 | + |
| 6 | +DayTime::DayTime() { |
| 7 | + hours = 0; |
| 8 | + mins = 0; |
| 9 | + secs = 0; |
| 10 | +} |
| 11 | + |
| 12 | +DayTime::DayTime(const DayTime& other) { |
| 13 | + hours = other.hours; |
| 14 | + mins = other.mins; |
| 15 | + secs = other.secs; |
| 16 | +} |
| 17 | + |
| 18 | +DayTime::DayTime(int h, int m, int s) { |
| 19 | + hours = h; |
| 20 | + mins = m; |
| 21 | + secs = s; |
| 22 | +} |
| 23 | + |
| 24 | +// From milliseconds. Does not handle days! |
| 25 | +DayTime::DayTime(long ms) { |
| 26 | + ms /= 1000; // seconds |
| 27 | + secs = (int)(ms % 60); |
| 28 | + ms = (ms - secs) / 60; |
| 29 | + mins = (int)(ms % 60); |
| 30 | + ms = (ms - mins) / 60; |
| 31 | + hours = (int)ms ; |
| 32 | +} |
| 33 | + |
| 34 | +DayTime::DayTime(float timeInHours) { |
| 35 | + hours = floor(timeInHours); |
| 36 | + timeInHours = (timeInHours - hours) * 60; |
| 37 | + mins = floor(timeInHours); |
| 38 | + timeInHours = (timeInHours - mins) * 60; |
| 39 | + secs = floor(timeInHours); |
| 40 | +} |
| 41 | + |
| 42 | +int DayTime::getHours() { |
| 43 | + return hours; |
| 44 | +} |
| 45 | + |
| 46 | +int DayTime::getMinutes() { |
| 47 | + return mins; |
| 48 | +} |
| 49 | + |
| 50 | +int DayTime::getSeconds() { |
| 51 | + return secs; |
| 52 | +} |
| 53 | + |
| 54 | +float DayTime::getTotalHours() { |
| 55 | + return 1.0f * getHours() + ((float)getMinutes() / 60.0f) + ((float)getSeconds() / 3600.0f); |
| 56 | +} |
| 57 | + |
| 58 | +float DayTime::getTotalMinutes() { |
| 59 | + return 60.0f * getHours() + (float)getMinutes() + ((float)getSeconds() / 60.0f); |
| 60 | +} |
| 61 | + |
| 62 | +float DayTime::getTotalSeconds() { |
| 63 | + return 3600.0f * getHours() + (float)getMinutes() * 60.0f + (float)getSeconds(); |
| 64 | +} |
| 65 | + |
| 66 | +int DayTime::getTime(int& h, int& m, int& s) { |
| 67 | + h = hours; |
| 68 | + m = mins; |
| 69 | + s = secs; |
| 70 | +} |
| 71 | + |
| 72 | +void DayTime::set(int h, int m, int s) { |
| 73 | + hours = h; |
| 74 | + mins = m; |
| 75 | + secs = s; |
| 76 | + checkHours(); |
| 77 | +} |
| 78 | + |
| 79 | +void DayTime::set(const DayTime& other) { |
| 80 | + hours = other.hours; |
| 81 | + mins = other.mins; |
| 82 | + secs = other.secs; |
| 83 | + checkHours(); |
| 84 | +} |
| 85 | + |
| 86 | +// Add hours, wrapping days (which are not tracked) |
| 87 | +void DayTime::addHours(int deltaHours) { |
| 88 | + hours += deltaHours; |
| 89 | + checkHours(); |
| 90 | +} |
| 91 | + |
| 92 | +void DayTime::checkHours() { |
| 93 | + Serial.println("DtIn: "+String(hours)); |
| 94 | + while (hours >= hourWrap) { |
| 95 | + hours -= hourWrap; |
| 96 | + } |
| 97 | + while (hours < 0) { |
| 98 | + hours += hourWrap; |
| 99 | + } |
| 100 | + Serial.println("DtOut: "+String(hours)); |
| 101 | +} |
| 102 | + |
| 103 | +// Add minutes, wrapping hours if needed |
| 104 | +void DayTime::addMinutes(int deltaMins) { |
| 105 | + mins += deltaMins; |
| 106 | + while (mins > 59) { |
| 107 | + mins -= 60; |
| 108 | + addHours(1); |
| 109 | + } |
| 110 | + while (mins < 0) { |
| 111 | + mins += 60; |
| 112 | + addHours(-1); |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +// Add seconds, wrapping minutes and hours if needed |
| 117 | +void DayTime::addSeconds(long deltaSecs) { |
| 118 | + secs += deltaSecs; |
| 119 | + while (secs > 59) { |
| 120 | + secs -= 60; |
| 121 | + addMinutes(1); |
| 122 | + } |
| 123 | + while (secs < 0) { |
| 124 | + secs += 60; |
| 125 | + addMinutes(-1); |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +// Add time components, wrapping seconds, minutes and hours if needed |
| 130 | +void DayTime::addTime(int deltaHours, int deltaMinutes, int deltaSeconds) |
| 131 | +{ |
| 132 | + addSeconds(deltaSeconds); |
| 133 | + addMinutes(deltaMinutes); |
| 134 | + addHours(deltaHours); |
| 135 | +} |
| 136 | + |
| 137 | +// Add another time, wrapping seconds, minutes and hours if needed |
| 138 | +void DayTime::addTime(const DayTime& other) |
| 139 | +{ |
| 140 | + addSeconds(other.getSeconds()); |
| 141 | + addMinutes(other.getMinutes()); |
| 142 | + addHours(other.getHours()); |
| 143 | +} |
| 144 | + |
| 145 | +// Subtract another time, wrapping seconds, minutes and hours if needed |
| 146 | +void DayTime::subtractTime(const DayTime& other) |
| 147 | +{ |
| 148 | + addSeconds(-other.getSeconds()); |
| 149 | + addMinutes(-other.getMinutes()); |
| 150 | + addHours(-other.getHours()); |
| 151 | +} |
| 152 | + |
| 153 | +// Convert to a standard string (like 14:45:06) |
| 154 | +String DayTime::ToString() |
| 155 | +{ |
| 156 | + char achBuf[12]; |
| 157 | + char*p = achBuf; |
| 158 | + |
| 159 | + if (hours < 10) { |
| 160 | + *p++ = '0'; |
| 161 | + } else { |
| 162 | + *p++ = '0' + (hours / 10); |
| 163 | + } |
| 164 | + *p++ = '0' + (hours % 10); |
| 165 | + |
| 166 | + *p++ = ':'; |
| 167 | + if (mins < 10) { |
| 168 | + *p++ = '0'; |
| 169 | + } else { |
| 170 | + *p++ = '0' + (mins / 10); |
| 171 | + } |
| 172 | + *p++ = '0' + (mins % 10); |
| 173 | + *p++ = ':'; |
| 174 | + if (secs < 10) { |
| 175 | + *p++ = '0'; |
| 176 | + } else { |
| 177 | + *p++ = '0' + (secs / 10); |
| 178 | + } |
| 179 | + *p++ = '0' + (secs % 10); |
| 180 | + *p++ = '\0'; |
| 181 | + return String(achBuf); |
| 182 | +} |
| 183 | + |
| 184 | + |
| 185 | +DegreeTime::DegreeTime(): DayTime() { |
| 186 | + hourWrap = 180; |
| 187 | +} |
| 188 | + |
| 189 | +DegreeTime::DegreeTime(const DegreeTime& other): DayTime(other) { } |
| 190 | +DegreeTime::DegreeTime(int h, int m, int s) : DayTime(h, m, s) { } |
| 191 | +DegreeTime::DegreeTime(float inDegrees) : DayTime(inDegrees) { } |
| 192 | + |
| 193 | +void DegreeTime::addDegrees(int deltaDegrees) { |
| 194 | + addHours(deltaDegrees); |
| 195 | +} |
| 196 | + |
| 197 | +int DegreeTime::getDegrees() { |
| 198 | + return hours; |
| 199 | +} |
| 200 | + |
| 201 | +int DegreeTime::getPrintDegrees() { |
| 202 | + return NORTHERN_HEMISPHERE ? hours + 90 : hours - 90; |
| 203 | +} |
| 204 | + |
| 205 | +float DegreeTime::getTotalDegrees() { |
| 206 | + return getTotalHours(); |
| 207 | +} |
| 208 | + |
| 209 | +void DegreeTime::checkHours() { |
| 210 | + if (NORTHERN_HEMISPHERE) { |
| 211 | + Serial.println("DgIn: "+String(hours)); |
| 212 | + if (hours > 0) hours = 0; |
| 213 | + if (hours < -180) hours = -180; |
| 214 | + Serial.println("DgOut: "+String(hours)); |
| 215 | + } else { |
| 216 | + if (hours > 180) hours = 180; |
| 217 | + if (hours < 0) hours = 0; |
| 218 | + } |
| 219 | +} |
0 commit comments