1
0
Fork 0
mirror of https://github.com/opentx/opentx.git synced 2025-07-15 04:15:26 +03:00

Handle large timers (#7962)

This fixes #7607
This commit is contained in:
3djc 2020-09-25 12:55:18 +02:00 committed by GitHub
parent dbaa4058df
commit cd1716b941
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 17 deletions

View file

@ -111,7 +111,7 @@ void ModelCell::loadBitmap()
getTimerString(timer, 0); getTimerString(timer, 0);
for (uint8_t i = 0; i < MAX_TIMERS; i++) { for (uint8_t i = 0; i < MAX_TIMERS; i++) {
if (partialmodel.timers[i].mode != 0 && partialmodel.timers[i].persistent) { if (partialmodel.timers[i].mode != 0 && partialmodel.timers[i].persistent) {
getTimerString(timer, partialmodel.timers[i].value); getTimerString(timer, partialmodel.timers[i].value, 1);
break; break;
} }
} }

View file

@ -170,6 +170,10 @@ char * strAppendStringWithIndex(char * dest, const char * s, int idx)
return strAppendUnsigned(strAppend(dest, s), abs(idx)); return strAppendUnsigned(strAppend(dest, s), abs(idx));
} }
constexpr int32_t secondsPerDay = 24 * 3600;
constexpr int32_t secondsPer99Hours = 99*3600 + 59*60 + 59;
constexpr int32_t secondsPerYear = 365 * secondsPerDay;
char * getTimerString(char * dest, int32_t tme, uint8_t hours) char * getTimerString(char * dest, int32_t tme, uint8_t hours)
{ {
char * s = dest; char * s = dest;
@ -180,28 +184,63 @@ char * getTimerString(char * dest, int32_t tme, uint8_t hours)
*s++ = '-'; *s++ = '-';
} }
qr = div((int)tme, 60); if (tme < secondsPerDay) {
qr = div((int) tme, 60);
if (hours) { if (hours) {
div_t qr2 = div(qr.quot, 60); div_t qr2 = div(qr.quot, 60);
*s++ = '0' + (qr2.quot / 10);
*s++ = '0' + (qr2.quot % 10);
*s++ = ':';
qr.quot = qr2.rem;
}
if (!hours && qr.quot > 99) {
*s++ = '0' + (qr.quot / 100);
qr.quot = qr.quot % 100;
}
*s++ = '0' + (qr.quot / 10);
*s++ = '0' + (qr.quot % 10);
*s++ = ':';
*s++ = '0' + (qr.rem / 10);
*s++ = '0' + (qr.rem % 10);
*s = '\0';
}
else if (tme < secondsPer99Hours) {
qr = div((int) tme, 3600);
div_t qr2 = div(qr.rem, 60);
*s++ = '0' + (qr.quot / 10);
*s++ = '0' + (qr.quot % 10);
*s++ = 'H';
*s++ = '0' + (qr2.quot / 10); *s++ = '0' + (qr2.quot / 10);
*s++ = '0' + (qr2.quot % 10); *s++ = '0' + (qr2.quot % 10);
*s++ = ':'; *s = '\0';
qr.quot = qr2.rem;
} }
else if (tme < secondsPerYear) {
if (!hours && qr.quot > 99) { qr = div((int) tme, secondsPerDay);
div_t qr2 = div(qr.rem, 60);
*s++ = '0' + (qr.quot / 100); *s++ = '0' + (qr.quot / 100);
qr.quot = qr.quot % 100; *s++ = '0' + (qr.quot / 10);
*s++ = '0' + (qr.quot % 10);
*s++ = 'D';
*s++ = '0' + (qr2.quot / 10);
*s++ = '0' + (qr2.quot % 10);
*s++ = 'H';
*s = '\0';
}
else {
qr = div((int) tme, secondsPerYear);
div_t qr2 = div(qr.rem, secondsPerDay);
*s++ = '0' + (qr.quot / 10);
*s++ = '0' + (qr.quot % 10);
*s++ = 'Y';
*s++ = 'Y';
*s++ = '0' + (qr2.quot / 10);
*s++ = '0' + (qr2.quot % 10);
*s++ = 'D';
*s = '\0';
} }
*s++ = '0' + (qr.quot / 10);
*s++ = '0' + (qr.quot % 10);
*s++ = ':';
*s++ = '0' + (qr.rem / 10);
*s++ = '0' + (qr.rem % 10);
*s = '\0';
return dest; return dest;
} }