Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a timestamp is a whole hour

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?

like image 505
Wouter Luberti Avatar asked Nov 06 '14 21:11

Wouter Luberti


People also ask

How many digits is a timestamp?

10 digit epoch time format surrounded by brackets (or followed by a comma). The digits must be at the very start of the message.

What is the unit of timestamp?

It's in seconds, because time() returns a unix timestamp, which is the amount of seconds since jan 1 1970. Unix time, or POSIX time, is a system for describing instants in time, defined as the number of seconds that have elapsed since midnight Coordinated Universal Time (UTC), January 1, 1970.

What is the difference between timestamp and datetime in python?

Timestamp is the pandas equivalent of python's Datetime and is interchangeable with it in most cases. It's the type used for the entries that make up a DatetimeIndex, and other timeseries oriented data structures in pandas. Value to be converted to Timestamp. Offset which Timestamp will have.


2 Answers

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
like image 198
Padraic Cunningham Avatar answered Nov 12 '22 03:11

Padraic Cunningham


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.

like image 30
espang Avatar answered Nov 12 '22 03:11

espang