Reputation: 2945
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
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