如何将秒转换为适合人类的持续时间? [英] How do I convert seconds into human-friendly time durations?

查看:66
本文介绍了如何将秒转换为适合人类的持续时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Python脚本几乎就在那里,所以这里是:



My Python script is nearly there, so here it is:

def prettyList(human_time):
    if len(human_time) > 1:
        return ' '.join([', '.join(human_time[:-1]), "and", human_time[-1]])
    elif len(human_time) == 1:
        return human_time[0]
    else:
        return ""

def format_duration(seconds, granularity = 5):
    intervals = (('years', 29030400), ('months', 2419200), ('weeks', 604800),('days', 86400),('hours', 3600), ('minutes', 60), ('seconds', 1))
    human_time = []
    for name, count in intervals: 
        value = seconds // count
        if value: 
            seconds -= value * count
            if value == 1:
                name = name.rstrip('s')
            human_time.append("{} {}".format(value, name))
    if not human_time:
        return "now"
    human_time = human_time[:granularity]
    return prettyList(human_time)





代码应输入秒,然后用适当的语法读取持续时间列表(年,月) ,周,天,小时,分钟,秒)。我的测试正在通过但是对于其中一个较大的测试,它正在阅读以下内容:





The code should take an input of seconds, which then reads with proper grammar a list of durations (years, months, weeks, days, hours, minutes, seconds). My tests are passing but for one of the larger tests, it's reading the following:

'6 months, 2 weeks, 1 hour, 44 minutes and 40 seconds' should equal '182 days, 1 hour, 44 minutes and 40 seconds'





基本上,它要求我将现在的6个月和2个星期翻译成182天。请指教!



我的尝试:



我是尝试将秒转换为持续时间,然后根据正确的逗号和和连接以使输出语句语法上合理。我的语法很重要,现在是给我带来麻烦的持续时间!



Essentially it's requiring that I translate what's now 6 months and 2 weeks into 182 days. Please advise!

What I have tried:

I've attempted to translate seconds as durations, then join in accordance with the proper commas and "and"s to make the output statement grammatically sound. My grammar is on point, it's the durations which are givin me trouble now!

推荐答案

你确定这是你的代码,你不能让它回复天数?

变化非常简单,你应该可以自己动手。



错误报告:

您应该检查每年和每月的秒数,有一个惊喜:)



Are you sure this is your code and that you can't make it reply in number of days ?
the change is really easy, you should be able to do it yourself.

Bug report:
You should check the number of seconds per years and per months, there is a surprise :)

引用:

'6个月,2周,1小时,44分40秒'应该等于'182天,1小时,44分40秒'

'6 months, 2 weeks, 1 hour, 44 minutes and 40 seconds' should equal '182 days, 1 hour, 44 minutes and 40 seconds'

如果你曲柄你的大脑一点,你应该发现182天大约是半年或6个月,而不是6个月和2个星期!



[更新]

If you crank your brain a little, you should discover that 182 days is about half a year or 6 months, not 6 months and 2 weeks!

[Update]

Quote:

该函数必须接受一个非负整数。如果它为零,则只返回now。否则,持续时间表示为年,日,小时,分钟和秒的组合。

The function must accept a non-negative integer. If it is zero, it just returns "now". Otherwise, the duration is expressed as a combination of years, days, hours, minutes and seconds.



那么,你在哪里看到你需要几个月和几周?


So, Where have you seen that you needed months and weeks ?


这篇关于如何将秒转换为适合人类的持续时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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