简体   繁体   English

在Python中将秒转换为星期几小时几分秒

[英]Convert seconds to weeks-days-hours-minutes-seconds in Python

I'm trying to code a Python script for 'Enter the number of Seconds' and get results in weeks, days, hours, minutes and seconds. 我正在尝试为“输入秒数”编写Python脚本,并在几周,几天,几小时,几分钟和几秒钟内获得结果。 Here is what I have, but I am not getting the correct answers. 这是我所拥有的,但是我没有得到正确的答案。 What am I doing wrong? 我究竟做错了什么?

seconds = raw_input("Enter the number of seconds:")
seconds = int(seconds)

minutes = seconds/60

seconds = seconds % 60

hours = minutes/60
hours = seconds/3600

minutes = minutes % 60

days = hours/24
days = minutes/1440
days = seconds/86400

hours = hours % 60
hours = minutes % 60
hours = seconds % 3600

weeks = days/7
weeks = hours/168
weeks = minutes/10080
weeks = seconds/604800

days = days % 1
days = hours % 24
days = minutes % 1440
days = seconds % 86400

weeks = weeks % 1
weeks = days % 7
weeks = hours % 168
weeks = minutes % 10080
weeks = seconds % 604800

print weeks, 'weeks', days, 'days', hours, 'hours', minutes, 'minutes', seconds, 'seconds'

Just from the basic conversion principles: 仅根据基本转换原则:

weeks = seconds / (7*24*60*60)
days = seconds / (24*60*60) - 7*weeks
hours = seconds / (60*60) - 7*24*weeks - 24*days
minutes = seconds / 60 - 7*24*60*weeks - 24*60*days - 60*hours
seconds = seconds - 7*24*60*60*weeks - 24*60*60*days - 60*60*hours - 60*minutes

A bit of a less noisy way of doing the same thing: 做同样事情的方法比较少一些:

weeks = seconds / (7*24*60*60)
seconds -= weeks*7*24*60*60
days = seconds / (24*60*60)
seconds -= days*24*60*60
hours = seconds / (60*60)
seconds -= hours*60*60
minutes = seconds / 60
seconds -= minutes *60

A cleaner version of again the same thing with divmod function which returns both division result and remainder in a tuple (division, remainder) : 带有divmod函数的同一事物的更干净版本,它返回除法结果和元组中的(division, remainder)

weeks, seconds = divmod(seconds, 7*24*60*60)
days, seconds = divmod(seconds, 24*60*60)
hours, seconds = divmod(seconds, 60*60)
minutes, seconds = divmod(seconds, 60)

Basically, this solution is closest to your attempt since this is what divmod does: 基本上,此解决方案最接近您的尝试,因为这是divmod所做的:

weeks, seconds = divmod(seconds, 7*24*60*60)

equivalent to 相当于

weeks = seconds / (7*24*60*60)
seconds = seconds % (7*24*60*60)

Here we are essentially finding the number of whole weeks in our time and keeping what is left after these weeks are removed. 在这里,我们本质上是在寻找时间中的整个星期数,并保留这些星期后剩下的剩余时间。


And also you can go from the other end to make it even prettier: 另外,您可以从另一端做起,使其更漂亮:

minutes, seconds = divmod(seconds, 60)
hours, minutes = divmod(minutes, 60)
days, hours = divmod(hours, 24)
weeks, days = divmod(days, 7)

The idea behind this is that the number of seconds in your answer is the remainder after dividing them in minutes; 其背后的想法是,答案中的秒数是将它们除以分钟后的余数; minutes are the remainder of dividing all minutes into hours etc... This version is better because you can easily adjust it to months, years, etc... 分钟是将所有分钟数除以小时等的余数。此版本更好,因为您可以轻松地将其调整为月,年等。

Using Python's datetime timedeltas with support for milliseconds or microseconds. 在支持毫秒或微秒的情况下使用Python的datetime timedelta。

import datetime
def convert(sec): 
    td = datetime.timedelta(seconds=sec, microseconds=sec-int(sec)) 
    return td.days/7, (td.days/7)%7, td.seconds/3600, (td.seconds/60)%60, td.seconds%60, td.microseconds, td.microseconds/1000

seconds = 8*24*60*60 + 21627.123  # 8 days, 6 hours (21600 seconds), 27.123 seconds
w, d, h, m, s, us, ms = convert(seconds)
print '{}s / {}w {}d {}h {}m {}s {}us {}ms'.format(seconds,w,d,h,m,s,us,ms)

712827.123s / 1w 1d 6h 0m 27s 123000us 123ms
def humanize_duration(amount, units='s'):
INTERVALS = [(lambda mlsec:divmod(mlsec, 1000), 'ms'),
             (lambda seconds:divmod(seconds, 60), 's'),
             (lambda minutes:divmod(minutes, 60), 'm'),
             (lambda hours:divmod(hours, 24), 'h'),
             (lambda days:divmod(days, 7), 'D'),
             (lambda weeks:divmod(weeks, 4), 'W'),
             (lambda years:divmod(years, 12), 'M'),
             (lambda decades:divmod(decades, 10), 'Y')]

for index_start, (interval, unit) in enumerate(INTERVALS):
    if unit == units:
        break

amount_abrev = []
last_index = 0
amount_temp = amount
for index, (formula, abrev) in enumerate(INTERVALS[index_start: len(INTERVALS)]):
    divmod_result = formula(amount_temp)
    amount_temp = divmod_result[0]
    amount_abrev.append((divmod_result[1], abrev))
    if divmod_result[1] > 0:
        last_index = index
amount_abrev_partial = amount_abrev[0: last_index + 1]
amount_abrev_partial.reverse()

final_string = ''
for amount, abrev in amount_abrev_partial:
    final_string += str(amount) + abrev + ' '
return final_string

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM