我无法在 python 中使用 time() 找出这个错误
这是我的代码:
# Given an Unix timestamp in milliseconds (ts), return a human-readable date and time (hrdt)
def parseTS(ts):
hrdt = time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.time(int(ts)/1000))
return str(hrdt)
我收到此错误:
TypeError: time() takes no arguments (1 given)
更新:
这有效:
hrdt = datetime.datetime.fromtimestamp(int(ts)//1000)
return hrdt
Here is my code:
# Given an Unix timestamp in milliseconds (ts), return a human-readable date and time (hrdt)
def parseTS(ts):
hrdt = time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.time(int(ts)/1000))
return str(hrdt)
I am getting this error:
TypeError: time() takes no arguments (1 given)
UPDATE:
This worked:
hrdt = datetime.datetime.fromtimestamp(int(ts)//1000)
return hrdt
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
time.time(int(ts)/1000) 函数是错误的。
尝试使用 time.ctime、time.gtime() 或 time.localtime() 函数之一来实现您想要的效果。
Python 文档(时间)
The time.time(int(ts)/1000) function is wrong.
Try one of time.ctime, time.gtime() or time.localtime() functions to achieve what you want.
Python Docs (Time)
正如错误所述, time.time() 不接受任何参数,它只是以浮点形式返回当前时间。也许您正在考虑 time.ctime()?
As the error says, time.time() doesn't take any arguments, it just returns current time as floating point. Maybe you are thinking of time.ctime()?
问题是这样的:
并且(正如错误告诉您的那样), time() 需要没有参数。
目前尚不清楚您要做什么,但也许您想要:
或者如果您想要不带浮点部分的时间(以秒为单位),则只需
int(time.time())
。The problem is this:
And (as the error is telling you), time() takes no arguments.
It's unclear what you're trying to do, but maybe you want:
Or just
int(time.time())
if you want the time in seconds without the floating point part.