What’s Delorean?
Delorean是Python里一个很方便的日期时间库,可以让人更简单省心的获取时间,不必受困于时间模块中的各种问题。
Delorean是电影《回到未来》里的那辆极为炫酷的鸥翼汽车,采用这部电影里的非常具有代表性的汽车的名字作为库名,作者估计也是想表达使用这个库能让你在时空里任意遨游,没有掣肘。
项目地址:https://delorean.readthedocs.io/en/latest/
Delorean: Time Travel Made Easy.
确定几个概念。
- navie datetime:没有指定时区的datetime对象
- localized datetime:指定时区的datetime对象
- localizing:指定市区的的navie datetime
- normalizing:datetime时区切换
安装Delorean
pip3 install Delorean
导入
from delorean import Delorean
使用
d = Delorean()
print(d)
Delorean(datetime=datetime.datetime(2018, 5, 10, 8, 52, 23, 560811), timezone='UTC')
``这里默认的是UTC时间``
转换为国内时区
d = d.shift("Asia/Shanghai")
print(d)
Delorean(datetime=datetime.datetime(2018, 5, 10, 16, 52, 23, 560718), timezone='Asia/Shanghai')
当然也可以打印datetime、date
print(d.datetime, d.date)
2018-05-10 16:58:22.397155+08:00 2018-05-10
转换成datetime
>>> d.date
datetime.date(2019, 3, 14)
>>> d.datetime
datetime.datetime(2019, 3, 14, 11, 26, 19, 941079, tzinfo=<DstTzInfo 'Asia/Shanghai' CST+8:00:00 STD>)
查看无时区时间及时间戳
>>> d.epoch
1552534638.322473
>>> d.naive
datetime.datetime(2019, 3, 14, 3, 37, 18, 322473)
用unix时间戳初始化Delorean
>>> from delorean import epoch
>>> epoch(1357971038.102223).shift("US/Eastern")
Delorean(datetime=datetime.datetime(2013, 1, 12, 1, 10, 38, 102223), timezone='US/Eastern')
初始化后,就可以方便的切换到自己所需的时区
Delorean也可以使用指定的datetime对象进行初始化,Delorean会自动处理时区和时间
>>> import pytz
>>> import datetime
>>> tz=pytz.timezone('US/Pacific')
>>> dt = tz.localize(datetime.datetime.utcnow())
>>> dt
datetime.datetime(2019, 3, 14, 3, 48, 40, 316686, tzinfo=<DstTzInfo 'US/Pacific' PDT-1 day, 17:00:00 DST>)
>>> d = Delorean(datetime=dt)
>>> d
Delorean(datetime=datetime.datetime(2019, 3, 14, 3, 48, 40, 316686), timezone='US/Pacific')
>>> d = Delorean(datetime=dt, timezone="US/Eastern")
>>> d
Delorean(datetime=datetime.datetime(2019, 3, 14, 3, 48, 40, 316686), timezone='US/Pacific')
Delorean支持timedelta的时间加减法。Delorean可以使用timedelta进行加减,得到一个Delorean对象
>>> d = Delorean()
>>> d
Delorean(datetime=datetime.datetime(2019, 3, 14, 3, 52, 8, 811679), timezone='UTC')
>>> d +=datetime.timedelta(hours=2)
>>> d
Delorean(datetime=datetime.datetime(2019, 3, 14, 5, 52, 8, 811679), timezone='UTC')
>>> d -=datetime.timedelta(hours=2)
>>> d
Delorean(datetime=datetime.datetime(2019, 3, 14, 3, 52, 8, 811679), timezone='UTC')
>>> d2=d+datetime.timedelta(hours=2)
>>> d2-d
datetime.timedelta(0, 7200)
Delorean提供多种方法获取一个指定的时间,如明年或者下周三
Delorean提供了一些方便的方法进行如上操作。
>>> d = Delorean()
>>> d
Delorean(datetime=datetime.datetime(2019, 3, 14, 3, 56, 30, 375769), timezone='UTC')
>>> d.next_tuesday()
Delorean(datetime=datetime.datetime(2019, 3, 19, 3, 56, 30, 375769), timezone='UTC')
>>> d.last_tuesday()
Delorean(datetime=datetime.datetime(2019, 3, 12, 3, 56, 30, 375769), timezone='UTC')
>>> d.last_tuesday(2).midnight
datetime.datetime(2019, 3, 5, 0, 0, tzinfo=<UTC>)
过滤
通常情况下我们不关心有多少微妙或者多少秒。例如,我们很难区别同一分钟的两个datetime对象。我们把不关心的字段设置为0。
Delorean提供了很方便的方法按照微妙、秒、分钟、小时进行过滤
>>> d = Delorean()
>>> d
Delorean(datetime=datetime.datetime(2019, 3, 14, 4, 0, 50, 597357), timezone='UTC')
>>> d.truncate('second')
Delorean(datetime=datetime.datetime(2019, 3, 14, 4, 0, 50), timezone='UTC')
>>> d.truncate('hour')
Delorean(datetime=datetime.datetime(2019, 3, 14, 4, 0), timezone='UTC')
>>> d.truncate('month')
Delorean(datetime=datetime.datetime(2019, 3, 1, 0, 0), timezone='UTC')
>>> d.truncate('year')
Delorean(datetime=datetime.datetime(2019, 1, 1, 0, 0), timezone='UTC')
同样,也支持按照年、月份
>>> d=Delorean(datetime=datetime.datetime(2012, 5, 15, 3, 50, 00, 555555),timezone="US/Eastern")
>>> d
Delorean(datetime=datetime.datetime(2012, 5, 15, 3, 50, 0, 555555), timezone='US/Eastern')
>>> d.truncate('month')
Delorean(datetime=datetime.datetime(2012, 5, 1, 0, 0), timezone='US/Eastern')
>>> d.truncate('year')
Delorean(datetime=datetime.datetime(2012, 1, 1, 0, 0), timezone='US/Eastern')
字符串处理
另一个麻烦事是处理datetime格式的字符串。Delorean可以很方便的处理
>>> from delorean import parse
>>> parse("2011/01/01 00:00:00 -0700")
Delorean(datetime=datetime.datetime(2011, 1, 1, 0, 0), timezone=pytz.FixedOffset(-420))
>>> parse("2018-05-06")
Delorean(datetime=datetime.datetime(2018, 6, 5, 0, 0), timezone='UTC')
歧义字段的处理
Delorean提供了两个字段dayfirst=True and yearfirst=True用来处理相应格式的字符串,如果dayfirst和yearfirst是True
默认情况下,对于May 6th, 2013格式,Delorean返回‘2013-05-06
>>> parse("2018-05-06")
Delorean(datetime=datetime.datetime(2018, 6, 5, 0, 0), timezone='UTC')
可以使用 dayfirst=Fasle表明 日在月之后
>>> parse("2018-05-06",dayfirst=False)
Delorean(datetime=datetime.datetime(2018, 5, 6, 0, 0), timezone='UTC')
补充问题
在使用Delorean 切换时区并存入数据库时(需要使用datetime格式写入),遇到了去掉时区时间就返回的问题。
>>> raw_date = '2018-09-06 01:00:00.834000'
>>> parse(raw_date,dayfirst=False)
Delorean(datetime=datetime.datetime(2018, 9, 6, 1, 0, 0, 834000), timezone='UTC')
>>> parse(raw_date,dayfirst=False).shift("Asia/Shanghai")
Delorean(datetime=datetime.datetime(2018, 9, 6, 9, 0, 0, 834000), timezone='Asia/Shanghai')
>>> parse(raw_date,dayfirst=False).shift("Asia/Shanghai").datetime
datetime.datetime(2018, 9, 6, 9, 0, 0, 834000, tzinfo=<DstTzInfo 'Asia/Shanghai' CST+8:00:00 STD>)
>>> str(parse(raw_date,dayfirst=False).shift("Asia/Shanghai").naive)
'2018-09-06 01:00:00.834000'
>>> str(parse(raw_date,dayfirst=False).shift("Asia/Shanghai").datetime)
'2018-09-06 09:00:00.834000+08:00'
>>> str(parse(raw_date,dayfirst=False).shift("Asia/Shanghai").datetime)[:-6]
'2018-09-06 09:00:00.834000'
参考:https://delorean.readthedocs.io/en/latest/
https://blog.csdn.net/bing900713/article/details/80270065
https://blog.csdn.net/frone/article/details/82463824
https://blog.csdn.net/nisxiya/article/details/84887554