格式化timedelta对象 [英] Formatting timedelta objects

查看:84
本文介绍了格式化timedelta对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个 datetime 对象。我需要计算它们之间的 timedelta ,然后以特定格式显示输出。

I have two datetime objects. I need to calculate the timedelta between them and then show the output in a specific format.

Alpha_TimeObj = datetime.datetime(int(AlphaTime.strftime('%Y')), int(AlphaTime.strftime('%m')), int(AlphaTime.strftime('%d')), int(AlphaTime.strftime('%H')), int(AlphaTime.strftime('%M')), int(AlphaTime.strftime('%S')))
Beta_TimeObj = datetime.datetime(int(BetaTime.strftime('%Y')), int(BetaTime.strftime('%m')), int(BetaTime.strftime('%d')), int(BetaTime.strftime('%H')), int(BetaTime.strftime('%M')), int(BetaTime.strftime('%S')))
Turnaround_TimeObj = Beta_TimeObj  - Alpha_TimeObj

Turnaround_TimeObj 时间增量的示例为 2天,22:13:45。我想格式化输出,但无法格式化。

An example of this Turnaround_TimeObj time delta is "2 days, 22:13:45". I want to format the output, but I am unable to do so.

print Turnaround_TimeObj.strftime('%H hrs %M mins %S secs')

不起作用。

我知道这样做的一种方法是将其转换为秒,然后进行除法以获得所需的格式。

I know one way of doing this will be to convert it to seconds and then divmoding to get the required formatting.

如:

totalSeconds = Turnaround_TimeObj.seconds
hours, remainder = divmod(totalSeconds, 3600)
minutes, seconds = divmod(remainder, 60)
print '%s:%s:%s' % (hours, minutes, seconds)

但是我想知道是否可以使用任何日期时间函数(例如 strftime )在一行中完成它。

But I was wondering if I can do it in a single line using any date time function like strftime.

实际上转换为秒也不起作用。如果我使用以下时间将时间增量 1天,3:42:54转换为秒:

Actually converting to seconds doesn't work either. If I convert the time delta "1 day, 3:42:54" to seconds using:

totalSeconds = Turnaround_TimeObj.seconds

totalSeconds 的值显示为13374,而不是99774。也就是说,它忽略了 day值。

The totalSeconds value is shown as 13374 instead of 99774. i.e. it's ignoring the "day" value.

推荐答案


但是我想知道是否可以使用任何日期时间函数(如 strftime )单行执行。

据我所知, timedelta 没有内置方法可以做到这一点。如果您经常这样做,则可以创建自己的函数,例如

As far as I can tell, there isn't a built-in method to timedelta that does that. If you're doing it often, you can create your own function, e.g.

def strfdelta(tdelta, fmt):
    d = {"days": tdelta.days}
    d["hours"], rem = divmod(tdelta.seconds, 3600)
    d["minutes"], d["seconds"] = divmod(rem, 60)
    return fmt.format(**d)

用法:

>>> print strfdelta(delta_obj, "{days} days {hours}:{minutes}:{seconds}")
1 days 20:18:12
>>> print strfdelta(delta_obj, "{hours} hours and {minutes} to go")
20 hours and 18 to go

如果您想使用更接近 strftime 使用的字符串格式,我们可以使用 string.Template

If you want to use a string format closer to the one used by strftime we can employ string.Template:

from string import Template

class DeltaTemplate(Template):
    delimiter = "%"

def strfdelta(tdelta, fmt):
    d = {"D": tdelta.days}
    d["H"], rem = divmod(tdelta.seconds, 3600)
    d["M"], d["S"] = divmod(rem, 60)
    t = DeltaTemplate(fmt)
    return t.substitute(**d)

用法:

>>> print strfdelta(delta_obj, "%D days %H:%M:%S")
1 days 20:18:12
>>> print strfdelta(delta_obj, "%H hours and %M to go")
20 hours and 18 to go








totalSeconds 的值显示为13374,而不是99774。即

The totalSeconds value is shown as 13374 instead of 99774. I.e. it's ignoring the "day" value.

在上面的示例中请注意,您可以使用 timedelta.days 来获取 day值。

Note in the example above that you can use timedelta.days to get the "day" value.

或者,从Python 2.7开始,timedelta具有 total_seconds()方法,该方法返回持续时间中包含的总秒数。

Alternatively, from Python 2.7 onwards, timedelta has a total_seconds() method which return the total number of seconds contained in the duration.

这篇关于格式化timedelta对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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