Python-time module time, datetime

1. Time module time, datetime

1.1 Three time format analysis

Timestamp (timestamp): Generally speaking, the timestamp represents the offset in seconds from 00:00:00 on January 1, 1970. We run "type(time.time())" and the return is a float type.

Format String

Structured time (struct_time): The struct_time tuple has 9 elements and a total of 9 elements: (year, month, day, hour, minute, second, week of the year, day of the year, daylight saving time)

import time
print(time.time())  # 1596091400.0042257 时间戳

print(time.strftime('%Y-%m-%d %X')) 
# 2020-07-30 14:43:20 格式化时间字符串

print(time.localtime())  # 结构化时间 
# 本地时间 
# time.struct_time(tm_year=2020, tm_mon=7, tm_mday=30, tm_hour=14, tm_min=43, tm_sec=20, tm_wday=3, tm_yday=212, tm_isdst=0)

print(time.gmtime())
# UTC时区时间
# time.struct_time(tm_year=2020, tm_mon=7, tm_mday=30, tm_hour=6, tm_min=43, tm_sec=20, tm_wday=3, tm_yday=212, tm_isdst=0)

1.2 Conversion between time formats

The time recognized by the computer can only be in the'time stamp' format, and the time that programmers can process or humans can understand are:'formatted time string','structured time', so there is a time format The conversion relationship is shown in the figure below; the
Insert picture description here
timestamp is converted to the structured time in the current time zone . If the secs parameter is not provided, the current time shall prevail.

import time
# gmtime([secs]) 和localtime()方法类似,gmtime()方法是将一个时间戳转换为UTC时区的struct_time。
a = time.localtime(3333)
b = time.localtime(time.time())
c = time.localtime()
print(a)
print(b)
print(c)

# time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=8, tm_min=55, tm_sec=33, tm_wday=3, tm_yday=1, tm_isdst=0)
# time.struct_time(tm_year=2020, tm_mon=7, tm_mday=30, tm_hour=14, tm_min=54, tm_sec=0, tm_wday=3, tm_yday=212, tm_isdst=0)
# time.struct_time(tm_year=2020, tm_mon=7, tm_mday=30, tm_hour=14, tm_min=54, tm_sec=0, tm_wday=3, tm_yday=212, tm_isdst=0)

mktime(t): Convert a struct_time into a timestamp.

print(time.mktime(time.localtime())) #1473525749.0

strftime(format[, t]): Convert a tuple or struct_time representing time into a formatted time string.

print(time.strftime("%Y-%m-%d %X", time.localtime()))  #2020-07-30 13:49:56

Convert a formatted time string to struct_time. In fact, it and strftime() are inverse operations.

print(time.strptime('2011-05-05 16:37:06', '%Y-%m-%d %X'))
# time.struct_time(tm_year=2011, tm_mon=5, tm_mday=5, tm_hour=16, tm_min=37, tm_sec=6, tm_wday=3, tm_yday=125, tm_isdst=-1)

Insert picture description here

asctime([t]): Express a tuple or struct_time representing time in this form:'Sun Jun 20 23:21:05 1993'.

# 如果没有参数,将会将time.localtime()作为参数传入。
print(time.asctime())#Sun Sep 11 00:43:43 2020

ctime([secs]): Convert a timestamp (a floating point number calculated in seconds) into the form of time.asctime(). If the parameter is not given or is None, the default time.time() will be the parameter. Its function is equivalent to time.asctime(time.localtime(secs)).

print(time.ctime())
print(time.ctime(time.time()))  
# Thu Jul 30 15:11:06 2020
# Thu Jul 30 15:11:06 2020

1.3 datetime

import datetime

print(datetime.datetime.now())  # 返回 2020-07-30 15:13:10.834223
print(datetime.date.fromtimestamp(time.time()))  # 时间戳直接转成日期格式 2020-07-30
print(datetime.datetime.now() + datetime.timedelta(3))  # 当前时间+3天
print(datetime.datetime.now() + datetime.timedelta(-3))  # 当前时间-3天
print(datetime.datetime.now() + datetime.timedelta(hours=3))  # 当前时间+3小时
print(datetime.datetime.now() + datetime.timedelta(minutes=30))  # 当前时间+30分

c_time = datetime.datetime.now()
print(c_time.replace(minute=3, hour=2))  # 时间替换
# 结果
2020-07-30 15:13:10.834223
2020-07-30
2020-08-02 15:13:10.834223
2020-07-27 15:13:10.834223
2020-07-30 18:13:10.834223
2020-07-30 15:43:10.834223
2020-07-30 02:03:10.834223

1.4 Other

# 线程推迟指定的时间运行,单位为秒。
time.sleep(3) # 停三秒后,继续运行

Guess you like

Origin blog.csdn.net/msmso/article/details/107689976