time和datetime模块
0.1 time模块
import time
0.1.1 时间戳
时间戳(timestamp):时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量。
time_stamp = time.time()
print(time_stamp, type(time_stamp))
1552551519.291029 <class 'float'>
0.1.2 格式化时间
格式化的时间字符串(format string):格式化时间表示的是普通的字符串格式的时间。
format_time = time.strftime("%Y-%m-%d %X")
print(format_time, type(format_time))
2019-03-07 16:22:11 <class 'str'>
0.1.3 结构化时间
结构化的时间(struct time):struct_time元组共有9个元素共九个元素,分别为(年,月,日,时,分,秒,一年中第几周,一年中第几天,夏令时)
print('本地时区的struct_time:\n{}'.format(time.localtime()))
print('UTC时区的struct_time:\n{}'.format(time.gmtime()))
本地时区的struct_time:
time.struct_time(tm_year=2019, tm_mon=3, tm_mday=7, tm_hour=16, tm_min=22, tm_sec=11, tm_wday=3, tm_yday=66, tm_isdst=0)
UTC时区的struct_time:
time.struct_time(tm_year=2019, tm_mon=3, tm_mday=7, tm_hour=8, tm_min=22, tm_sec=11, tm_wday=3, tm_yday=66, tm_isdst=0)
# 结构化时间的基准时间
print(time.localtime(0))
time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=8, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)
# 结构化时间的基准时间上增加一年时间
print(time.localtime(3600*24*365))
time.struct_time(tm_year=1971, tm_mon=1, tm_mday=1, tm_hour=8, tm_min=0, tm_sec=0, tm_wday=4, tm_yday=1, tm_isdst=0)
0.1.4 格式化字符串的时间格式
%a Locale’s abbreviated weekday name.
%A Locale’s full weekday name.
%b Locale’s abbreviated month name.
%B Locale’s full month name.
%c Locale’s appropriate date and time representation.
%d Day of the month as a decimal number [01,31].
%H Hour (24-hour clock) as a decimal number [00,23].
%I Hour (12-hour clock) as a decimal number [01,12].
%j Day of the year as a decimal number [001,366].
%m Month as a decimal number [01,12].
%M Minute as a decimal number [00,59].
%p Locale’s equivalent of either AM or PM. (1)
%S Second as a decimal number [00,61]. (2)
%U Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0. (3)
%w Weekday as a decimal number [0(Sunday),6].
%W Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0. (3)
%x Locale’s appropriate date representation.
%X Locale’s appropriate time representation.
%y Year without century as a decimal number [00,99].
%Y Year with century as a decimal number.
%z Time zone offset indicating a positive or negative time difference from UTC/GMT of the form +HHMM or -HHMM, where H represents decimal hour digits and M represents decimal minute digits [-23:59, +23:59].
%Z Time zone name (no characters if no time zone exists).
%% A literal '%' character.
0.1.5 不同格式时间的转换
如上图所示,我们总能通过某些方法在结构化时间-格式化时间-时间戳三者之间进行转换,下面我们将用代码展示如何通过这些方法转换时间格式。
# 结构化时间
now_time = time.localtime()
print(now_time)
time.struct_time(tm_year=2019, tm_mon=3, tm_mday=7, tm_hour=16, tm_min=22, tm_sec=11, tm_wday=3, tm_yday=66, tm_isdst=0)
# 把结构化时间转换为时间戳格式
print(time.mktime(now_time))
1551946931.0
# 把结构化时间转换为格式化时间
# %Y年-%m月-%d天 %X时分秒=%H时:%M分:%S秒
print(time.strftime("%Y-%m-%d %X", now_time))
2019-03-07 16:22:11
# 把格式化时间化为结构化时间,它和strftime()是逆操作
print(time.strptime('2013-05-20 13:14:52', '%Y-%m-%d %X'))
time.struct_time(tm_year=2013, tm_mon=5, tm_mday=20, tm_hour=13, tm_min=14, tm_sec=52, tm_wday=0, tm_yday=140, tm_isdst=-1)
# 把结构化时间表示为这种形式:'Sun Jun 20 23:21:05 1993'。
print(time.asctime())
Thu Mar 7 16:22:11 2019
# 如果没有参数,将会将time.localtime()作为参数传入。
print(time.asctime(time.localtime()))
Thu Mar 7 16:22:11 2019
# 把一个时间戳转化为time.asctime()的形式。
print(time.ctime())
Thu Mar 7 16:22:11 2019
# 如果参数未给或者为None的时候,将会默认time.time()为参数。它的作用相当于time.asctime(time.localtime(secs))。
print(time.ctime(time.time()))
Thu Mar 7 16:22:11 2019
0.1.6 其他用法
# 推迟指定的时间运行,单位为秒
start = time.time()
time.sleep(3)
end = time.time()
print(end-start)
3.0005428791046143
0.2 datetime模块
常用:
from datetime import datetime,timedelta
this_date = datetime.now().date()
print(this_date, type(this_date)) # 当前所处的年月日 2018-03-10 <class 'datetime.date'>
str_nowtime = datetime.now().strftime('%Y-%m-%d %H')
print(str_nowtime,type(str_nowtime)) # 2018-03-10 16 <class 'str'>
day = timedelta(days=1)
print(day, type(day)) # 一天 1 day, 0:00:00 <class 'datetime.timedelta'>
now = datetime.now() # 具体的年月日时分秒
print(now,type(now)) # 2018-03-10 16:40:48.420261 <class 'datetime.datetime'>
this_year = now.year
print(this_year, type(this_year)) # 2018 <class 'int'>
this_month = now.month
print(this_month, type(this_month)) # 3 <class 'int'>
today = now.day
print(today,type(today)) # 几天是几号 10 <class 'int'>
this_year_have_months = range(1, now.month + 1)
print(this_year_have_months) # range(1, 4) # 从已经过去的月份
this_year_next_months = range(now.month + 1, 13)
print(this_year_next_months) # 今年剩下的月份 range(4, 13)
last_year = int(now.year) - 1
print(last_year,type(last_year)) # 去年 2017 <class 'int'>
start = now + timedelta(days=-1)
print(start) # 2020-03-09 17:10:55.553623
生成最近三天的每个小时时间:
from datetime import datetime,timedelta
now_hour= datetime.now() # 当前所处的整点小时数
def before_72hours(now_hour,hours):
hour = timedelta(hours=1)
print(hour,type(hour)) # 1:00:00 <class 'datetime.timedelta'>
for i in range(hours):
yield (now_hour - hour * i).strftime('%Y-%m-%d %H')
print(now_hour) # 当前时间 2020-07-10 16:26:28.688087
for i in before_72hours(now_hour, 72):
print(i)
生成最近的30天:
from datetime import datetime,timedelta
this_date = datetime.now().date()
data_list =[]
def gen_dates(this_date, days):
day = timedelta(days=1)
for i in range(days):
yield (this_date - day * i)
for everyday in gen_dates(this_date, 30):
print(everyday,type(everyday)) # 2018-03-10 <class 'datetime.date'>
data_list.append(everyday)
print(data_list[::-1])
# [datetime.date(2018, 2, 9), datetime.date(2018, 2, 10), .....,datetime.date(2018, 3, 10)]
'''
2018-03-10 <class 'datetime.date'>
2018-03-09 <class 'datetime.date'>
2018-03-08 <class 'datetime.date'>
2018-03-07 <class 'datetime.date'>
2018-03-06 <class 'datetime.date'>
2018-03-05 <class 'datetime.date'>
2018-03-04 <class 'datetime.date'>
2018-03-03 <class 'datetime.date'>
2018-03-02 <class 'datetime.date'>
2018-03-01 <class 'datetime.date'>
2018-02-28 <class 'datetime.date'>
2018-02-27 <class 'datetime.date'>
2018-02-26 <class 'datetime.date'>
2018-02-25 <class 'datetime.date'>
2018-02-24 <class 'datetime.date'>
2018-02-23 <class 'datetime.date'>
2018-02-22 <class 'datetime.date'>
2018-02-21 <class 'datetime.date'>
2018-02-20 <class 'datetime.date'>
2018-02-19 <class 'datetime.date'>
2018-02-18 <class 'datetime.date'>
2018-02-17 <class 'datetime.date'>
2018-02-16 <class 'datetime.date'>
2018-02-15 <class 'datetime.date'>
2018-02-14 <class 'datetime.date'>
2018-02-13 <class 'datetime.date'>
2018-02-12 <class 'datetime.date'>
2018-02-11 <class 'datetime.date'>
2018-02-10 <class 'datetime.date'>
2018-02-09 <class 'datetime.date'>
'''