iTesting软件测试知识分享

Python自动化测试--关于datetime的一点思考2

还记得我们前面讲过的Python自动化测试–关于datetime的一点思考吗?
我们使用pytz来处理时间,最后还自己实现了一个时间类来更好的处理现实问题,今天我们介绍一个更加方便的方式:

Arrow: better dates and times for Python。

1.安装

1
2
#url http://arrow.readthedocs.io/en/latest/
pip install arrow

2.简单使用

1
2
3
4
5
6
7
8
9
10
11
12
import arrow
utc = arrow.utcnow()
print(utc)
#输出:
>2017-10-30T05:59:06.678566+00:00
utc = arrow.now()
print(utc)
#输出:
>2017-10-30T13:59:06.678566+08:00

可以看到,qrrow带有时区信息。 还记得我们上次讲过的datetime类型吗? 由此可见,arrow的时间是time clock aware的。 跟datetime的 naive类型不一样。
那么,如果我们想得到navie类型的时间,该怎么做呢?

1
2
3
4
5
6
7
import arrow
utc = arrow.utcnow().naive
print(utc)
#输出:
>2017-10-30 14:29:22.418641

3.其它常用方法
arrow.get(x)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import arrow
from datetime import datetime
> ** Create from timestamps (ints or floats, or strings that convert to a float): **
print(arrow.get(1499900664))
#输出:
>2017-07-12T23:04:24+00:00
> ** Use a naive or timezone-aware datetime, or flexibly specify a timezone: **
print(arrow.get(datetime.utcnow()))
#输出:
>2017-10-30T06:39:42.114556+00:00
print(arrow.get(datetime(2017, 10, 30), tz.gettz('US/Pacific')))
#输出:
>2017-10-30T00:00:00-07:00
print(arrow.get(datetime.now(tz.gettz('Asia/Shanghai'))))
#输出:
>2017-10-30T14:45:44.807080+08:00
> ** Parse from a string: **
print(arrow.get('2017-10-30 12:30:45', 'YYYY-MM-DD HH:mm:ss'))
#输出:
>2017-10-30T12:30:45+00:00
> ** Search a date in a string: **
print(arrow.get('iTesting is awesome October 2017', 'MMMM YYYY'))
#输出:
>2017-10-01T00:00:00+00:00
arrow还可以自动识别字符串中的时间信息,并转为时间,不过这个功能不强大,例如字符串中有特殊符号或多个空格就会失败

Properties

1
2
3
4
5
6
7
** Get a naive datetime, and tzinfo **
import arrow
a = arrow.now('US/Pacific')
print(a.tzinfo)
#输出:
>tzfile('US/Pacific')

Get any datetime value:

1
2
3
4
5
6
7
8
9
import arrow
a = arrow.now('US/Pacific')
print(a.year)
print(a.month)
print(a.day)
#输出:
>2017
10
30

Call datetime functions that return properties:

1
2
3
4
5
print(a.date())
print(a.time())
#输出:
>2017-10-30
00:34:14.689940

Replace&Shift 更改时间

1
2
3
4
5
6
7
import arrow
a = arrow.now()
print(a)
print(a.replace(hour=1, minutes=30))
#输出:
>2017-10-30T15:38:28.064605+08:00
2017-10-30T02:08:28.064605+08:00

Format

1
2
3
4
import arrow
print(arrow.now().format('YYYY-MM-DD HH:mm:ss ZZ'))
#输出:
>2017-10-30 15:40:55 +08:00

Convert 时间转换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import arrow
print(arrow.utcnow().to('Asia/Shanghai'))
#输出:
>2017-10-30T15:42:30.510084+08:00
#arrow还帮我们实现了daylight saving的自动转换:
t1 = arrow.get(datetime(2017, 11, 5, 1, 59), 'US/Eastern')
print(t1)
t1 = arrow.get(datetime(2017, 11, 5, 2, 00), 'US/Eastern')
print(t1)
#输出:
>2017-11-05T01:59:00-04:00
2017-11-05T02:00:00-05:00
可以看到,时区信息自动切换了, 这个就比pytz里的方法方便,不需要再把时间给normize了。

以上就是arrow的大致用法,是不是比pytz要简单些? 对应到我们前面的问题,如何判断t1==t2呢?

1
2
3
4
#t1, <class 'datetime.datetime'>, 时区Eastern time
2017-08-22 08:49:38
#t2, <class 'datetime.datetime'>, 无时区信息
2017-08-22 20:49:38

答案不言而喻:

1
2
3
4
5
6
7
8
9
import arrow
naive_t1 = arrow.get('2017-11-05 02:00:00').naive
t1 = arrow.get(naive_t1, 'US/Eastern')
print(t1.to('local'))
navie_t2 = arrow.get('2017-11-05 15:00:00').naive
t2 = arrow.get(navie_t2, 'Asia/Shanghai')
assert t1== t2

总结一下, arrow是一个非常优秀的关于时间处理的第三方库,有了它,你可以更高效的处理时间参数了!

🐶 您的支持将鼓励我继续创作 🐶
-------------评论, 吐槽, 学习交流,请关注微信公众号 iTesting-------------
请关注微信公众号 iTesting wechat
扫码关注,跟作者互动