python的datetime.strptime_Python中的时间函数datetime.strptime()参数顺序的问题

即便是python资深用户也经常会错写datetime.strptime(string, format) ,颠倒两个参数的顺序而写成了datetime.strptime(format, string) ,与re.find(pattern, string)相混淆。

分析问题

1、datetime.strptime()

首先strptime的函数命名不太好, 词义模糊, 不如直接叫str2time。string是source,time是result。

strptime(string, format) method of builtins.type instance

string, format -> new datetime parsed from a string (like time.strptime())

#Case

In [6]: datetime.strptime("8 May, 2019", "%d %B, %Y")

Out[6]: datetime.datetime(2019, 5, 8, 0, 0)

本函数的工作是parse a string to produce a time object。strptime 的字面含义读起来却像是毫无意义的string to parse time或者是读成相反的含义parse time to produce a string。妥协的处理方法理解为‘string parsed’ to time。将strp当做一个组合名词即strp2time。

官方的 strptime 命名比较 str2time 的优点是存在动词 parse, 点出函数的内部逻辑需要做的核心工作是解析。将普通的字符串解析成为有实际价值和意义的时间对象。(更像是开发者提醒自己非写给API的用户)

我们可以将参数名做简单的扩展:

datetime.strptime(string, format)

#扩展为

def strptime(src_string, parsed_format) -> "dst_time_object":

这样看来strptime还是符合 source to destination 的模式。

容易混淆 string 与 format 顺序的一个直接原因是存在 re.match(pattern, str),按照module re的这个思路, 要事第一!对strptime(string, format)的处理似乎也应该将format拿到前面。

对此的理解:

1)source to destination 是惯例, strptime(string, format)没有打破惯。

2)regex 突破了惯例, 可能因为 consturct regex 需要花费较多脑力,而其他如time-format并不需要多少思考。(regex的所有的methods都是将构建regex-pattern作为第一个参数)

3)处理 strptime 从字面含义去引导直觉. strptime(string, parsed-format) to a time-object

2、datetime.strftime()

容易混淆的另外一个原因是strptime的逆函数strftime。

strftime(format)

format -> strftime() style string.

In [8]: dt

Out[8]: datetime.datetime(2019, 5, 8, 0, 0)

In [9]: dt.strftime("%d %B, %Y")

Out[9]: '08 May, 2019'

strftime是time2str。

上例中datetime_obj.strftime(format)是其对象式编程的写法,由于里面只有一个format参数,因而也往往容易诱导python用户在写strptime的时候也将format写在前面。

strftime的函数式写法为

In [9]: datetime.strftime(datetime.now(),"%b %d %Y %H:%M:%S")

Out[9]: 'Jun 11 2020 08:08:52'

这样一来与strptime函数就完全保持一致了,二者的参数都遵循 src to dst 的原则。

总结

1、二者的共性

使用strptime与strftime,直觉思考的时候首先从动词出发,分别从parse和format出发,并应用其函数式的完整参数模式,思考链条为:

parse -> parse src_string to dst_time

format -> format src_time to dst_string。

概括为以下三点:

辨析parse or format

都遵循 src to dst

str[pf]time顺序不变

2、特例与常规

除了re.search(pattern, src)之外,还有一个不守规矩的特例。

先看字符串的str.split函数

In [12]: "source to destination".split()

Out[12]: ['source', 'to', 'destination'] #遵循source to destination, convert src_str to dst_list

但是其逆操作却将要输出的结果放在了前面。

In [13]: " ".join(['source', 'to', 'destination'])

Out[13]: 'source to destination'

显然['source', 'to', 'destination'].join(" ")更为合理 convert src_list to dst_string

一种可能的解释,Python将所有涉及string的操作集中在了一处。

In [16]: len([m for m in dir(str) if not m.startswith("__")])

Out[16]: 45

In [17]: len([m for m in dir(list) if not m.startswith("__")])

Out[17]: 11

可以看到字符串str的方法整整是list的方法的四倍还多。

由此,我们辨析特例与常规:

regex.find 和 str.join 是特例,strptime, strftime, str.split是常规。

3、系统的date与clock服务

费这么多笔墨辨析strptime这一个函数,乃是因为维护系统的时间是操作系统最核心的基础服务之一。

$ date -u

$ sudo hwclock -u

2019-05-08 20:19:14.764965+08:00

#str2time, strptime

$ date -d "08 May 2019" +"%c"

Wed 08 May 2019 14:00:00 AM CST

#time2str, strftime

$ date +"%d %B, %Y"

08 May, 2019

#思路与strptime, strftime一致

盼本文对你有点滴启发有帮助。

文源网络,仅供学习之用,如有侵权请联系删除。

在学习Python的道路上肯定会遇见困难,别慌,我这里有一套学习资料,包含40+本电子书,800+个教学视频,涉及Python基础、爬虫、框架、数据分析、机器学习等,不怕你学不会!

https://shimo.im/docs/JWCghr8prjCVCxxK/ 《Python学习资料》

关注公众号【Python圈子】,优质文章每日送达。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值