8

I'm building a python application where I get a lot of data from various timeseries (ranging from 1 minute to 1 day). I would only like to store the times (unix timestamps) that are exactly on a whole hour (xx:00 minutes). How do I build this check?

Is a simple if timestamp % 3600 == 0: save the timestamp sufficient enough? Or is there a better way?

Wouter Luberti
  • 105
  • 2
  • 7

2 Answers2

5

use datetime.fromtimestamp

from datetime import datetime  

ts = 1415309268
cts = datetime.fromtimestamp(ts)
print(cts.minute==00)

If you want seconds also:

cts.minute==00 and cts.second==00
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
5

The timestamp is in seconds, so the time mod 3600 should be zero.

if timestamp % 3600 == 0:
    # save the timestamp-value tuple

This is enough, you don't have to make a datetime object and check the minute and second for every timestamp you get.

espang
  • 673
  • 6
  • 6