python 'datetime.time' 没有 'mktime'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1287598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'datetime.time' has no 'mktime'
提问by Federer
I'm trying to convert a datetime object to a UNIX timestamp (preferably in milliseconds, though I wouldn't mind with and without).
我正在尝试将日期时间对象转换为 UNIX 时间戳(最好以毫秒为单位,尽管我不介意有没有)。
Mktime seems to be the method that usually gets it, however I keep getting the error:
Mktime 似乎是通常获取它的方法,但是我不断收到错误消息:
AttributeError: type object 'datetime.time' has no attribute 'mktime'.
AttributeError: 类型对象“datetime.time”没有属性“mktime”。
Can anyone tell me what I'm doing wrong? I keep going round in circles!
谁能告诉我我做错了什么?我一直在兜圈子!
回答by Daniel Roseman
I think you have done
我想你已经完成了
from datetime import datetime, time
instead of
代替
import time
from datetime import datetime
so that the object called time
is actually coming from the datetime module, not the time module.
这样调用的对象time
实际上来自 datetime 模块,而不是 time 模块。
回答by Diego Favero
Actually, even using the above answer, I still got the same error message.
实际上,即使使用上述答案,我仍然收到相同的错误消息。
I′ve solved my problem using
我已经解决了我的问题
>>>>from time import mktime as mktime
>>>>today = mktime(2012, 12, 21, 0, 0, 0, 0, 0, 0)
I don't know the why, but, it only worked using the alias (as mktime)... can somebody tell me the reason ...
我不知道为什么,但是,它只能使用别名(如 mktime)...有人可以告诉我原因...