'Get Epoch timestamp accurate by the day with datetime

I want to get a day-accurate (not hour, minutes, seconds) Epoch timestamp that remains the same throughout the day.

This is accurate by the millisecond (and therefore too accurate):

from datetime import date, datetime
timestamp = datetime.today().strftime("%s")

Is there any simple way to make it less precise?



Solution 1:[1]

It depends what do you want.

If you just want a quick way, either use time.time_ns() or time.time(). Epoch time is used by system (on many OS), and so there is no conversion. The _ns() version avoid floating point maths, so faster.

If you want to store it in more efficient way, you can just do a: (int(time.time()) % (24*60*60) so you get the epoch at start of the day. Epoch contrary most of other times (and GPS time) has all days long 246060 seconds (so discarding leap seconds).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Giacomo Catenazzi