Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use the time library of Python

Shulou Source: shulou.com Published: 2022-05-31 22:44:44 10月27日 Update

This article introduces the knowledge of "how to use Python's time library". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Time stamp

Timestamp 1970.1.1 to the specified time to interval, in seconds

Import timeprint (time.time ())

Output:

1649834054.98593

Calculate the timestamp of an hour ago

# calculate the timestamp print (time.time ()-3600) one hour ago

Output:

1649830637.5699048

two。 Structured time object

Get the current time

# get current time st = time.localtime () print (type (st)) print (st)

Output:

Time.struct_time (tm_year=2022, tm_mon=4, tm_mday=13, tm_hour=15, tm_min=19, tm_sec=24, tm_wday=2, tm_yday=103, tm_isdst=0)

St is essentially a tuple meta-ancestor, which contains nine elements.

Print (f "Today is {st [0]}-{st [1]}-{st [2]}")

Output:

Today is 2022-4-13.

Print (f "Today is the week {st.tm_wday+1}")

The properties in this object are read-only, and we can only view them, not modify them:

St.tm_wday = 3

An error will be reported at this time:

Traceback (most recent call last):

File "/ Users/liuhuanhuan/PycharmProjects/Pytorch_frame/python/python basic Code exercise / time/01.py of python Standard Library", line 18, in

St.tm_wday = 3

AttributeError: readonly attribute

3. Formatted time string # formatted time string print (time.ctime ())

Output:

Wed Apr 13 15:28:25 2022

# strftime ("Y-%m-%d H:%M:%s") print (time.strftime ("Y-%m-%d H:%M:%S"))

Output:

2022-04-13 15:32:02

Print (time.strftime ("% Y year -% m month -% d% H:% M minutes:% S seconds"))

Output

2022-April-13 15:00: 32 minutes: 45 seconds

Print (time.strftime ("% Y years -% m months -% d% H:% M minutes:% S seconds% a") print ("% Y years -% m months -% d days% H:% M minutes:% S seconds% A") print ("% Y years -% m months -% d days% H:% M minutes:% S seconds% b") print (time.strftime ("% Y years -% Y years -% S seconds% b") M month -% d% H:% M minutes:% S seconds% B "))

Output:

2022-April-13 15:00: 34 minutes: 23 seconds Wed

2022-April-13 15:00: 34 minutes: 23 seconds Wednesday

2022-April-13 15:00: 34 minutes: 23 seconds Apr

2022-April-13 15:00: 34 minutes: 23 seconds April

Sleep, wait time

Print ("start") time.sleep (2) print ("end")

Calculation program calculation time:

T1 = time.time () print ("start") time.sleep (2) print ("end") T2 = time.time () print (f "interval {t2-t1}")

Output:

Start

End

Interval 2.005164861679077

4. Conversion between three formats

1. The timestamp is converted to the object's

# the two effects are the same print (time.gmtime ()) print (time.gmtime (time.time ()) print (time.localtime ()) print (time.localtime (time.time ()-3600))

Output

Time.struct_time (tm_year=2022, tm_mon=4, tm_mday=13, tm_hour=7, tm_min=42, tm_sec=26, tm_wday=2, tm_yday=103, tm_isdst=0)

Time.struct_time (tm_year=2022, tm_mon=4, tm_mday=13, tm_hour=7, tm_min=42, tm_sec=26, tm_wday=2, tm_yday=103, tm_isdst=0)

Time.struct_time (tm_year=2022, tm_mon=4, tm_mday=13, tm_hour=15, tm_min=42, tm_sec=26, tm_wday=2, tm_yday=103, tm_isdst=0)

Time.struct_time (tm_year=2022, tm_mon=4, tm_mday=13, tm_hour=14, tm_min=42, tm_sec=26, tm_wday=2, tm_yday=103, tm_isdst=0)

2.# structured object conversion timestamp

# structured object to timestamp print (time.time ()) print (time.mktime (time.localtime ()

Output:

1649835895.358733

1649835895.0

3. Structured object to time string

# structured object to time string print (time.strftime ("% Y-%m-%d% H:%m:%S", time.localtime ()) print (time.strftime ("% Y-%m-%d% H:%m:%S", time.gmtime (time.time ()

Output:

2022-04-13 15:04:20

2022-04-13 07:04:20

This is the end of the content of "how to use Python's time Library". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Time output object structure structure character string format content hour more knowledge utility achievement next between code element unit dilemma Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Xiaomi Apple Huawei NVidia