#!/usr/bin/env python3 # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, # MA 02110-1301, USA. from sys import argv def get_argv(): class ScaleError(Exception): pass class RangeError(Exception): pass if len(argv) < 3: print("Usage: {0} 00:00 m|am|pm".format(argv[0])) exit() try: part = argv[2].lower() if not part in {"m", "am", "pm"}: raise ScaleError("unknown scale '{0}', use 'm (military)', 'am' or 'pm'.".format(part)) time = argv[1].split(":") hour = int(time[0]) max = 23 if part == "m" else 12 min = 0 if part == "m" else 1 if not (min <= hour <= max): raise RangeError("{0} hour(s) does not fall within {1} to {2}".format(hour, min, max)) minute = int(time[1]) if not (0 <= minute < 60): raise RangeError("{0} minute(s) does not fall within 0 to 59".format(minute)) except (ValueError, RangeError, ScaleError) as err: print("Error:", err) exit() return hour, minute, part def military_to_standerd(hour): if hour == 0: h = 12 part = "am" if hour == 12: h = 12 part = "pm" elif hour > 12: h = hour - 12 part = "pm" else: h = hour part = "am" return h, part def standerd_to_military(hour, part): if hour == 12 and part == "am": h = 0 elif hour == 12 and part == "pm": h = 12 elif part in "pm": h = hour + 12 else: h = hour return h def main(): hour, minute, part = get_argv() if part in "m": hour, part = military_to_standerd(hour) p = "{0}:{1} {2}" if len(str(minute)) == 2 else "{0}:0{1} {2}" print(p.format(hour, minute, part)) elif part in {"am", "pm"}: hour = standerd_to_military(hour, part) p = "{0}:{1}" if len(str(minute)) == 2 else "{0}:0{1}" p = p if len(str(hour)) == 2 else "0{0}".format(p) print(p.format(hour, minute)) if __name__ == "__main__": main()