diff --git a/time_zone_conversion/README.md b/time_zone_conversion/README.md new file mode 100644 index 000000000..10dab60b3 --- /dev/null +++ b/time_zone_conversion/README.md @@ -0,0 +1,26 @@ +# Time Zone Conversion + +This is a python script that converts a time from one time zone to another. The possible time +zones considered are Eastern, Central, Mountain, or Pacific. + +--- + +## Steps to Run + +1. Clone/Download this repository +``` +git clone clone_path +``` +2. Run the program using command +``` +time_zone.py +``` +--- + +## Input +The user enters the time in the usual American way, such as 3:48pm or 11:26am. The first time zone the user enters is that of the original time and the second is the desired time zone. + +--- + +## Output + diff --git a/time_zone_conversion/output.PNG b/time_zone_conversion/output.PNG new file mode 100644 index 000000000..d1ccf000b Binary files /dev/null and b/time_zone_conversion/output.PNG differ diff --git a/time_zone_conversion/time_zone.py b/time_zone_conversion/time_zone.py new file mode 100644 index 000000000..59af59d30 --- /dev/null +++ b/time_zone_conversion/time_zone.py @@ -0,0 +1,84 @@ +class InvalidHours(Exception): + """Invalid""" + pass + + +class InvalidMins(Exception): + """Invalid""" + pass + + +# Taking user input +starting_zone = input("Enter starting zone: ") +time = input("Enter time: ") +end_zone = input("Enter desired time zone: ") + +lst = time.split(":") +hours1 = int(lst[0]) +mins1 = int(lst[1][:2]) +ampm = lst[1][2:] + +# Exception handling +try: + if hours1 >= 12: + raise InvalidHours +except InvalidHours: + print("Invalid Hours") + exit() + +try: + if mins1 >= 60: + raise InvalidMins +except InvalidMins: + print("Invalid Minutes") + +# Time conversion according to zone +if starting_zone == "CST": + hours1 = hours1 + 6 + pass +elif starting_zone == "EST": + hours1 = hours1 - 10 + pass +elif starting_zone == "MST": + hours1 = hours1 + 7 + pass +elif starting_zone == "PST": + hours1 = hours1 + 8 + +if hours1 > 12: + hours1 = hours1 - 12 + if ampm == 'am': + ampm = 'pm' + elif ampm == 'pm': + ampm = 'am' +elif hours1 < 0: + hours1 = hours1 + 12 + if ampm == 'am': + ampm = 'pm' + elif ampm == 'pm': + ampm = 'am' + +if end_zone == "CST": + hours1 = hours1 - 6 +elif end_zone == "EST": + hours1 = hours1 + 10 +elif end_zone == "MST": + hours1 = hours1 - 7 +elif end_zone == "PST": + hours1 = hours1 - 8 + +if hours1 > 12: + hours1 = hours1 - 12 + if ampm == 'am': + ampm = 'pm' + elif ampm == 'pm': + ampm = 'am' +elif hours1 < 0: + hours1 = hours1 + 12 + if ampm == 'am': + ampm = 'pm' + elif ampm == 'pm': + ampm = 'am' + +# Result +print('Time: ' + str(hours1) + ":" + str(mins1) + ampm)