【英文】Python3的time包

Introduction

Notes on the Python3 time package.

Import Module

1
import time

Get the Timestamp of the Current Time

1
time.time()

Convert Timestamp to a Time Tuple

1
t = time.localtime(time.time())

Get Elements from the Time Tuple

Year

1
t.tm_year

Month

  • Range: [1, 12]
1
t.tm_mon

Day of the Month

  • Range: [1, 31]
1
t.tm_mday

Day of the Year

  • Range: [1, 366]
1
t.tm_yday

Hour

  • Range: [0, 23]
1
t.tm_hour

Minute

  • Range: [0, 59]
1
t.tm_min

Second

  • Range: [0, 60] (including leap seconds [0, 61])
1
t.tm_sec

Weekday

1
t.tm_wday

Daylight Saving Time (DST)

Values

-1: Default value, unknown
1: DST is in effect
0: DST is not in effect

1
t.tm_isdst

Format Time

Convert Time to a String

Built-in

1
time.asctime(t)

Custom

1
time.strftime("%Y-%m-%d %H:%M:%S", t)

Convert String to Time

Built-in

<str>: String

1
t = time.mktime(time.strptime(<str>,"%a %b %d %H:%M:%S %Y"))

Custom

<str>: String

1
t = time.mktime(time.strptime(<str>,"%Y-%m-%d %H:%M:%S"))

Conclusion

References

菜鸟笔记
CSDN——洛城-sola