Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to get millisecond time by python

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces python how to obtain millisecond time related knowledge, detailed and easy to understand, simple and fast operation, has a certain reference value, I believe that everyone will have a harvest after reading this python how to obtain millisecond time article, let's take a look at it.

Getting ms-level system time in python can be obtained by importing datetimeprint(datetime.datetime.now ().microsecond)

However, by testing with the following code, it is found that the value returned is not the value of ms, but the value of us:

import datetime def getTime_ms(): return (datetime.datetime.now().hour*3600 + datetime.datetime.now().minute*60+ datetime.datetime.now().second )*1000+ datetime.datetime.now().microsecond/1000 def timetime(): t = getTime_ms() while getTime_ms() - t

< 500: pass a = getTime_ms() print(t, a, a- t) if __name__ == '__main__': from timeit import timeit t = timeit('timetime()', 'from __main__ import timetime', number=1) print(t) 打印结果 (35460614, 35461114, 500) 0.499531984329 上面实验不太直观,换一个直观点的:def timetime(): t1 = datetime.datetime.now() while 1: t2 = datetime.datetime.now() if (t2 - t1).microseconds >

= 1: print(t2,t2.microsecond,t1,t1.microsecond,(t2-t1).seconds) break if __name__ == '__main__': from timeit import timeit t = timeit('timetime()', 'from __main__ import timetime', number=1) print(t) #print (datetime.datetime.now())

output

(datetime.datetime(2019, 4, 3, 10, 6, 20, 461882), 461882, datetime.datetime(2019, 4, 3, 10, 6, 20, 461863), 461863, 0) 7.10487365723e-05

def timetime(): t1 = datetime.datetime.now() while 1: t2 = datetime.datetime.now() if (t2 - t1).microseconds >= 1000: print(t2,t2.microsecond,t1,t1.microsecond,(t2-t1).seconds) break if __name__ == '__main__': from timeit import timeit t = timeit('timetime()', 'from __main__ import timetime', number=1) print(t) #print (datetime.datetime.now())

output

(datetime.datetime(2019, 4, 3, 10, 13, 52, 76548), 76548, datetime.datetime(2019, 4, 3, 10, 13, 52, 75547), 75547, 0) 0.00104904174805

Python datetime removes milliseconds and returns the current time

datetime module returns the current time import datetimeif __name__ == '__main__': a = datetime.datetime.now().replace(microsecond=0) print(a, type(a))

Result of calculation:

2020-05-22 12:13:42 Process finished with exit code 0

The content of this article on "how python gets millisecond time" is introduced here. Thank you for reading! I believe everyone has a certain understanding of "how python obtains millisecond time" knowledge. If you still want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report