sundance
sundance

Reputation: 2945

How to get the number of hours since, from a UTC timestamp?

I have a UTC timestamp and I'd like to calculate the number of hours from then to now in Python. How can I do that?

Thanks!

Upvotes: 1

Views: 585

Answers (1)

Pi Marillion
Pi Marillion

Reputation: 4674

datetime is the module to use here (due to UTC requirement):

def hours_since(timestamp):
    import datetime
    return (datetime.datetime.utcnow() - datetime.datetime.utcfromtimestamp(timestamp)).total_seconds() / 3600.

Example:

hours_since(1385715744.206451)
0.18432851777777778

Upvotes: 5

Related Questions