Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Convert to unix times, and do the calculation with those?


It's not that simple. Luxon docs have a good section on this: https://moment.github.io/luxon/#/math

Some random examples...

When you cross DST switchovers, for example, you may magically gain or lose an hour. A naive milliseconds calculation will miss that. It gets worse because DST in a country isn't a static thing either, but will change with the laws over time. So over decades, you need to have several lookup tables.

And people are ambiguous. Is January 31 plus 1 month the end of February or the beginning of March? What about during a leap year?

And a "day" isn't necessarily 24 hours (because of DST, again). Humans doing day math across those boundaries will just ignore (or really, never think about) the missing or gained hours, but computers have to explicitly account for it.

Then once you throw in different time zones it gets even crazier, especially for the half hour and 45 minute zones.

It's okay to store datetimes in epoch milliseconds (ideally with a separate field for the time zone, not just offset, which holds less information). But you can't easily/correctly do math on that without more specific instructions and cultural/locale adjustments.


No if you are manipulating dates do not use time.

Use an integer type denoting days since the start date. See Modified Julian date.

Thus tomorrow is always 1 more than today - if you have times you need leap seconds.


Thanks for the explanation! (edit - this was meant genuinely, not sarcastically)

Slightly sad to have been downvoted, though, just for making a tentative suggestion (note use of question mark) :'(




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: