Adding hours to unix time stamp in python

蹲街弑〆低调 提交于 2021-01-27 04:12:18

问题


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?


回答1:


UNIX timestamps are in second units.

end_timestamp = start_timestamp + 5 * 60 * 60



回答2:


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)


来源:https://stackoverflow.com/questions/13924124/adding-hours-to-unix-time-stamp-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!