The code defines a Python script for converting time between 12-hour and 24-hour formats.
Review Goals:
- Is the code idiomatic? (Am I using any outdated practices or functions?)
- Is there a simpler way to achieve the same thing?
- Are the tests well thought of?
- Does any part of my code require a comment?
- How can I improve the code?
The code was formatted with autopep8
.
#!/usr/bin/env python3
def _convert_to_24_hr(hr: int, mins: int, time_ind: str) -> str:
if time_ind.upper() == 'PM':
hr = hr + 12 if hr != 12 else 0
return f'{hr}:{mins:0>2} {time_ind.upper()}'
def _convert_to_12_hr(hr: int, mins: int) -> str:
time_ind = 'PM' if hr >= 12 else 'AM'
hr = 12 if hr == 0 else (hr - 12 if hr > 12 else hr)
return f'{hr}:{mins:0>2} {time_ind.upper()}'
def time_conversion(hr: int, mins: int, fmt: str, time_ind: str = None) -> str:
"""Converts a time from one format to another (either 12-hour or 24-hour).
Args:
hr: The hour part of the time.
mins: The minute part of the time.
format: A string representing the desired time format ('12-hour'
or '24-hour').
time_ind: An optional time indicator ('AM' or 'PM') used in 12-hour
format conversion.
Returns: The converted time as a string in the specified format
on success. Else, it returns 'Invalid arguments.'.
Note:
- If '24-hour' format is selected, the time_ind argument must be provided.
- The function handles both upper and lower case format and time indicator
strings.
"""
conditions_24 = [1 <= hr <= 12,
0 <= mins <= 59,
time_ind and time_ind.upper() in ['AM', 'PM']]
conditions_12 = [0 <= hr <= 23,
0 <= mins <= 59]
if fmt.lower() == '24-hour':
if all(conditions_24):
# Valid 12-hour format
return _convert_to_24_hr(hr, mins, time_ind)
return 'Invalid arguments.'
if fmt.lower() == '12-hour':
if all(conditions_12):
# Valid 12-hr format
return _convert_to_12_hr(hr, mins)
return 'Invalid arguments.'
def _test(got: str, expected: str) -> None:
if got == expected:
prefix = ' OK '
else:
prefix = ' X '
print(f'{prefix.ljust(4)} | got: {got.ljust(20)} | '
f'expected: {expected.ljust(20)}')
def _main():
_test(time_conversion(15, 30, '12-hour'), '3:30 PM')
_test(time_conversion(3, 30, '24-hour', 'PM'), '15:30 PM')
_test(time_conversion(13, 30, '24-hour', 'AM'), 'Invalid arguments.')
_test(time_conversion(25, 20, '12-hour'), 'Invalid arguments.')
_test(time_conversion(13, 5, '12-hour'), '1:05 PM')
_test(time_conversion(13, 5, '24-hour'), 'Invalid arguments.')
_test(time_conversion(10, 5, '24-hour', 'am'), '10:05 AM')
_test(time_conversion(10, 5, '24-hour', 'PM'), '22:05 PM')
_test(time_conversion(17, 5, '12-hour'), '5:05 PM')
_test(time_conversion(25, 32, '12-hour'), 'Invalid arguments.')
_test(time_conversion(0, 23, '12-hour'), '12:23 AM')
_test(time_conversion(12, 0, '12-hour'), '12:00 PM')
_test(time_conversion(-4, -3, '13-hour'), 'Invalid arguments.')
_test(time_conversion(1003, 29, '39 hour', None), 'Invalid arguments.')
if __name__ == '__main__':
_main()
P.S: I would like to mention that this is my first Python script, or at least the first one I am posting for review. Feel free to expatiate.
_convert_to_12_hh
erroneously converts midnight to noon.time_ind = 'PM' if hh >= 12 else 'AM'
==>time_ind = 'PM' if hh >= 12 and hh != 0 else 'AM'
. It is fixed now. \$\endgroup\$15:30 PM
, or any other 24 hour format time with am/pm following (e.g.08:30 AM
) as an error. Either15:30
or3:30 PM
(in fact for me15.30
or3.30pm
). \$\endgroup\$