Timestamps in Python
[b]Convert datetime.datetime object to unix timestamp:[/b]
[code]
def timestamp(datetime_obj):
""" Convert datetime object to timestamp """
if type(datetime_obj) is datetime.datetime:
return int(time.mktime(datetime_obj.timetuple()))
else:
return int(datetime_obj)
[/code]
[b]Convert unix timestamp to datetime.datetime object:[/b]
[code]
def from_timestamp(timestamp):
""" Convert timestamp to datetime object"""
return datetime.datetime.fromtimestamp(timestamp)
[/code]
[b]Return current unix timestamp:[/b]
[code]
def timestamp_now():
""" Return current unix timestamp """
return timestamp(datetime.datetime.now())
[/code]
Подписаться на:
Комментарии к сообщению (Atom)
Комментариев нет:
Отправить комментарий