4

Since 24 hours have 86400000 milliseconds, is is safe to call:

timestamp % 86400000

to get milliseconds passed during particular day? timestamp is the unix epoch expressed in milliseconds, eg 1480771537000.

I'm wondering whether this is a safe technique given the leap seconds are being added and subtracted every now and then? Will above code always return correctly the milliseconds during the day, independent of what timestamp is?

Voy
  • 5,286
  • 1
  • 49
  • 59
  • @RyanVincent I'm using the unix timestamp generated by Android which returns milliseconds - thanks for pointing out that by book unix epoch is expressed in seconds. Nonetheless, it's irrelevant, consider the same question asked in regards to seconds. My use case varies from displaying time remaining during the day to performing an operation at midnight – Voy Dec 03 '16 at 15:29
  • @RyanVincent yes, one of the things I'm trying to achieve is running some logic when I detect change from one day to another. Other than that, I really do need to know the time that has passed during the day, to display amount of time remaining during the day. Note that I'm doing this using NTP timestamp not the machine clock – Voy Dec 03 '16 at 15:45
  • @RyanVincent I'm currently writing in Java. Thanks for your suggestions, but again these details are irrelevant as the question doesn't ask for particular implementation nor for my use cases. It's related to unix epoch theory and robustness of performing modulo operation on a unix timestamp considering leap seconds – Voy Dec 03 '16 at 16:03

2 Answers2

2

Yes, you can use timestamp % 86400000 to tell whether the timestamp represents midnight/beginning of a day (assuming the timestamp is in milliseconds since Epoch, and not seconds).

According to this Stack Overflow answer there are always 86400000 milliseconds in a day as far as Unix time is concerned, even though in reality we sometimes add leap seconds so that some days are one second longer.

Community
  • 1
  • 1
ArneHugo
  • 6,051
  • 1
  • 26
  • 47
-1

Epoch time is GMT time. You'll want a timestamp incorporating your time zone. I'm using the code below for this:

time_t t = time(NULL);

t = timegm(localtime(&t));

if(t %  86400 == 0) {

}
Chris
  • 41
  • 1
  • 2
  • Hey thanks for participating, although I don't think this really answers the question. Despite, your implementation seems to be lacking explanation and formatting – Voy Feb 27 '17 at 15:03