人类可读的datetime间隔到Python中的datetime.timedelta吗? [英] Human-readable datetime interval to datetime.timedelta in Python?
问题描述
我发现自己需要在python配置文件中指定很多时间。
I find myself needing to specify a timespan in a python configuration file a lot.
是否可以通过stdlib在python配置文件中指定更易读的时间范围(类似于PostgreSQL的Interval语法)?还是需要第三方库?
Is there a way that I can specify a more human readable timeframe (similar to PostgreSQL's Interval syntax) in a python configuration file with stdlib? Or will this require a 3rd party lib?
澄清我在 ConfigParser.ConfigParser中找不到任何内容
stdlib API。我想我真正需要的是一种从人类可读的日期/时间间隔到 datetime.timedelta
值的方法。
Clarification I'm not looking for anything in the ConfigParser.ConfigParser
stdlib API specifically. I guess what I really need is a way to go from human readable date/time interval to datetime.timedelta
value.
推荐答案
我认为没有标准的库模块。我写了一个做到这一点。您可以安装它,也可以根据需要对其进行调整。
I don't think there is a standard library module for that. I wrote one that does that. You can install it, or adapt it to your needs.
该模块称为 pycopia.timespec
它会转换字符串,例如 1day 3min 到秒,以浮点数表示。从中获取 datetime.timedelta
很容易。
It converts strings such as "1day 3min" to seconds, as a float. It's easy to get a datetime.timedelta
from that.
这篇关于人类可读的datetime间隔到Python中的datetime.timedelta吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!