Search code examples
pythontimestampunix-timestamp

How do I round down an epoch timestamp to the nearest minute in Python?


I'd like to round down/floor an epoch/Unix timestamp so as to remove the seconds.

I'm using Python 2.x and Python 3.x

E.g.

1488728901 = Sun, 05 Mar 2017 15:48:21 GMT

to:

1488728880 = Sun, 05 Mar 2017 15:48:00 GMT

There's some great answers by using datetime to manipulate the time but it seems OTT if my input and output are integers.


Solution

  • A simple bit of maths:

    int(timestamp//60 * 60)
    

    (// replicates Python 2.x division (/) where the result is a whole number)