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 parse the python built-in time module

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

How to analyze the python built-in time module, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

No-parameter function

First explain the timestamp, the so-called timestamp, that is, the number of seconds experienced since 00:00:00 on January 1, 1970, and then you can understand the following function. The following code defaults

From time import *

Implementationmonotonicadjustableresolution'time'GetSystemTimeAsFileTime () FalseTrue0.015625'thread_time'GetThreadTimes () TrueFalse1e-07'process_time'GetProcessTimes () TrueFalse1e-07'monotonic'GetTickCount64 () TrueFalse0.015625'perf_counter'QueryPerformanceCounter () TrueFalse1e-07

Of the above five sets of functions, only the value of time.time () has absolute meaning, and the other values have only relative meaning.

You can view the characteristics of these clocks through the get_clock_info function, and their inputs and outputs are

Implementationmonotonicadjustableresolution'time'GetSystemTimeAsFileTime () FalseTrue0.015625'thread_time'GetThreadTimes () TrueFalse1e-07'process_time'GetProcessTimes () TrueFalse1e-07'monotonic'GetTickCount64 () TrueFalse0.015625'perf_counter'QueryPerformanceCounter () TrueFalse1e-07

Among them

Adjustable is True if the clock can be changed automatically or manually by the system administrator, otherwise False.

Implementation represents the name of the underlying C function used to get the clock value.

Monotonic is True if the clock cannot be turned back, otherwise False.

Resolution represents the clock resolution in seconds.

Next, you can test the characteristics of these clocks.

Def test (n):... ATime = time.time (). ATh = time.thread_time (). APr = time.process_time (). AMo = time.monotonic (). APe = time.perf_counter (). For i in range (int (n)): J = iTunes 2... BTime = time.time (). BTh = time.thread_time (). BPr = time.process_time (). BMo = time.monotonic (). BPe = time.perf_counter (). AStr = fanciaTime = {aTime}, aTh= {aTh}, aPr= {aPr}, aMo= {aMo}, aPe= {aPe}\ n... BStr = fancibTime = {bTime}, bTh= {bTh}, bPr= {bPr}, bMo= {bMo}, bPe= {bPe} '... Print (aStr+bStr)... > > test (1e6) aTime=1634625786.136904,aTh=0.03125,aPr=0.03125,aMo=199082.078,aPe=199085.4751224bTime=1634625786.340363,bTh=0.234375,bPr=0.234375,bMo=199082.281,bPe=199085.6787309 > test (1e6) aTime=1634625789.7817287,aTh=0.234375,aPr=0.234375,aMo=199085.734,aPe=199089.1195357bTime=1634625789.981198,bTh=0.421875,bPr=0.421875,bMo=199085.921,bPe=199089.3195721 > test (1e6) aTime=1634625796.3934195,aTh=0.421875,aPr=0.421875,aMo=199092.343,aPe=199095.731209bTime=1634625796.5789576,bTh=0.609375,bPr=0.609375,bMo=199092.531,bPe=199095.9172852 >

You can clearly see that thread_time and process_time do not change during the interval between calls to test, that is, they do not count the time when a thread or process sleeps.

Generally, in the time module, the two most commonly used functions are time.time () and time.sleep (). The former is used to obtain the timestamp to count the running time of the program, while the latter can pause the thread.

You can test the function of the sleep function through time.thread_time ()

Def test (n):... ATime = time.time (). ATh = time.thread_time (). APr = time.process_time (). Time.sleep (n)... BTime = time.time (). BTh = time.thread_time (). BPr = time.process_time (). AStr = fanciaTime = {aTime}, aTh= {aTh}, aPr= {aPr}\ ntimes. BStr = fancibTime = {bTime}, bTh= {bTh}, bPr= {bPr} '... Print (aStr+bStr)... > > test (1) aTime=1634649370.2819958,aTh=0.640625,aPr=0.640625bTime=1634649371.2862759,bTh=0.640625,bPr=0.640625 > test (1) aTime=1634649372.72013,aTh=0.640625,aPr=0.640625bTime=1634649373.723695,bTh=0.640625,bPr=0.640625 > test (1) time zone concept

Next, we need to introduce some concepts about time.

GMT: Greenwich mean time.

UTC: the world's coordinated time is more accurate than Greenwich.

DST:D, or Daylight, represents daylight saving time.

CST: standard time for the United States, Australia, China and Cuba.

Once you know the concepts of these time zones, you can understand the constants in time:

Constant altzonedaylighttznametimezone

Time zone offset if no non-DST time zone is defined, 0 time zone name local time zone offset struct_time

To better represent time, the struct_time class is encapsulated in time, and its members include

Index attribute value means 0tm_year positive integer year 1tm_monrange [1,12] month 2tm_mdayrange [1,31] month date 3tm_hourrange [0,23] 4tm_minrange [0,59] minutes 5tm_secrange [0,61] seconds 6tm_wdayrange [0,6], Monday is 0 week, that is, the day of 7tm_ydayrange [1,366] in the year 8tm_isdst0, 1 or-1 is the abbreviation of DST-tm_zone time zone name.

-tm_gmtoff deviates to the east of UTC in seconds

After understanding the data structure of struct_time, you can read the following functions.

The one-parameter function gmtime (secs) converts the timestamp into UTC time [struct_time format] localtime (secs), converts the timestamp into local time [struct_time format] ctime (secs), converts the timestamp into UTC time string asctime (secs), converts the time structure into the inverse function of the local time string mktimelocaltime, and converts struct_time into seconds time.strftime (format [, t])

You can format the output of the struct_time with a match, which is converted to

Name implication A week abbreviation% a week name% b month abbreviation% B month name% c appropriate date and time representation

% d month / day, range [01Magne31]% j / year / day, range [001366]% H hours, range [00mage23]% I hours, range [01jue 12]% M minutes, range [00559]% s seconds, range [000.61]% pAM or PM

% m month, range [0119912]

% U Mid-year weeks, range [00BZ 53]

Sunday as the first day% W is the same as the left, Monday as the first day% w Zhong Japan, range [0 (Sunday), 6]

% x appropriate date represents% X appropriate time represents% y century-free year, range [00,0099]% Y belt century year% z time zone offset

% Z time zone name

% literal'% 'character.

Strptime () is its inverse function.

For example

> t = time.strftime ("% a,% d% b% Y% H:%M:%S + 0000", gmtime ()) > t'Tue, 19 Oct 2021 13:46:37 + 0000' > T = time.strptime (t, "% a,% d% Y% H:%M:%S + 0000") > Ttime.struct_time (tm_year=2021, tm_mon=10, tm_mday=19, tm_hour=13, tm_min=46, tm_sec=37, tm_wday=1, tm_yday=292, tm_isdst=-1) finish reading the above Do you know how to parse the built-in time module of python? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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