Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding hours to unix time stamp in python

I need to add 5 hours to a certain unix time stamp. Its like start and stop time for a game. So I have the knowledge of start time and the duration of the game. I need to put end time. How this can be done in python?

like image 489
user1371033 Avatar asked Dec 17 '12 23:12

user1371033


2 Answers

UNIX timestamps are in second units.

end_timestamp = start_timestamp + 5 * 60 * 60
like image 105
Jon Gauthier Avatar answered Oct 14 '22 21:10

Jon Gauthier


Could be explicit and work with the following:

>>> from datetime import timedelta, datetime
>>> a = datetime.fromtimestamp(0)
>>> b = a + timedelta(hours=5)
>>> c = time.mktime(b.timetuple())
>>> c
18000.0
>>> datetime.fromtimestamp(c)
datetime.datetime(1970, 1, 1, 6, 0)
like image 30
Jon Clements Avatar answered Oct 14 '22 22:10

Jon Clements