将可读时间差(不是时间戳)转换为可用于排序的内容 [英] Convert human readable time difference (not timestamp) to something usable for sorting

查看:181
本文介绍了将可读时间差(不是时间戳)转换为可用于排序的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一系列时间差异,例如:

I have a series of time differences, such as:

7 months
11 months
1 hour, 24 minutes
10 months, 3 weeks
1 year
1 year, 1 month
8 months, 2 weeks
2 months
2 months, 4 weeks
8 months, 1 week
9 months, 3 weeks

我想将它们转换为绝对值,例如在几秒钟内进行排序。是的,我可以写我自己的图书馆,但是我想知道是否有东西已经存在。

and I want to convert them to an absolute value, such as in all in seconds for sorting purposes. Yes, I could write my own library, but I wanted to know if something already exists.

Google没有帮助,因为它有太多的时间戳结果。 / p>

Google has not been helpful, because it has way too many timestamp results.

推荐答案

Python在datetime模块中有timedelta类 - 它不能解析上面的数量,但是可以一些最小的解析,创建直接可比较的timedelta对象(可以直接添加和减去正常日期或datetime对象);

Python has the "timedelta" class in the datetime module - it can't parse the quantities above, but you can, with some minimal parsing, create timedelta objects which are directly comparable (and can be added and subtracted directly to normal date or datetime objects);

In [1]: from datetime import timedelta
In [5]: x = timedelta(weeks=40)
In [6]: x
Out[6]: datetime.timedelta(280)

timedelta可能需要几周,几年,几天和几秒的关键字参数,但不是几个月长度没有明确定义。此外,创建timedelta最常用的方式是减去两个date(或datetime)对象。

timedelta can take weeks, years, days and seconds as keyweord parameters, but not months since their lenght is not well defined. Also, the most usual way of creating a timedelta is by subtracting two date (or datetime) objects.

这个小功能利用你所使用的时间单位几乎相同,被接受为timedelta构造函数,以节省一些行来解析你的英语时差,使用正则表达式创建一个timedelta对象,使用正则表达式:

This small function takes advantage of the time units you are using being almost the same that are accepted as a timedelta constructor to save some lines in parsing your time differences in English and creating a timedelta object from them, using regular expressions:

import re
from datetime import timedelta
def get_timedelta(line):
    timespaces = {"days": 0}
    for timeunit in "year month week day hour minute second".split():
        content = re.findall(r"([0-9]*?)\s*?" + timeunit, line)
        if content:
            timespaces[timeunit + "s"] = int(content[0])
    timespaces["days"] += 30 * timespaces.pop("months", 0) + 365 * timespaces.pop("years", 0)
    return timedelta(**timespaces)

使用您提供的示例,有一个:

And using the examples you provide, one has:

In [26]: lines = """7 months
11 months                     
1 hour, 24 minutes
10 months, 3 weeks
1 year
1 year, 1 month
8 months, 2 weeks
2 months
2 months, 4 weeks
8 months, 1 week
9 months, 3 weeks""".split("\n")

In [27]: for line in lines:
    print(get_timedelta(line))
   ....:     
210 days, 0:00:00
330 days, 0:00:00
1:24:00
321 days, 0:00:00
365 days, 0:00:00
395 days, 0:00:00
254 days, 0:00:00
60 days, 0:00:00
88 days, 0:00:00
247 days, 0:00:00
291 days, 0:00:00

这篇关于将可读时间差(不是时间戳)转换为可用于排序的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆