Guest User

Untitled

a guest
Jun 18th, 2018
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. year = 31536000
  2. month = 2592000
  3. week = 604800
  4. day = 86400
  5. hour = 3600
  6.  
  7.  
  8. from math import floor
  9.  
  10.  
  11. def parse_time(time_s):
  12. if time_s == 0:
  13. return None
  14. if time_s < hour:
  15. return 'меньше часа'
  16. if time_s >= year:
  17. return '{} г. '.format(floor(time_s / year)) + parse_time(time_s % year)
  18. if time_s >= month:
  19. return '{} мес. '.format(floor(time_s / month)) + parse_time(time_s % month)
  20. elif time_s >= week:
  21. return '{} нед. '.format(floor(time_s / week)) + parse_time(time_s % week)
  22. elif time_s >= day:
  23. return '{} д. '.format(floor(time_s / day)) + parse_time(time_s % day)
  24. else:
  25. return '{} ч.'.format(floor(time_s / hour))
  26.  
  27. print(parse_time(2827567.5759670734)) # 1 мес. 2 д. 17 ч.
  28. print(parse_time(269649.6857390404)) # 3 д. 2 ч.
  29.  
  30. def parse_time(time_s):
  31. year = 31536000
  32. month = 2592000
  33. week = 604800
  34. day = 86400
  35. hour = 3600
  36.  
  37. if time_s == 0:
  38. return None
  39.  
  40. if time_s < hour:
  41. return 'меньше часа'
  42.  
  43. result = ''
  44.  
  45. if time_s >= year:
  46. result += '{} г. '.format(int(time_s / year))
  47. time_s %= year
  48.  
  49. if time_s >= month:
  50. result += '{} мес. '.format(int(time_s / month))
  51. time_s %= month
  52.  
  53. if time_s >= week:
  54. result += '{} нед. '.format(int(time_s / week))
  55. time_s %= week
  56.  
  57. if time_s >= day:
  58. result += '{} д. '.format(int(time_s / day))
  59. time_s %= day
  60.  
  61. if time_s >= hour:
  62. result += '{} ч.'.format(int(time_s / hour))
  63.  
  64. return result
  65.  
  66. print(parse_time(2827567.5759670734)) # 1 мес. 2 д. 17 ч.
  67. assert parse_time(2827567.5759670734) == "1 мес. 2 д. 17 ч."
  68.  
  69. print(parse_time(269649.6857390404)) # 3 д. 2 ч.
  70. assert parse_time(269649.6857390404) == "3 д. 2 ч."
Add Comment
Please, Sign In to add comment