我目前正在生成给定秒数的时间量。这是我很快想到的。它运行良好,但非常丑陋。
我想不出有什么技巧可以使它更优雅(不让它变得复杂或依赖这个那个),但也许这里有人有一些技巧。
def humanizeTime(seconds):
if seconds < 60:
return "%d seconds" % int(round(seconds))
else:
minutes = seconds / 60.0
if minutes < 60:
return "%d minutes %d seconds" % divmod(seconds, 60)
else:
hours = minutes / 60.0
if hours < 24:
return "%d hours %d minutes" % divmod(minutes, 60)
else:
days = hours / 24.0
if days < 7:
return "%d days" % int(round(days))
else:
weeks = days / 7.0
if weeks < 4:
return "%d weeks" % int(round(weeks))
else:
months = days / 30.0
if months < 12:
return "%d months" % int(round(months))
else:
return "%d years" % int(round(days / 365.0))
编辑
如果有一个好的库可以用正确的语法计算我上面的内容(同样,永远不会超过 2 个字段),我肯定会加入。
然后我又找不到任何这样做的东西,因为任何可以计算这个的库仍然需要我编写这样的代码(或下面显示的一些答案)以最多只显示 2 个字段。
最佳答案
免责声明
这个答案是一个更 pythonic 的表达 OP 显示的代码的例子。这只是一个使用高阶函数的有趣编码练习,绝不是建议的,甚至不是理智处理真实时间间隔的选项。
请记住,现实生活中的时间和日历计算既不简单也不琐碎,最好使用经过良好测试的成熟库来解决这个问题。
也就是说,这里有一种方法,显然更优雅:
# A function to generate the divisions list
def divisions(l, v):
l.append(l[-1] * v)
return l
# A function to reduce the divisions list
def reduction(l, v):
q, r = divmod(l[-1], v)
l[-1] = q
l.append(r)
return l
TIME_STEPS = (60, 60, 24, 7, 30, 12)
DIVISIONS = reduce(divisions, TIME_STEPS, [1])[1:]
DIVISIONS.reverse()
# The "seconds" variable holds the time interval in seconds
seconds = 6000
fragments = reduce(reduction, DIVISIONS, [seconds])
# Fragments is a list: [years, months, weeks, days, hours, minutes, seconds]
# In this example: [0, 0, 0, 0, 1, 40, 0]
# And here's the readability part
NAMES = ("years", "months", "weeks", "days", "hours", "minutes", "seconds")
readable = " ".join("%d %s" % (v, n) for v, n in zip(fragments, NAMES) if v > 0)
# Final result: readable = "1 hours 40 minutes"
请注意,大多数列表在缩减过程中都发生了变化,这有点值得怀疑。在相当纯的函数式编程语言中,我可能会因为写这个而被活活烧死。但是,由于 Python 不是一种函数式编程语言,所以它还不错。
另请注意,显示的一半代码用于计算 DIVISIONS
列表,该列表始终相同,因此可以手动预先计算。
关于python - 生成时间以供人类查看的更优雅版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11301808/