格式化python timedelta对象 [英] formatting python timedelta objects
问题描述
我有两个datetime对象。我需要计算它们之间的timedelta,然后以spcific格式显示输出。
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。
我想格式化输出,但是无法这样做。
print Turnaround_TimeObj.strftime('%H hrs %M mins%S secs')
无效。
我知道这样做的一种方法是将其转换为秒,然后将divmodding转换为获取所需的格式。
as in;
totalSeconds = Turnaround_TimeObj.seconds
pre>
hours,remaining = divmod(totalSeconds,3600)
分钟,秒= divmod(余数,60)
打印'%s:%s:%s'%(小时,分钟,秒)
但我想知道是否可以使用任何日期时间函数(如strftime)在单行中执行。
编辑:实际转换为秒也不起作用。
如果我将时间增量1天,3:42:54转换为秒使用totalSeconds = Turnaround_TimeObj .seconds
totalSeconds值显示为13374而不是99774. ie。它忽略了天值。
解决方案
但我想知道我是否可以使用任何日期时间函数的单行,如strftime。
据我所知,没有内置的方法到
timedelta
这样做。如果你经常做,你可以创建自己的功能,例如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)
用法: / p>
>>>打印strfdelta(delta_obj,{days} days {hours}:{minutes}:{seconds})
1天20:18:12
>>>打印strfdelta(delta_obj,{小时}小时和{分钟}去)
20小时和18去
如果要使用更接近
strftime
使用的字符串格式,我们可以使用string.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天20:18:12
>>>打印strfdelta(delta_obj,%H小时和%M去)
20小时和18去
totalSeconds值显示为13374而不是99774. ie。它忽略了日值。
注意,在上面的例子中可以使用
timedelta.days
获得day值。
或者,从Python 2.7起,timedelta有一个 total_seconds()方法,返回持续时间中包含的总秒数。
I am rather new to python. I have two datetime objects. I need to calculate the timedelta between them and then show the output in a spcific 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
an example of this Turnaround_TimeObj time delta is "2 days, 22:13:45". I want to format the output but am unable to do so.
print Turnaround_TimeObj.strftime('%H hrs %M mins %S secs')
doesn't works.
I know one way of doing this will be to convert it to seconds and then divmodding to get the required formatting. as in;
totalSeconds = Turnaround_TimeObj.seconds hours, remainder = divmod(totalSeconds, 3600) minutes, seconds = divmod(remainder, 60) print '%s:%s:%s' % (hours, minutes, seconds)
but i was wondering if I can do it in a single line using any date time function like strftime.
EDIT: actually converting to seconds doesn't works either. if I convert the time delta "1 day, 3:42:54" to seconds using
totalSeconds = Turnaround_TimeObj.seconds
totalSeconds value is shown as 13374 instead of 99774. ie. its ignoring the "day" value.
解决方案but i was wondering if I can do it in a single line using any date time function like strftime.
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)
Usage:
>>> 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
If you want to use a string format closer to the one used by
strftime
we can employstring.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)
Usage:
>>> 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 value is shown as 13374 instead of 99774. ie. its ignoring the "day" value.
Note in the example above that you can use
timedelta.days
to get the "day" value.Alternatively, from Python 2.7 onwards, timedelta has a total_seconds() method which return the total number of seconds contained in the duration.
这篇关于格式化python timedelta对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!