用于TZ感知日期时间的简单函数。
Saturn的Python项目详细描述
使用干净的语法对日期时间执行常用操作,充当瘦包装器 对于DateTime和Pytz。强制时区感知 约会时间。所有操作都是顶级功能:不处理 来自多个模块和对象的方法。
有几种现有的模块设计用于改进Python的数据时间。 这就是土星与众不同的原因:
- Uses native datetime types for compatibility and speed
- Only one import required
- Clean, intuitive syntax and function names. No boilerplate.
- Operates exclusively with top-level functions; no sorting through methods from multiple objects and modules
- Raises an exception if a func is given a tz-naive datetime as input. All datetime outputs are timezone-aware, with an easily-set TZ, defaulting to UTC.
这个项目没有解决python日期时间可能出现的更深层的问题。 来自时区和本地化冲突。它旨在通过提供 简洁、一致和有表现力的api;防止天真的日期时间进入您的代码。
土星使用Pytz作为时区。用作从属关系: Pytz website Pytz是根据麻省理工学院的许可证授权的。
…并使用箭头中的代码进行字符串格式化和解析。不是依赖关系。 Arrow website: Arrow在Apache2下获得许可。
不支持Python2。
包含的功能
- datetime: Return a timezone-aware datetime.datetime object. Created the same way as datetime.datetime, with an optional ‘tz’ argument for a timezone string. Defaults to UTC.
- time: Same concept as datetime.time; easily create a tz-aware time.
- now: Find current utc time; timezone-aware.
- range_dt: Iterate over datetimes, with a customizable interval. Similar to builtin range. Lazy.
- fix_naive: Convert a timezone-naive datetime to an aware one.
- move_tz: Change a datetime from one timezone to another.
- combine: Similar to datetime.datetime.combine, but always tz-aware.
- to_str: Similar to datetime.datetime.strftime, but with a cleaner format string, from Arrow.
- from_str: Similar to datetime.datetime.strptime, but with a cleaner format string, from Arrow. Returns date, datetime, or time objects as needed.
- to_iso: Wrapper for datetime.datetime’s isoformat() method, as a function.
- from_iso: Create a datetime from an isoformat string.
- to_epoch: Wrapper for datetime.datetime’s timestamp method, as a function.
- from_epoch: Wrapper for datetime.datetime’s from_timestamp method, as a function.
- split: Split a datetime into date and time components. Useful because datetime’s .time() method strips timezone info.
- add, subtract: Add or subtract to/from a datetime.
- overlaps: Deterine if two date/time/datetime ranges overlap.
- timedelta, date, and today are included as wrappers for their respective datetime/date classes, so you don’t need to import datetime.
安装
pip install saturn
文档
创建可识别时区的日期时间。如果不指定“tz”参数,则默认为 到UTC。也适用于时间:
saturn.datetime(2016,1,1,16,tz='US/Eastern')# datetime.datetime(2016, 1, 1, 16, 0, tzinfo=<DstTzInfo 'US/Eastern' EST-1 day, 19:00:00 STD>)saturn.datetime(2016,1,1,16)# datetime.datetime(2016, 1, 1, 16, 0, tzinfo=<UTC>)saturn.time(11,29,30)# datetime.time(11, 29, 30, tzinfo=<UTC>)
使TZ天真的日期时间意识到:
naive=datetime.datetime(2016,1,1)saturn.fix_naive(naive,"Pacific/Midway")# datetime.datetime(2016, 1, 1, 0, 0, tzinfo=<DstTzInfo 'Pacific/Midway' SST-1 day, 13:00:00 STD>)
以UTC为单位查找当前日期时间:
saturn.now()# datetime.datetime(2016, 4, 29, 20, 36, 53, 257753, tzinfo=<UTC>)
从一个时区移动到另一个时区:
dt=saturn.datetime(2016,1,1,tz='Asia/Gaza')# datetime.datetime(2016, 1, 1, 0, 0, tzinfo=<DstTzInfo 'Asia/Gaza' EET+2:00:00 STD>)saturn.move_tz(dt,'Europe/Vatican')# datetime.datetime(2015, 12, 31, 23, 0, tzinfo=<DstTzInfo 'Europe/Vatican' CET+1:00:00 STD>
将日期和时间合并为可识别时区的日期时间。如果时间已知,则忽略“tz”参数:
date,time=datetime.date(2016,3,2),datetime.time(16,30)saturn.combine(date,time)# datetime.datetime(2016, 3, 2, 16, 30, tzinfo=<UTC>)saturn.combine(date,time,tz='Europe/London')# datetime.datetime(2016, 3, 2, 16, 30, tzinfo=<DstTzInfo 'Europe/London' GMT0:00:00 STD>)
将DateTime拆分为日期和时间组件;保留TZINFO,与DateTime.time()不同。
dt=saturn.datetime(2016,3,2,16,30,1,500,tz='US/Mountain')date,time=saturn.split(dt)# datetime.date(2016, 3, 2)# datetime.time(16, 30, 1, 500, tzinfo=<DstTzInfo 'US/Mountain' MST-1 day, 17:00:00 STD>)
遍历一个日期时间范围。有效间隔为“周”、“月”、“日” “小时”、“分钟”、“秒”、“毫秒”和“微秒”:
start,end=saturn.datetime(2016,1,2,12,30),saturn.datetime(2016,1,5,12,30)fordtinsaturn.range_dt(start,end,interval='day'):print(dt)# 2016-01-02 12:30:00+00:00# 2016-01-03 12:30:00+00:00# 2016-01-04 12:30:00+00:00fordtinsaturn.range_dt(start,end,4,interval='hour'):print(dt)# 2016-01-02 12:30:00+00:00# 2016-01-02 16:30:00+00:00# 2016-01-02 20:30:00+00:00...# 2016-01-05 00:30:00+00:00# 2016-01-05 04:30:00+00:00# 2016-01-05 08:30:00+00:00
将日期时间转换为字符串。使用箭头中的格式:
saturn.to_str(saturn.now(),'YYYY-MM-DD hh:mm')# '2016-04-29 03:30'
将字符串转换为日期时间。使用箭头格式。如果字符串包含时区,则忽略可选的TZ参数:
saturn.from_str('2016-04-29 03:30','YYYY-MM-DD hh:mm')# datetime.datetime(2016, 4, 29, 3, 30, tzinfo=<UTC>)saturn.from_str('2016-04-29 03:30','YYYY-MM-DD hh:mm',tz='Africa/Cairo')# datetime.datetime(2016, 4, 29, 3, 30, tzinfo=<DstTzInfo 'Africa/Cairo' EET+2:00:00 STD>)saturn.from_str('1381685817','X')# datetime.datetime(2013, 10, 13, 17, 36, 57, tzinfo=<UTC>)
将日期时间转换为ISO-8601字符串或纪元:
saturn.to_iso(saturn.now())# '2016-04-29T20:12:05.807558+00:00'saturn.to_epoch(saturn.now())# 1461960725.807558
将ISO-8601字符串或epoch转换为日期时间:
saturn.from_iso('2016-04-29T20:12:05.000000+00:00')# datetime.datetime(2016, 4, 29, 20, 12, 05, tzinfo=<UTC>)saturn.from_epoch(1461960725)# datetime.datetime(2016, 4, 29, 21, 12, 5, tzinfo=<UTC>)
有关to-str和from-str语法的详细信息,请参考Arrow’s formatting guide。
检查时间范围是否重叠。
start1=saturn.datetime(2018,1,1,9)end1=saturn.datetime(2018,1,1,12)start2=saturn.datetime(2018,1,1,11)end2=saturn.datetime(2018,1,1,17)saturn.overlaps(start1,end1,start2,end2)# Truesaturn.overlaps(start1,end1,saturn.datetime(2018,1,1,13),end2)# False
功能输入和输出:
datetime(year:float,month:float,day:float,hour:float=0,minute:float=0,second:float=0,microsecond:float=0,tzinfo=None,tz:str='UTC')->datetime.datetimetime(hour:float,minute:float=0,second:float=0,microsecond:float=0,tzinfo=None,tz:str='UTC')->datetime.timenow()->datetime.datetimecombine(_date:datetime.date,_time:_datetime.time,tz:str='UTC')->datetime.datetimefix_naive(dt:TimeOrDatetime,tz:str='UTC')->datetime.datetimeto_str(dt:DateOrDatetime,str_format:str)->strfrom_str(dt_str:str,str_format:str,tz:str='UTC')->DateOrTimeOrDatetimeto_iso(dt:DateOrDatetime)->strfrom_iso(iso_str:str,tz:str='UTC')->datetime.datetimeto_epoch(dt:DateOrDatetime)->float:from_epoch(epoch:float,tz:str='UTC')->_datetime.datetime:move_tz(dt:datetime.datetime,tz:str)->datetime.datetimeadd(dt:datetime.datetime,days:float=0,seconds:float=0,microseconds:float=0,milliseconds:float=0,minutes:float=0,hours:float=0,weeks:float=0)->datetime.datetimesubtract(dt:datetime.datetime,days:float=0,seconds:float=0,microseconds:float=0,milliseconds:float=0,minutes:float=0,hours:float=0,weeks:float=0)->datetime.datetimerange_dt(start:DateOrDatetime,end:DateOrDatetime,step:int=1,interval:str='day')->Iterator[datetime.datetime]split(dt:datetime.datetime)->Tuple[_datetime.date,_datetime.time]:overlaps(start1:DateOrTimeOrDatetime,start2:DateOrTimeOrDatetime,end1:DateOrTimeOrDatetime,end2:DateOrTimeOrDatetime)->bool:
我们要避开的一些语法:
pytz.timezone('Europe/Berlin').localize(datetime.datetime(1985,2,1,13,21))arrow.Arrow(1999,9,9,9,30,tzinfo=dateutil.tz.gettz('US/Eastern'))pytz.timezone('US/Mountain').localize(datetime.datetime.combine(date,time))aware_dt.astimezone(pytz.timezone('US/Pacific'))aware_time=datetime.time(aware_dt.hour,aware_dt.minute,aware_dt.second,aware_dt.microsecond,aware_dt.tzinfo)
替换为:
saturn.datetime(1985,2,1,13,21,tz='Europe/Berlin')saturn.datetime(1999,9,9,9,30,tz='US/Eastern')saturn.combine(date,time,'US/Mountain')saturn.move_tz(aware_dt,'US/Pacific')_,aware_time=saturn.split(aware_dt)