-2

I wrote a python function to get the number of milliseconds since 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'

Getting an error:

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

How do I convert datetime.timedelta to an int in python 2.7?

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.
user674669
  • 10,681
  • 15
  • 72
  • 105

1 Answers1

0

You can call method total_seconds() on datetime.timedelta object and cast to int.

Example:

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

    return int(time_)
  • This won't give millisecond precision. – user674669 Mar 19 '19 at 21:27
  • why not? I ran following, ```import datetime def TimestampMillisec64(): datetime_ = (datetime.datetime.utcnow() - datetime.datetime(1970, 1, 1)) print 'Micro seconds: %s' % str(datetime_.microseconds) datetime_ = datetime_.total_seconds() print 'Total seconds: %s' % str(datetime_) time_ = datetime_ * 1000 return int(time_) print 'Total milliseconds: %s' % str(TimestampMillisec64())``` and got following result ```Micro seconds: 877000 Total seconds: 1553045009.88 Total milliseconds: 1553045009877``` – amageswaran Mar 20 '19 at 01:24