将时间量转换为天,小时和分钟

问题:将时间量转换为天,小时和分钟

我有一个timedelta。我想要从中得到的天,小时和分钟-作为元组或字典…我不大惊小怪。

多年来,我肯定已经用十几种语言完成了十二次,但是Python通常对所有问题都有一个简单的答案,所以我想我会在这里提出一些令人讨厌的简单(但冗长)的数学之前先问一下。

福兹先生提出了一个很好的观点。

我正在处理“列表”(有点像ebay列表),每个列表都有持续时间。我试图通过做找到剩余的时间when_added + duration - now

我是不是说DST不正确?如果不是,加/减一小时的最简单方法是什么?

I’ve got a timedelta. I want the days, hours and minutes from that – either as a tuple or a dictionary… I’m not fussed.

I must have done this a dozen times in a dozen languages over the years but Python usually has a simple answer to everything so I thought I’d ask here before busting out some nauseatingly simple (yet verbose) mathematics.

Mr Fooz raises a good point.

I’m dealing with “listings” (a bit like ebay listings) where each one has a duration. I’m trying to find the time left by doing when_added + duration - now

Am I right in saying that wouldn’t account for DST? If not, what’s the simplest way to add/subtract an hour?


回答 0

如果您有一个datetime.timedeltatd,那么td.days已经为您提供了所需的“天数”。timedelta值将天的分数保持为秒(而不是小时或分钟),因此您确实必须执行“令人作呕的简单数学”,例如:

def days_hours_minutes(td):
    return td.days, td.seconds//3600, (td.seconds//60)%60

If you have a datetime.timedelta value td, td.days already gives you the “days” you want. timedelta values keep fraction-of-day as seconds (not directly hours or minutes) so you’ll indeed have to perform “nauseatingly simple mathematics”, e.g.:

def days_hours_minutes(td):
    return td.days, td.seconds//3600, (td.seconds//60)%60

回答 1

这更加紧凑,您可以在两行中获得小时,分钟和秒。

days = td.days
hours, remainder = divmod(td.seconds, 3600)
minutes, seconds = divmod(remainder, 60)
# If you want to take into account fractions of a second
seconds += td.microseconds / 1e6

This is a bit more compact, you get the hours, minutes and seconds in two lines.

days = td.days
hours, remainder = divmod(td.seconds, 3600)
minutes, seconds = divmod(remainder, 60)
# If you want to take into account fractions of a second
seconds += td.microseconds / 1e6

回答 2

days, hours, minutes = td.days, td.seconds // 3600, td.seconds // 60 % 60

对于DST,我认为最好的方法是将两个datetime对象都转换为秒。这样,系统将为您计算DST。

>>> m13 = datetime(2010, 3, 13, 8, 0, 0)  # 2010 March 13 8:00 AM
>>> m14 = datetime(2010, 3, 14, 8, 0, 0)  # DST starts on this day, in my time zone
>>> mktime(m14.timetuple()) - mktime(m13.timetuple())     # difference in seconds
82800.0
>>> _/3600                                                # convert to hours
23.0
days, hours, minutes = td.days, td.seconds // 3600, td.seconds // 60 % 60

As for DST, I think the best thing is to convert both datetime objects to seconds. This way the system calculates DST for you.

>>> m13 = datetime(2010, 3, 13, 8, 0, 0)  # 2010 March 13 8:00 AM
>>> m14 = datetime(2010, 3, 14, 8, 0, 0)  # DST starts on this day, in my time zone
>>> mktime(m14.timetuple()) - mktime(m13.timetuple())     # difference in seconds
82800.0
>>> _/3600                                                # convert to hours
23.0

回答 3

我不明白

days, hours, minutes = td.days, td.seconds // 3600, td.seconds // 60 % 60

这个怎么样

days, hours, minutes = td.days, td.seconds // 3600, td.seconds % 3600 / 60.0

您会得到几分钟和一分钟的秒数浮动。

I don’t understand

days, hours, minutes = td.days, td.seconds // 3600, td.seconds // 60 % 60

how about this

days, hours, minutes = td.days, td.seconds // 3600, td.seconds % 3600 / 60.0

You get minutes and seconds of a minute as a float.


回答 4

我使用了以下内容:

delta = timedelta()
totalMinute, second = divmod(delta.seconds, 60)
hour, minute = divmod(totalMinute, 60)
print(f"{hour}h{minute:02}m{second:02}s")

I used the following:

delta = timedelta()
totalMinute, second = divmod(delta.seconds, 60)
hour, minute = divmod(totalMinute, 60)
print(f"{hour}h{minute:02}m{second:02}s")

回答 5

timedeltas具有daysand seconds属性..您可以轻松地自己转换它们。

timedeltas have a days and seconds attribute .. you can convert them yourself with ease.