Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def time_difference(time_start, time_end):
- '''Calculate the difference between two times on the same date.
- Args:
- time_start: the time to use as a starting point
- time_end: the time to use as an end point
- Returns:
- the difference between time_start and time_end. For example:
- >>> time_difference(1500, 1600)
- 60
- >>> time_difference(1100, 1310)
- 130
- Raises:
- TypeError: if time_start and time_end is not a string
- ValueError: if time_start and time_end is below 0 or above 2400
- '''
- try:
- if not isinstance(time_start, str):
- raise TypeError('time_start has to be a string!')
- elif not isinstance(time_end, str):
- raise TypeError('time_end has to be a string!')
- elif int(time_start) > 2400 or int(time_end) < 0:
- raise ValueError('time_start nor time_end can be below 0 or above 2400!')
- elif int(time_start) > int(time_end):
- raise ValueError('time_start cannot be higher than time_end!')
- except (TypeError, ValueError):
- raise
- # Convert time_start and time_end to necessary variables
- # for the calculation.
- hours_begin = time_start[0] + time_start[1]
- minutes_begin = time_start[2] + time_start[3]
- hours_end = time_end[0] + time_end[1]
- minutes_end = time_end[2] + time_end[3]
- # Calculates the difference.
- minutes = ((int(hours_end) - int(hours_begin)) * 60 +
- int(minutes_end) - int(minutes_begin))
- return(minutes)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement