billion = 1000000000 sec_365 = 31536000 # (60 * 60 * 24 *365) sec_366 = 31622400 # leap years sec_day = 86400 # 1day = 86400seconds sec_hour = 3600 # 1hour = 3600seconds sec_min = 60 years = 0 days = 0 hours = 0 minutes = 0 seconds = 0 def billionConvertToTime(secs): global years global days global hours global minutes global seconds if secs < sec_min: seconds = secs return years, days, hours, minutes, seconds elif secs > sec_365: if years % 4 == 0: # leap years secs = secs - sec_366 years += 1 billionConvertToTime(secs) else: secs = secs - sec_365 years += 1 billionConvertToTime(secs) elif secs > sec_day: secs = secs - sec_day days += 1 billionConvertToTime(secs) elif secs > sec_hour: secs = secs - sec_hour hours += 1 billionConvertToTime(secs) elif secs > sec_min: secs = secs - sec_min minutes += 1 billionConvertToTime(secs) billionConvertToTime(billion) print ("One billion seconds equals to %s years, %s days, %s hours, %s minutes and %s seconds" % (years, days, hours, minutes, seconds))Output:
One billion seconds equals to 31 years, 251 days, 1 hours, 46 minutes and 40 seconds
No comments:
Post a Comment