如何将当前date转换为纪元时间戳?
如何将当前date转换为纪元时间戳?
格式当前date:
29.08.2011 11:05:02
这应该做到这一点
import time date_time = '29.08.2011 11:05:02' pattern = '%d.%m.%Y %H:%M:%S' epoch = int(time.mktime(time.strptime(date_time, pattern))) print epoch
假设您正在使用24小时时间格式:
import time; t = time.mktime(time.strptime("29.08.2011 11:05:02", "%d.%m.%Y %H:%M:%S"));
Your code will behave strange if 'TZ' is not set properly, eg 'UTC' or 'Asia/Kolkata' So, you need to do below >>> import time, os >>> d='2014-12-11 00:00:00' >>> p='%Y-%m-%d %H:%M:%S' >>> epoch = int(time.mktime(time.strptime(d,p))) >>> epoch 1418236200 >>> os.environ['TZ']='UTC' >>> epoch = int(time.mktime(time.strptime(d,p))) >>> epoch 1418256000
import time def expires(): '''return a UNIX style timestamp representing 5 minutes from now''' return int(time.time()+300)
使用strptimeparsing时间,并调用time()来获得Unix时间戳。
如果你想要UTC尝试一些gm
函数:
import time import calendar date_time = '29.08.2011 11:05:02' pattern = '%d.%m.%Y %H:%M:%S' utc_epoch = calendar.timegm(time.strptime(date_time, pattern)) print utc_epoch