some time refactors

no more weird totalTicks name
code looks better
This commit is contained in:
tildearrow 2025-10-30 20:35:14 -05:00
parent a2b56b5b64
commit 5ff81aef33
16 changed files with 256 additions and 211 deletions

View file

@ -27,6 +27,23 @@
#include "ta-utils.h"
enum TATimeFormats: unsigned char {
TA_TIME_FORMAT_SECONDS=0, // seconds only
TA_TIME_FORMAT_MS, // m:ss
TA_TIME_FORMAT_HMS, // h:mm:ss
TA_TIME_FORMAT_MS_ZERO, // mm:ss
TA_TIME_FORMAT_HMS_ZERO, // hh:mm:ss
TA_TIME_FORMAT_DAYS_HMS, // years, months, days, h:mm:ss
TA_TIME_FORMAT_DAYS_HMS_ZERO, // years, months, days, hh:mm:ss
TA_TIME_FORMAT_AUTO, // automatic
TA_TIME_FORMAT_AUTO_ZERO, // automatic (zero)
TA_TIME_FORMAT_AUTO_MS, // automatic (from minutes)
TA_TIME_FORMAT_AUTO_MS_ZERO, // automatic (from minutes; zero)
};
struct TimeMicros {
int seconds, micros;
@ -37,13 +54,62 @@ struct TimeMicros {
return seconds+(double)micros/1000000.0;
}
// operators
inline TimeMicros& operator+(TimeMicros& other) {
seconds+=other.seconds;
micros+=other.micros;
while (micros>=1000000) {
micros-=1000000;
seconds++;
}
return *this;
}
inline TimeMicros& operator+(int other) {
seconds+=other;
return *this;
}
inline TimeMicros& operator-(TimeMicros& other) {
seconds-=other.seconds;
micros-=other.micros;
while (micros<0) {
micros+=1000000;
seconds--;
}
return *this;
}
inline TimeMicros& operator-(int other) {
seconds-=other;
return *this;
}
// comparison operators
inline bool operator==(TimeMicros& other) {
return (seconds==other.seconds && micros==other.micros);
}
inline bool operator>(TimeMicros& other) {
return (seconds>other.seconds || (seconds==other.seconds && micros>other.micros));
}
inline bool operator>=(TimeMicros& other) {
return (seconds>other.seconds || (seconds==other.seconds && micros>=other.micros));
}
inline bool operator!=(TimeMicros& other) {
return !(*this==other);
}
inline bool operator<(TimeMicros& other) {
return !(*this>=other);
}
inline bool operator<=(TimeMicros& other) {
return !(*this>other);
}
/**
* convert this TimeMicros to a String.
* @param prec maximum digit precision (0-6). use -1 for automatic precision.
* @param hms how many denominations to include (0: seconds, 1: minutes, 2: hours).
* @return formatted TimeMicros.
*/
String toString(signed char prec=-1, unsigned char hms=0);
String toString(signed char prec=6, TATimeFormats hms=TA_TIME_FORMAT_SECONDS);
static TimeMicros fromString(String s);
TimeMicros(int s, int u):