Skip to content

Commit 123b4b0

Browse files
authored
Merge pull request #758 from PriyankaHotchandani/time-zone-conversion
Added Code for Time Zone Conversion
2 parents 018dd9e + 08bc655 commit 123b4b0

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed

time_zone_conversion/README.md

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Time Zone Conversion
2+
3+
This is a python script that converts a time from one time zone to another. The possible time
4+
zones considered are Eastern, Central, Mountain, or Pacific.
5+
6+
---
7+
8+
## Steps to Run
9+
10+
1. Clone/Download this repository
11+
```
12+
git clone clone_path
13+
```
14+
2. Run the program using command
15+
```
16+
time_zone.py
17+
```
18+
---
19+
20+
## Input
21+
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.
22+
23+
---
24+
25+
## Output
26+
<img src="output.PNG">

time_zone_conversion/output.PNG

74.7 KB
Loading

time_zone_conversion/time_zone.py

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
class InvalidHours(Exception):
2+
"""Invalid"""
3+
pass
4+
5+
6+
class InvalidMins(Exception):
7+
"""Invalid"""
8+
pass
9+
10+
11+
# Taking user input
12+
starting_zone = input("Enter starting zone: ")
13+
time = input("Enter time: ")
14+
end_zone = input("Enter desired time zone: ")
15+
16+
lst = time.split(":")
17+
hours1 = int(lst[0])
18+
mins1 = int(lst[1][:2])
19+
ampm = lst[1][2:]
20+
21+
# Exception handling
22+
try:
23+
if hours1 >= 12:
24+
raise InvalidHours
25+
except InvalidHours:
26+
print("Invalid Hours")
27+
exit()
28+
29+
try:
30+
if mins1 >= 60:
31+
raise InvalidMins
32+
except InvalidMins:
33+
print("Invalid Minutes")
34+
35+
# Time conversion according to zone
36+
if starting_zone == "CST":
37+
hours1 = hours1 + 6
38+
pass
39+
elif starting_zone == "EST":
40+
hours1 = hours1 - 10
41+
pass
42+
elif starting_zone == "MST":
43+
hours1 = hours1 + 7
44+
pass
45+
elif starting_zone == "PST":
46+
hours1 = hours1 + 8
47+
48+
if hours1 > 12:
49+
hours1 = hours1 - 12
50+
if ampm == 'am':
51+
ampm = 'pm'
52+
elif ampm == 'pm':
53+
ampm = 'am'
54+
elif hours1 < 0:
55+
hours1 = hours1 + 12
56+
if ampm == 'am':
57+
ampm = 'pm'
58+
elif ampm == 'pm':
59+
ampm = 'am'
60+
61+
if end_zone == "CST":
62+
hours1 = hours1 - 6
63+
elif end_zone == "EST":
64+
hours1 = hours1 + 10
65+
elif end_zone == "MST":
66+
hours1 = hours1 - 7
67+
elif end_zone == "PST":
68+
hours1 = hours1 - 8
69+
70+
if hours1 > 12:
71+
hours1 = hours1 - 12
72+
if ampm == 'am':
73+
ampm = 'pm'
74+
elif ampm == 'pm':
75+
ampm = 'am'
76+
elif hours1 < 0:
77+
hours1 = hours1 + 12
78+
if ampm == 'am':
79+
ampm = 'pm'
80+
elif ampm == 'pm':
81+
ampm = 'am'
82+
83+
# Result
84+
print('Time: ' + str(hours1) + ":" + str(mins1) + ampm)

0 commit comments

Comments
 (0)