简体   繁体   中英

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. 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) :

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:

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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