时间概念
时间戳:格林威治时间1970年01月01日00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数
元组struct_time:python定义了一个时间类,包含9个属性:
属性 | 描述 |
---|---|
tm_year | 年 |
tm_mon | 月,范围1~12 |
tm_mday | 日,范围1~31 |
tm_hour | 小时,范围0~23 |
tm_min | 分钟,范围0~59 |
tm_sec | 秒,范围0~61(60或61是闰秒) |
tm_wday | 星期,范围0~6(周一为0) |
tm_yday | 一年内第几天,范围0~366 |
tm_isdst | 夏时令,-1,0,1 |
五种时间形式
>>> import time
>>> time.time() # 时间戳
1569851683.5405068
>>> time.localtime() # struct_time类型的本地时间
time.struct_time(tm_year=2019, tm_mon=9, tm_mday=30, tm_hour=21, tm_min=55, tm_sec=10, tm_wday=0, tm_yday=273, tm_isdst=0)
>>> time.gmtime() # struct_time类型的utc时间
time.struct_time(tm_year=2019, tm_mon=9, tm_mday=30, tm_hour=13, tm_min=55, tm_sec=25, tm_wday=0, tm_yday=273, tm_isdst=0)
>>> datetime.now() # datetime类型的本地时间
datetime.datetime(2019, 9, 30, 23, 34, 31, 572628)
>>>datetime.utcnow() # datetime类型的utc时间
datetime.datetime(2019, 9, 30, 15, 35, 36, 275686)
时间形式的相互转化
>>> import time
>>> import calendar
>>> from datetime import datetime
>>> time_stamp = time.time()
>>> local_time = time.localtime()
>>> utc_time = time.gmtime()
>>> local_datetime = datetime.now()
>>> utc_datetime = datetime.utcnow()
>>> time.localtime(time_stamp) # 时间戳 --> struct_time类型的本地时间
time.struct_time(tm_year=2019, tm_mon=9, tm_mday=30, tm_hour=22, tm_min=10, tm_sec=37, tm_wday=0, tm_yday=273, tm_isdst=0)
>>> time.gmtime(time_stamp) # 时间戳 --> struct_time类型的utc时间
time.struct_time(tm_year=2019, tm_mon=9, tm_mday=30, tm_hour=14, tm_min=10, tm_sec=37, tm_wday=0, tm_yday=273, tm_isdst=0)
>>> time.mktime(local_time) # struct_time类型的本地时间 --> 时间戳
1569856100.0
>>> calendar.timegm(utc_time) # struct_time类型的utc时间 --> 时间戳
1569856340
>>> local_time.timestamp() # datetime类型的本地时间 --> 时间戳
1569857909.824448
>>> datetime.fromtimestamp(time_stamp) # 时间戳 --> datetime类型的本地时间
datetime.datetime(2019, 9, 30, 22, 10, 37, 822788)
>>> datetime.utcfromtimestamp(time_stamp) # 时间戳 --> datetime类型的utc时间
datetime.datetime(2019, 9, 30, 14, 10, 37, 822788)
>>> local_datetime.timetuple() # datetime类型的本地时间 -- > struct_time类型的本地时间
time.struct_time(tm_year=2019, tm_mon=9, tm_mday=30, tm_hour=23, tm_min=42, tm_sec=27, tm_wday=0, tm_yday=273, tm_isdst=-1)
>>> utc_datetime.utctimetuple() # # datetime类型的utc时间 -- > struct_time类型的utc时间
time.struct_time(tm_year=2019, tm_mon=9, tm_mday=30, tm_hour=15, tm_min=44, tm_sec=19, tm_wday=0, tm_yday=273, tm_isdst=0)
时间形式转化为字符串
>>> import time
>>> from datetime import datetime
>>> time_stamp = time.time()
>>> local_time = time.localtime()
>>> utc_time = time.gmtime()
>>> time.ctime(time_stamp) # 时间戳 --> 本地时间字符串
'Mon Sep 30 22:10:37 2019'
>>> time.asctime(local_time) # struct_time类型的本地时间 --> 字符串
'Mon Sep 30 23:00:26 2019'
>>> time.asctime(utc_time) # struct_time类型的utc时间 --> 字符串
'Mon Sep 30 15:03:30 2019'
>>> time.strftime('%Y-%m-%d %H:%M:%S, %w', local_time()) # struct_time类型的本地时间 --> 自定义字符串
'2019-09-30 23:15:14, 1'
>>> time.strftime('%d/%m/%Y', utc_time) # struct_time类型的utc时间 --> 自定义字符串
'30/09/2019'
>>> datetime.strptime('2019-09-30 23:20:00, 1', '%Y-%m-%d %H:%M:%S, %w') # 字符串 --> datetime.datetime格式(字符串与时间格式要完全一致)
datetime.datetime(2019, 9, 30, 23, 20)
datetime类型时间
import datetime
>>> today = datetime.datetime.now() # datetime.datetime(2019, 9, 30, 23, 48, 6, 74148)
>>> int(today.strftime('%w')) # 1(周一) ~ 7(周日)
1
日期的加减法
>>> today + datetime.timedelta(days=1) # +1天
datetime.datetime(2019, 10, 1, 23, 48, 6, 74148)
>>> today + datetime.timedelta(minutes=1) # +1分钟
datetime.datetime(2019, 9, 30, 23, 49, 6, 74148)
>>> today - datetime.timedelta(seconds=3600) # -3600秒
datetime.datetime(2019, 9, 30, 22, 48, 6, 74148)
# 等价于:
>>> today + datetime.timedelta(seconds=-3600) # -3600秒
datetime.datetime(2019, 9, 30, 22, 48, 6, 74148)