简体   繁体   English

python - strtotime相当于?

[英]python - strtotime equivalent?

I'm using this to convert date time strings to a unix timestamp: 我正在使用它将日期时间字符串转换为unix时间戳:

str(int(time.mktime(time.strptime(date,"%d %b %Y %H:%M:%S %Z"))))

However often the date structure isn't the same so I keep getting the following error message: 但是,日期结构通常不一样,所以我不断收到以下错误消息:

time data did not match format: data=Tue, 26 May 2009 19:58:20 -0500 fmt=%d %b %Y %H:%M:%S %Z 时间数据格式不匹配:data = Tue,2009年5月26日19:58:20 -0500 fmt =%d%b%Y%H:%M:%S%Z

Does anyone know of any simply function to convert a string representation of a date/time to a unix timestamp in python? 有没有人知道任何简单的函数将日期/时间的字符串表示转换为python中的unix时间戳? I really don't want to have to open a process to call a php script to echo the timestamp everytime time in a loop :) 我真的不想打开一个进程来调用php脚本来循环每次回应时间戳:)

from dateutil.parser import parse

parse('Tue, 26 May 2009 19:58:20 -0500').strftime('%s')

# returns '1243364300'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM