6

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.

Alastair McCormack
  • 26,573
  • 8
  • 77
  • 100

3 Answers3

13

A simple bit of maths:

int(timestamp//60 * 60)

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

Alastair McCormack
  • 26,573
  • 8
  • 77
  • 100
  • 3
    In case this seems too simplistic: this works because Unix timestamps increase with 86400 every day. This is true for days including [leap seconds](https://stackoverflow.com/questions/16539436/unix-time-and-leap-seconds) as well. @alastair I am sure that you already knew this - just added this for the benefit of other skeptics finding this answer. – bohrax Sep 13 '18 at 06:43
  • Love it! Super simple! – kakhkAtion Oct 03 '19 at 23:28
  • As @bohrax alluded to, you can do the same thing with hours, days, etc. e.g. `(int(time.time()) // 86400) * 86400` – Cfomodz May 27 '22 at 17:20
  • 1
    @Cfomodz, doesn't seem to work: `>>> t = int(time.time()) >>> print( datetime.fromtimestamp(t) ) 2023-05-25 21:32:24 >>> print( datetime.fromtimestamp( (t//86400) * 86400 ) ) 2023-05-25 20:00:00 ` – mrbrahman May 26 '23 at 01:33
  • 1
    Expected to see `2023-05-25 00:00:00` – mrbrahman May 26 '23 at 01:38
  • 1
    @mrbrahman this actually took me a minute to figure out haha. So that *is* correct actually... https://www.epochconverter.com/ in GMT so if you want to round down to the day (this morning at 00:00:00) then you will need to round down to the day (shown above), and then adjust for time zone depending on, well, your time zone, but that *is* the correct timestamp for today at 00:00:00... GMT – Cfomodz Jun 14 '23 at 19:36
  • 1
    @mrbrahman ahhhhh, yes, as I was writing my answer I realized the issue here and why this problem isn't present for rounding (flooring) to minute. //60 doesn't touch hours, and thus is timezone independent, but flooring to day *does* touch hours, and thus you end up in GMT – Cfomodz Jun 14 '23 at 19:43
  • Ah! timezones! Thanks for taking a look. This is the best answer! – mrbrahman Jun 21 '23 at 00:36
3

Or if you're not clever you could use arrow:

>>> import arrow
>>> timestamp = arrow.get(1488728901)
>>> timestamp
<Arrow [2017-03-05T15:48:21+00:00]>
>>> timestamp = timestamp.floor('minute')
>>> timestamp.format('YYYY-MMM-DD HH:mm:ss')
'2017-Mar-05 15:48:00'
Bill Bell
  • 21,021
  • 5
  • 43
  • 58
-1

you can do one thing here lets suppose col has your unix value. modified_value=((col+30*1000)/60*1000).cast("long") -- it will be decimal or integer covert it into proper integer if

modified_value_final = modified_value*60*1000

Example val=1488728901
modifeid_value=(1488728901+30000)/60000 = 24,812.64835
modified_value = 24,812 --after casting to long
final_modified_value = 24812*60000=1,488,720,000
NKSM
  • 5,422
  • 4
  • 25
  • 38