python模块:时间处理模块

时间:2023-03-08 22:57:36
python模块:时间处理模块

http://blog.****.net/pipisorry/article/details/53067168

常用python自带时间处理模块

python自带的时间处理模块参考[操作系统服务:time时间模块+datetime模块]

有一些情况下,datetime却并没有那么好用。比如:

1.创建日期时间范围

2.创建未固定的日期时间

3.检验两个日期时间的差值是否在一定的范围内。

皮皮blog

dateutil模块

解析日期格式建议使用dateutil,不推荐使用datetime.datetime.strftime()。

The dateutil module provides powerful extensions tothe standard datetime module, available in Python.

>>> from dateutil.relativedelta import *
>>> from dateutil.easter import *
>>> from dateutil.rrule import *
>>> from dateutil.parser import *
>>> from datetime import *
>>> now = parse("Sat Oct 11 17:13:46 UTC 2003")
>>> today = now.date()
>>> year = rrule(YEARLY,dtstart=now,bymonth=8,bymonthday=13,byweekday=FR)[0].year
>>> rdelta = relativedelta(easter(year), today)
>>> print("Today is: %s" % today)
Today is: 2003-10-11
>>> print("Year with next Aug 13th on a Friday is: %s" % year)
Year with next Aug 13th on a Friday is: 2004
>>> print("How far is the Easter of that year: %s" % rdelta)
How far is the Easter of that year: relativedelta(months=+6)
>>> print("And the Easter of that year is: %s" % (today+rdelta))
And the Easter of that year is: 2004-04-11

计算时间差的示例

其中中间lz加了一个timestamp的转换(为了输入到只接收整形数的函数中),直接不转换也可以。

from dateutil.parser import parse
import datetime

a = 'Sat Jul 31 20:15:24 +0000 2011'
b = 'Sat Jul 30 20:07:44 +0000 2011'
# print(parse(a))
aa = datetime.datetime.timestamp(parse(a))
bb = datetime.datetime.timestamp(parse(b))
print(datetime.datetime.fromtimestamp(aa) - datetime.datetime.fromtimestamp(bb))
)

[dateutil - powerful extensions to datetime]

Delorean:解决 Python 中有关日期处理的棘手问题的库

Delorean是一个非常酷的日期/时间库。这个项目由Mahdi Yusuf维护,类似JavaScript的moment,拥有非常完善的技术文档。

用它处理日期和时间非常方便。设置时区,截取到秒、分、小时,甚至使用特定步骤从一个日期进到另一个日期。文档里面有很多例子。

用Python处理时间有点痛苦。如果你用Python的标准时间库datetime,并且处理过时区,你会感到绝望。delorean提供了一个相比于datetime和pytz的更好的抽象,让你处理时间更容易。它有很多有用的处理时区的特性,标准化时区或者从一个时区改变到另外一个时区。

from delorean import Delorean  
    EST = "US/Eastern"  
    d = Delorean(timezone=EST)

[Delorean英文文档]

皮皮blog

其它时间相关的库

pytz python时区处理模块(同时也包括夏令时)。世界时区,使用tz database时区信息数据库。

arrow,更好的日期和时间处理Python库。

chronyk,一个Python 3版函数库,用于解析人写的时间和日期。

moment,类似Moment.js的日期/时间Python库。

radar,雷达,生成随机日期/时间。

sandglass(沙漏)** 是一个增强的、友好的时间处理库,目的是为了解放程序员的生产力。在python中有太多处理时间的库,datetime/date/time/calendar等等。需要记的细节太多,选择困难。而sandglass就是解决这个的青霉素。从各种麻烦的转换中解脱出来。只需记住 **Sandglass对象** 和 **ben()** 、 **tslice()** 、 **cronwalk()** 这几个主要的api即可。[sandglass 0.0.1]

Python日期时间处理: datestuff

[Python日期时间处理: datestuff]

[datestuff github]

when.py

友好的时间日期库

>>> import when
>>> when.timezone()
'Asia/Shanghai'
>>> when.today()
datetime.date(2013, 5, 14)
>>> when.tomorrow()
datetime.date(2013, 5, 15)
>>> when.now()
datetime.datetime(2013, 5, 14, 21, 2, 23, 78838)

[https://github.com/dirn/When.py]

皮皮blog

from: http://blog.****.net/pipisorry/article/details/53067168

ref: [Python时间库性能比较]