Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. class Time:
  2. """Represents and manipulates hours and minutes"""
  3. def __init__(self, hour=12, minute=0):
  4. valid_hour, valid_minute = Time.make_valid(hour, minute)
  5. self.__hour = valid_hour
  6. self.__minute = valid_minute
  7.  
  8. def __str__(self):
  9. return "{0}:{1:02}".format(self.__hour, self.__minute)
  10.  
  11. def __add__(self, other):
  12. return Time.add_minute(Time.add_hour(self, other.__hour),
  13. other.__minute)
  14.  
  15. def get_hour(self):
  16. return self.__hour
  17.  
  18. def get_minute(self):
  19. return self.__minute
  20.  
  21. def is_valid_time(self):
  22. return Time.is_valid_hour(self.__hour) and Time.is_valid_minute(self.__minute)
  23.  
  24. def add(self, other):
  25. result_time = self + other
  26. self.__hour = result_time.__hour
  27. self.__minute = result_time.__minute
  28.  
  29. @staticmethod
  30. def add_hour(other, hour):
  31. """
  32. :rtype: Time
  33. """
  34. return Time(other.__hour + hour, other.__minute)
  35.  
  36. @staticmethod
  37. def add_minute(other, minute):
  38. return Time(other.__hour, other.__minute + minute)
  39.  
  40. @staticmethod
  41. def is_valid_hour(hour):
  42. return 1 <= hour <= 12
  43.  
  44. @staticmethod
  45. def is_valid_minute(minute):
  46. return 0 <= minute < 60
  47.  
  48. @staticmethod
  49. def make_valid(hour, minute):
  50. valid_minute, carryover_hour = Time.make_valid_minute(minute)
  51. valid_hour = Time.make_valid_hour(hour)
  52. return valid_hour + carryover_hour, valid_minute
  53.  
  54. @staticmethod
  55. def make_valid_hour(hour):
  56. if not Time.is_valid_hour(hour):
  57. valid_hour = hour % 12
  58. else:
  59. valid_hour = hour
  60. return valid_hour
  61.  
  62. @staticmethod
  63. def make_valid_minute(minute):
  64. """
  65.  
  66. :param minute:
  67. :return:
  68. """
  69. if not Time.is_valid_minute(minute):
  70. valid_minute = minute % 60
  71. carryover_hour = minute // 60
  72. else:
  73. valid_minute = minute
  74. carryover_hour = 0
  75. return valid_minute, carryover_hour
  76.  
  77. @staticmethod
  78. def from_string(time_str):
  79. """
  80. Semi-constructor from string
  81. :param time_str: String representing time (ex: 5:09)
  82. :return: Time object represented by time_str
  83. """
  84. hour, minute = map(int, time_str.strip().split(":"))
  85. return Time(hour, minute)
  86.  
  87.  
  88. def get_time_from_user(time_kind):
  89. """
  90. Prompts for "time_kind time" and returns inputted time
  91. :rtype: Time
  92. """
  93. try:
  94. entered_time = (input("Enter " + time_kind + " time (HH:MM): "))
  95. return Time.from_string(entered_time)
  96. except ValueError:
  97. print("Must enter a time (ex: 5:09)")
  98. return get_time_from_user(time_kind)
  99. except ImportError:
  100. print("Entered time not valid (1-12)")
  101. return get_time_from_user(time_kind)
  102.  
  103. startTime = get_time_from_user("start")
  104. timerTime = get_time_from_user("timer")
  105. endTime = startTime + timerTime
  106. print("End time: " + str(endTime))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement