(Answering my own question)
One answer suggests that as long as you use posix, time_t is in seconds, and arithmetic on time_t should work.
The second answer calculates time_t per second and uses this as a factor when performing arithmetic. But there are still some assumptions about time_t .
In the end, I decided that portability was more important; I don't want my code to fail on some embedded device. Therefore, I used the third method. It includes storing an integer representing the time since the program was launched. That is, I define
const static time_t time0 = time(nullptr); static tm time0Components = *localtime(&time0);
All time values ββused throughout the program are integers, indicating the time difference in seconds from time0 . To go from time_t to these delta seconds, I use difftime . To get back to time_t , I use something like this:
time_t getTime_t(int timeDeltaSeconds) { tm components = time0Components; components.tm_sec += timeDeltaSeconds; return mktime(&components); }
This approach allows you to make operations like + , - cheap, but returning to time_t is expensive. Please note that delta-time values ββare meaningful only for the current program launch. Please also note that time0 Components must be updated when the time zone changes.
Ant6n source share