这个问题类似to this question about subtracting dates with Python,但不完全相同。我不是在处理字符串,我必须弄清楚两个纪元时间戳之间的区别,并以人类可读的格式产生差异。
例如:
32 Seconds
17 Minutes
22.3 Hours
1.25 Days
3.5 Weeks
2 Months
4.25 Years
或者,我想表达这样的差异:
4 years, 6 months, 3 weeks, 4 days, 6 hours 21 minutes and 15 seconds
我认为我不能使用strptime
,因为我正在使用两个纪元日期的差异。我可以写一些东西来做这件事,但我很确定已经写了一些我可以使用的东西。
什么模块适合?我只是遗漏了time
中的内容吗?我的Python之旅刚刚开始,如果这确实是重复的,那是因为我没有找到要搜索的内容。
为了准确,我真的非常关心当年的日历。
答案 0 :(得分:47)
您可以使用精彩的dateutil模块及其relativedelta类:
import datetime
import dateutil.relativedelta
dt1 = datetime.datetime.fromtimestamp(123456789) # 1973-11-29 22:33:09
dt2 = datetime.datetime.fromtimestamp(234567890) # 1977-06-07 23:44:50
rd = dateutil.relativedelta.relativedelta (dt2, dt1)
print "%d years, %d months, %d days, %d hours, %d minutes and %d seconds" % (rd.years, rd.months, rd.days, rd.hours, rd.minutes, rd.seconds)
# 3 years, 6 months, 9 days, 1 hours, 11 minutes and 41 seconds
它不计算数周,但这不应该太难添加。
答案 1 :(得分:38)
与@ Schnouki解决方案相比,单线列表理解略有改进。在多个实体(如小时)的情况下也显示复数
导入relativedelta
>>> from dateutil.relativedelta import relativedelta
lambda函数
>>> attrs = ['years', 'months', 'days', 'hours', 'minutes', 'seconds']
>>> human_readable = lambda delta: ['%d %s' % (getattr(delta, attr), getattr(delta, attr) > 1 and attr or attr[:-1])
... for attr in attrs if getattr(delta, attr)]
使用示例:
>>> human_readable(relativedelta(minutes=125))
['2 hours', '5 minutes']
>>> human_readable(relativedelta(hours=(24 * 365) + 1))
['365 days', '1 hour']
答案 2 :(得分:22)
我今天早些时候遇到了同样的问题,我在标准库中找不到任何可以使用的东西,所以这就是我写的:
#!/usr/bin/env python
INTERVALS = [1, 60, 3600, 86400, 604800, 2419200, 29030400]
NAMES = [('second', 'seconds'),
('minute', 'minutes'),
('hour', 'hours'),
('day', 'days'),
('week', 'weeks'),
('month', 'months'),
('year', 'years')]
def humanize_time(amount, units):
"""
Divide `amount` in time periods.
Useful for making time intervals more human readable.
>>> humanize_time(173, 'hours')
[(1, 'week'), (5, 'hours')]
>>> humanize_time(17313, 'seconds')
[(4, 'hours'), (48, 'minutes'), (33, 'seconds')]
>>> humanize_time(90, 'weeks')
[(1, 'year'), (10, 'months'), (2, 'weeks')]
>>> humanize_time(42, 'months')
[(3, 'years'), (6, 'months')]
>>> humanize_time(500, 'days')
[(1, 'year'), (5, 'months'), (3, 'weeks'), (3, 'days')]
"""
result = []
unit = map(lambda a: a[1], NAMES).index(units)
# Convert to seconds
amount = amount * INTERVALS[unit]
for i in range(len(NAMES)-1, -1, -1):
a = amount / INTERVALS[i]
if a > 0:
result.append( (a, NAMES[i][1 % a]) )
amount -= a * INTERVALS[i]
return result
if __name__ == "__main__":
import doctest
doctest.testmod()
您可以使用dateutil.relativedelta()
计算准确的时间增量并使用此脚本将其人性化。
答案 3 :(得分:12)
def humanize_time(amount, units = 'seconds'):
def process_time(amount, units):
INTERVALS = [ 1, 60,
60*60,
60*60*24,
60*60*24*7,
60*60*24*7*4,
60*60*24*7*4*12,
60*60*24*7*4*12*100,
60*60*24*7*4*12*100*10]
NAMES = [('second', 'seconds'),
('minute', 'minutes'),
('hour', 'hours'),
('day', 'days'),
('week', 'weeks'),
('month', 'months'),
('year', 'years'),
('century', 'centuries'),
('millennium', 'millennia')]
result = []
unit = map(lambda a: a[1], NAMES).index(units)
# Convert to seconds
amount = amount * INTERVALS[unit]
for i in range(len(NAMES)-1, -1, -1):
a = amount // INTERVALS[i]
if a > 0:
result.append( (a, NAMES[i][1 % a]) )
amount -= a * INTERVALS[i]
return result
rd = process_time(int(amount), units)
cont = 0
for u in rd:
if u[0] > 0:
cont += 1
buf = ''
i = 0
for u in rd:
if u[0] > 0:
buf += "%d %s" % (u[0], u[1])
cont -= 1
if i < (len(rd)-1):
if cont > 1:
buf += ", "
else:
buf += " and "
i += 1
return buf
使用示例:
>>> print humanize_time(234567890 - 123456789)
3 years, 9 months, 3 weeks, 5 days, 11 minutes and 41 seconds
>>> humanize_time(9, 'weeks')
2 months and 1 week
优势(您不需要第三方!)。
从“Liudmil Mitev”算法改进。 (谢谢!)
答案 4 :(得分:7)
老问题,但我个人最喜欢这种方法:
import datetime
import math
def human_time(*args, **kwargs):
secs = float(datetime.timedelta(*args, **kwargs).total_seconds())
units = [("day", 86400), ("hour", 3600), ("minute", 60), ("second", 1)]
parts = []
for unit, mul in units:
if secs / mul >= 1 or mul == 1:
if mul > 1:
n = int(math.floor(secs / mul))
secs -= n * mul
else:
n = secs if secs != int(secs) else int(secs)
parts.append("%s %s%s" % (n, unit, "" if n == 1 else "s"))
return ", ".join(parts)
human_time(seconds=3721)
# -> "1 hour, 2 minutes, 1 second"
如果您想将秒部分与“和”分开:
"%s and %s" % tuple(human_time(seconds=3721).rsplit(", ", 1))
# -> "1 hour, 2 minutes and 1 second"
答案 5 :(得分:5)
查看人性化软件包
https://github.com/jmoiron/humanize
import datetime
humanize.naturaltime(datetime.datetime.now() - datetime.timedelta(seconds=1))
'a second ago'
humanize.naturaltime(datetime.datetime.now() - datetime.timedelta(seconds=3600))
'an hour ago'
答案 6 :(得分:1)
一个很老的问题,但我发现这个解决方案在 Python3 中似乎很简单:
print(datetime.timedelta(seconds=3600))
# output: 1:00:00
print(datetime.timedelta(hours=360.1245))
# output: 15 days, 0:07:28.200000
答案 7 :(得分:0)
这里是较短的,间隔为几秒,一天之内(t <86400)。如果您使用的是unix时间戳(自大纪元以来的秒数,UTC),则很有用。
t = 45678
print('%d hours, %d minutes, %d seconds' % (t//3600, t%3600//60, t%60))
可能会进一步扩展(t // 86400,...)。