I have an embedded device that's doing a crude time sync with a Linux-based wireless gateway's Unix time (time_t), which is set to UTC. I need to align some of my actions with the wall-clock time -- for example, there's a task I need to do at 30 seconds past the minute. Do I need to call the usual C standard library functions to get the seconds field, or is it safe to do something simpler:
int seconds_past_the_minute = (unix_utc_timestamp % 60);
It should be safe, but it’s ugly.
Since Unix timestamps ignore leap seconds and since they count from midnight on some date, the modulo operation unix_utc_timestamp % 60
should give you the second of minute. A few tests should verify, and if they do, I’d be happy about the correctness.
However, what’s wrong with using the library function? My experience with the C library dates back to 2002, so I don’t remember exactly what it looks like, but you may think that it gives more readable and self-explanatory code. So if there isn’t some strong argument against it, I’d tend to prefer it.
(The date the Unix timestamps count from is 1970-01-01. In UTC. But the argument would be the same if it had been any other UTC date.)