Universal Time,UT,世界时,包含UT0
、UT1
、UT2
等。
Greenwich Mean Time,GMT,格林尼治平时,即UT0
,英国伦敦皇家格林尼治天文台(指旧址,本初子午线所在地)当地的平太阳时。前世界标准时。
注:Greenwich曾译为格林威治,今多用格林尼治。
Coordinated Universal Time(英语),Temps Universel Coordonné(法语),(英法battle后)简称UTC,协调世界时,对标UT1
但不等同,UTC基于国际原子时,通过不规则的加入闰秒来抵消地球自转变慢的影响,闰秒在必要的时候会被插入到UTC中,以保证协调世界时与世界时UT1相差不超过0.9秒。现世界标准时。
Daylight Saving Time,DST,夏令时,日光节约时间,一般在天亮早的夏季人为将时间调快一小时,整体调整社会作息时间,可以使人早睡早起,减少照明量,以充分利用光照资源,从而节约照明用电。(在不实行或取消实行夏令时的地区,有的工厂或学校会调整上下班或上下学时间以达到夏令时效果)
Time Zone,时区
理论时区以被15整除的经线为中心,向东西两侧延伸7.5度,即每15°划分一个时区,共24个时区,本初子午线所在时区为中时区,东十二区和西十二区为同一个时区。理论时区的时间采用其中央经线(或标准经线)的地方时,每差一个时区,区时相差一个小时,为避免东西十二区同一地方日期却相差一天的矛盾,提出国际日期线的概念,国际日期线东侧比西侧迟整整24小时。
法定时区以国界线或行政区分界线划分实际时区,即法定时区。
中国理论时区有东五区、东六区、东七区、东八区、东九区,法定时区为东八区。
注:按国际日期线划分,基里巴斯共和国会被划分到国际日期线两边,两地计时将相差一天而造成不便。基里巴斯共和国将菲尼克斯群岛与莱恩群岛的法定时区分别订为UTC+13与UTC+14,令其与位于UTC+12时区的吉尔伯特群岛同属一天。
Python标准库
# datetime模块中类继承关系
object
timedelta
tzinfo
timezone
time
date
datetime
时区抽象基类
时区,直接继承tzinfo
class datetime.timezone(offset, name=None)
import datetime
print(datetime.timezone.utc) # UTC
print(datetime.timezone(datetime.timedelta(0))) # UTC
时间间隔
class datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)
import datetime
print(datetime.timedelta(microseconds=-1)) # -1 day, 23:59:59.999999
class datetime.time(hour=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold=0)
import datetime
print(datetime.time(4, 23, 1, 384)) # 04:23:01.000384
print(datetime.time.fromisoformat('04:23:01')) # 04:23:01
print(datetime.time.fromisoformat('T04:23:01')) # 04:23:01
print(datetime.time.fromisoformat('T042301')) # 04:23:01
print(datetime.time.fromisoformat('04:23:01.000384')) # 04:23:01.000384
print(datetime.time.fromisoformat('04:23:01,000')) # 04:23:01
print(datetime.time.fromisoformat('04:23:01+08:00')) # 04:23:01+08:00
print(datetime.time.fromisoformat('04:23:01Z')) # 04:23:01+00:00
print(datetime.time.fromisoformat('04:23:01+00:00')) # 04:23:01+00:00
# ISO 8601 https://zh.wikipedia.org/wiki/ISO_8601
# https://docs.python.org/zh-cn/3/library/datetime.html#datetime.time.fromisoformat
class datetime.date(year, month, day)
import datetime
print(datetime.date(2023, 9, 17)) # 2023-09-17
print(datetime.date.fromisoformat('2023-09-17')) # 2023-09-17
datetime直接继承date
class datetime.datetime(year, month, day, hour=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold=0)
from datetime import datetime, date, time, timezone
print(datetime.combine(date(2005, 7, 14), time(12, 30))) # 2005-07-14 12:30:00
print(datetime.now(timezone.utc)) # 2023-09-17 09:52:04.690051+00:00
print(datetime.now().tzinfo) # None
注:一些datetime类方法会默认将naive datetime object
作为本地时区时间,注意Aware Object
和 Naive Object
转换。
time、date、datetime支持strftime(format),datetime支持strptime(date_string, format)
https://docs.python.org/zh-cn/3/library/datetime.html#strftime-and-strptime-format-codes
Python标准库,time模块中函数主要基于所在平台C语言库,同一函数在不同平台可能有不同执行结果。
import time
# 时间戳
print(time.time()) # 1694948259.8878381
# 纳秒
print(time.time_ns()) # 1694948259887838100
# 格式化时间
print(time.strftime("%Y-%m-%d %H:%H:%S")) # 2023-09-17 18:18:39
# 结构化时间
print(time.struct_time.tm_min) #
print(
time.localtime()) # time.struct_time(tm_year=2023, tm_mon=9, tm_mday=17, tm_hour=18, tm_min=57, tm_sec=39, tm_wday=6, tm_yday=260, tm_isdst=0)
print(
time.gmtime()) # time.struct_time(tm_year=2023, tm_mon=9, tm_mday=17, tm_hour=10, tm_min=57, tm_sec=39, tm_wday=6, tm_yday=260, tm_isdst=0)
print(time.sleep(1)) # None 线程睡眠
print(time.strptime('2000-12.31',
'%Y-%m.%d')) # time.struct_time(tm_year=2000, tm_mon=12, tm_mday=31, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=366, tm_isdst=-1)
print(time.asctime()) # Sun Sep 17 19:00:07 2023
print(time.ctime()) # Sun Sep 17 19:00:07 2023
三方库
https://dateutil.readthedocs.io/en/stable/
pip install python-dateutil
from dateutil.relativedelta import relativedelta, MO
import datetime
t = datetime.datetime.now()
print(t) # 2023-09-17 19:49:52.051939
# 相对参数
print(t - relativedelta(years=1)) # 2022-09-17 19:49:52.051939
print(t - relativedelta(months=1)) # 2023-08-17 19:49:52.051939
print(t - relativedelta(days=1)) # 2023-09-16 19:49:52.051939
print(t - relativedelta(weeks=1)) # 2023-09-10 19:49:52.051939
print(t - relativedelta(hours=1)) # 2023-09-17 18:49:52.051939
print(t - relativedelta(minutes=1)) # 2023-09-17 19:48:52.051939
print(t - relativedelta(seconds=1)) # 2023-09-17 19:49:51.051939
# 绝对参数
print(t - relativedelta(year=1)) # 0001-09-17 19:49:52.051939
print(t - relativedelta(month=1)) # 2023-01-17 19:49:52.051939
print(t - relativedelta(day=1)) # 2023-09-01 19:49:52.051939
print(t - relativedelta(hour=1)) # 2023-09-17 01:49:52.051939
print(t - relativedelta(minute=1)) # 2023-09-17 19:01:52.051939
print(t - relativedelta(second=1)) # 2023-09-17 19:49:01.051939
print(t + relativedelta(year=1)) # 0001-09-17 19:49:52.051939
print(t + relativedelta(month=1)) # 2023-01-17 19:49:52.051939
print(t + relativedelta(day=1)) # 2023-09-01 19:49:52.051939
print(t + relativedelta(hour=1)) # 2023-09-17 01:49:52.051939
print(t + relativedelta(minute=1)) # 2023-09-17 19:01:52.051939
print(t + relativedelta(second=1)) # 2023-09-17 19:49:01.051939
print(t + relativedelta(weekday=MO(1))) # 2023-09-18 19:49:52.051939
print(t - relativedelta(weekday=MO(1))) # 2023-09-18 19:49:52.051939
print(t + relativedelta(weekday=MO(-1))) # 2023-09-11 19:49:52.051939
print(t - relativedelta(weekday=MO(-1))) # 2023-09-11 19:49:52.051939
# 若t为周一,则MO(1)、MO(-1)不会改变t