"python2.7: 如何将datetime.timedelta转换为整数?"

2024-06-10 20:03:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我编写了一个python函数来获取自1970-01-01以来的毫秒数:

def TimestampMillisec64():
    datetime_ = (datetime.datetime.utcnow() - datetime.datetime(1970, 1, 1))

    time_ = datetime_ * 1000

    return int(time_) # <-- TypeError: int() argument must be a string or a number, not 'datetime.timedelta'

获取错误:

TypeError: int() argument must be a string or a number, not 'datetime.timedelta'

如何转换日期时间.timedelta在python2.7中是int吗?你知道吗

TypeError: int() argument must be a string or a number, not 'datetime.timedelta'

Traceback (most recent call last)
File "/Users/user/company/istio/istio/samples/bookinfo/src/productpage/venv/lib/python2.7/site-packages/flask/app.py", line 1997, in __call__
return self.wsgi_app(environ, start_response)
File "/Users/user/company/istio/istio/samples/bookinfo/src/productpage/venv/lib/python2.7/site-packages/flask/app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "/Users/user/company/istio/istio/samples/bookinfo/src/productpage/venv/lib/python2.7/site-packages/flask/app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Users/user/company/istio/istio/samples/bookinfo/src/productpage/venv/lib/python2.7/site-packages/flask/app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "/Users/user/company/istio/istio/samples/bookinfo/src/productpage/venv/lib/python2.7/site-packages/flask/app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Users/user/company/istio/istio/samples/bookinfo/src/productpage/venv/lib/python2.7/site-packages/flask/app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Users/user/company/istio/istio/samples/bookinfo/src/productpage/venv/lib/python2.7/site-packages/flask/app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "/Users/user/company/istio/istio/samples/bookinfo/src/productpage/venv/lib/python2.7/site-packages/flask/app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/user/company/istio/istio/samples/bookinfo/src/productpage/productpage.py", line 277, in responseTimeApi
millis = TimestampMillisec64()
File "/Users/user/company/istio/istio/samples/bookinfo/src/productpage/productpage.py", line 303, in TimestampMillisec64
return int(time_)
TypeError: int() argument must be a string or a number, not 'datetime.timedelta'
The debugger caught an exception in your WSGI application. You can now look at the traceback which led to the error.
To switch between the interactive traceback and the plaintext one, you can click on the "Traceback" headline. From the text traceback you can also create a paste of it. For code execution mouse-over the frame you want to debug and click on the console icon on the right side.

You can execute arbitrary Python code in the stack frames and there are some extra helpers available for introspection:

dump() shows all variables in the frame
dump(obj) dumps all that's known about the object
Brought to you by DON'T PANIC, your friendly Werkzeug powered traceback interpreter.

Tags: theinpysrcappdatetimelineusers
1条回答
网友
1楼 · 发布于 2024-06-10 20:03:47

您可以在上调用方法total \u seconds()日期时间.timedelta对象并强制转换为int

示例:

def TimestampMillisec64():
    datetime_ = (datetime.datetime.utcnow() - datetime.datetime(1970, 1, 1))
    datetime_ = datetime_.total_seconds()
    time_ = datetime_ * 1000

    return int(time_)

相关问题 更多 >