Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- year = 31536000
- month = 2592000
- week = 604800
- day = 86400
- hour = 3600
- from math import floor
- def parse_time(time_s):
- if time_s == 0:
- return None
- if time_s < hour:
- return 'меньше часа'
- if time_s >= year:
- return '{} г. '.format(floor(time_s / year)) + parse_time(time_s % year)
- if time_s >= month:
- return '{} мес. '.format(floor(time_s / month)) + parse_time(time_s % month)
- elif time_s >= week:
- return '{} нед. '.format(floor(time_s / week)) + parse_time(time_s % week)
- elif time_s >= day:
- return '{} д. '.format(floor(time_s / day)) + parse_time(time_s % day)
- else:
- return '{} ч.'.format(floor(time_s / hour))
- print(parse_time(2827567.5759670734)) # 1 мес. 2 д. 17 ч.
- print(parse_time(269649.6857390404)) # 3 д. 2 ч.
- def parse_time(time_s):
- year = 31536000
- month = 2592000
- week = 604800
- day = 86400
- hour = 3600
- if time_s == 0:
- return None
- if time_s < hour:
- return 'меньше часа'
- result = ''
- if time_s >= year:
- result += '{} г. '.format(int(time_s / year))
- time_s %= year
- if time_s >= month:
- result += '{} мес. '.format(int(time_s / month))
- time_s %= month
- if time_s >= week:
- result += '{} нед. '.format(int(time_s / week))
- time_s %= week
- if time_s >= day:
- result += '{} д. '.format(int(time_s / day))
- time_s %= day
- if time_s >= hour:
- result += '{} ч.'.format(int(time_s / hour))
- return result
- def parse_time(time_s):
- items = [
- (31536000, '{} г. '),
- (2592000, '{} мес. '),
- (604800, '{} нед. '),
- (86400, '{} д. '),
- (3600, '{} ч.'),
- ]
- if time_s == 0:
- return None
- if time_s < 3600:
- return 'меньше часа'
- result = ''
- for value, fmt in items:
- if time_s >= value:
- result += fmt.format(int(time_s / value))
- time_s %= value
- return result
- print(parse_time(2827567.5759670734)) # 1 мес. 2 д. 17 ч.
- assert parse_time(2827567.5759670734) == "1 мес. 2 д. 17 ч."
- print(parse_time(269649.6857390404)) # 3 д. 2 ч.
- assert parse_time(269649.6857390404) == "3 д. 2 ч."
Add Comment
Please, Sign In to add comment