python2.7:如何将datetime.timedelta转换为int?

时间:2019-03-19 18:23:40

标签: python python-2.7

我写了一个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'

如何在python 2.7中将datetime.timedelta转换为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.

1 个答案:

答案 0 :(得分:0)

您可以在datetime.timedelta对象上调用方法total_seconds()并转换为int。

示例:

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

    return int(time_)