Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
147 views
in Technique[技术] by (71.8m points)

Python - time difference in milliseconds not working for me

I've read a few posts about this and thought I had some code that worked. If the difference between the 2 values is less than a 1sec then the millisecs displayed is correct.

If the difference is more than a sec, its still only showing me the difference of the millisecs.

As below.

Correct:

 now_wind 2013-08-25 08:43:04.776209 
 first_time_wind 2013-08-25 08:43:04.506301
 time_diff 0:00:00.269908
 diff 269

Wrong - this should be 2000 + 76?:

 now_wind 2013-08-25 08:43:25.660427
 first_time_wind 2013-08-25 08:43:23.583902
 time_diff 0:00:02.076525
 diff 76


 #!/usr/bin/env python
 import datetime
 import time
 from time import sleep
 first_time_wind = datetime.datetime.now()
 sleep (2)
 now_wind = datetime.datetime.now()
 print "now_wind", now_wind
 print "first_time_wind", first_time_wind
 time_diff_wind = (now_wind - first_time_wind)
 print "time_diff", time_diff_wind
 print "diff", time_diff_wind.microseconds / 1000
question from:https://stackoverflow.com/questions/18426882/python-time-difference-in-milliseconds-not-working-for-me

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
>>> a = datetime.datetime.now()
>>> b = datetime.datetime.now()
>>> a
datetime.datetime(2013, 8, 25, 2, 5, 1, 879000)
>>> b
datetime.datetime(2013, 8, 25, 2, 5, 8, 984000)
>>> a - b
datetime.timedelta(-1, 86392, 895000)
>>> b - a
datetime.timedelta(0, 7, 105000)
>>> (b - a).microseconds
105000
>>> (b - a).seconds
7
>>> (b - a).microseconds / 1000
105

your microseconds don't include the seconds that have passed


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...