Skip to content

Commit b0472be

Browse files
authored
Merge pull request #746 from piyushmohan01/main
CLI tool for Timezones
2 parents 362fd6d + 8d32e3e commit b0472be

File tree

8 files changed

+138
-0
lines changed

8 files changed

+138
-0
lines changed

timezone_cli/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# CLI Tool for Timezones
2+
3+
- This tool lets the user find the time pertaining to various timezones around the globe.
4+
- It allows users to pass a timezone of their choice or have ten common timezone results printed.
5+
- On top of that, the user can also pass two different timezones to get the comparisons.
6+
- The script uses the [pytz](http://pytz.sourceforge.net/) library which provides the required functionalities withing the context of python (2.4 or higher).
7+
8+
## Setup instructions
9+
10+
- Install libraries : ```pip install -r requirements.txt```
11+
- Run the script with parameters : ```python tz_tool.py [params]```
12+
13+
## Parameter instructions
14+
- Here's the list of parameters that can be passed :
15+
- ```--help``` : Get info about the paramters
16+
- ```--cm``` : Get time of 10 common timezones
17+
- ```--cu [timezone]``` : Get time of custom timezone
18+
- ```--cp [timezone_1]-[timezone_2]``` : Compare differences between two timezones
19+
- Points to note :
20+
- Always pass valid timezones (View examples below)
21+
- When comparing, pass two '-' seperated timezones
22+
- Examples :
23+
- ```python tz_tool.py --help```
24+
- ```python tz_tool.py --cm```
25+
- ```python tz_tool.py --cu Europe/Rome```
26+
- ```python tz_tool.py --cp Asia/Kolkata-Europe/Rome```
27+
28+
## Sample Output
29+
30+
![](./images/markdown-sample.JPG)
31+
32+
## Author(s)
33+
- [Piyush Mohan](https://github.com/piyushmohan01)

timezone_cli/images/common.JPG

69.1 KB
Loading

timezone_cli/images/compare.JPG

32.1 KB
Loading

timezone_cli/images/custom.JPG

30.9 KB
Loading

timezone_cli/images/help.JPG

40.3 KB
Loading
124 KB
Loading

timezone_cli/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pytz==2021.3
2+
python-dateutil==2.8.2

timezone_cli/tz_tool.py

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import sys
2+
import getopt
3+
from datetime import datetime
4+
from pytz import timezone
5+
from dateutil.relativedelta import relativedelta
6+
7+
8+
# --- INFO ON PARAMS ---
9+
def print_help():
10+
print('[+] use --cu and pass custom timezone to find time.')
11+
print('[+] use --cm to get the time of common timezones around the world.')
12+
print('[+] use --cmp and pass two \'-\' seperated timezones to compare.')
13+
print('[+] use --help again if needed.')
14+
print('\n' + '-' * 50 + '\n')
15+
16+
17+
# --- GET DATETIME OF PASSED TIMEZONE ---
18+
def get_datetime(time_zone):
19+
utcnow = timezone('utc').localize(datetime.utcnow())
20+
output_datetime = utcnow.astimezone(timezone(time_zone)).replace(tzinfo=None)
21+
return output_datetime
22+
23+
24+
# --- GET DATETIME OF CUSTOM TIMEZONE ---
25+
def get_custom_tz(time_zone):
26+
response = str(get_datetime(time_zone))
27+
print(f'%20s' % 'Timezone' + ' :' + ' ' * 2 + time_zone) # noqa
28+
date, time = response.split(' ')
29+
hours, minutes, seconds = time.split(':')
30+
print('%20s' % 'Hours' + ' :' + ' ' * 2 + hours)
31+
print('%20s' % 'Minutes' + ' :' + ' ' * 2 + minutes)
32+
print('%20s' % 'Seconds' + ' :' + ' ' * 2 + seconds)
33+
print('%20s' % 'Date' + ' :' + ' ' * 2 + date)
34+
print('\n' + '-' * 50 + '\n')
35+
36+
37+
# --- GET DATETIMES OF COMMON TIMEZONES ---
38+
def get_common_tz():
39+
common_tz_list = [
40+
'America/Sao_Paulo',
41+
'America/New_York',
42+
'America/Mexico_City',
43+
'Asia/Kolkata',
44+
'Asia/Tokyo',
45+
'Europe/Paris',
46+
'Europe/Berlin',
47+
'Africa/Johannesburg',
48+
'Africa/Casablanca',
49+
'Australia/Melbourne',
50+
]
51+
for item in common_tz_list:
52+
print('%-20s' % item, end='')
53+
print(':' + ' ' * 2 + str(get_datetime(item)))
54+
print('\n' + '-' * 50 + '\n')
55+
56+
57+
# --- COMPARE DIFFERENCES BTW TWO TIMEZONES ---
58+
def get_comparisons(tz_1, tz_2):
59+
offset = relativedelta(get_datetime(tz_1), get_datetime(tz_2))
60+
print('%25s' % 'Comparing :', tz_1)
61+
print('%25s' % 'With :', tz_2)
62+
print('')
63+
print('%25s' % 'Hours :', offset.hours)
64+
print('%25s' % 'Minutes :', offset.minutes)
65+
print('%25s' % 'Seconds :', offset.seconds)
66+
print('\n' + '-' * 50 + '\n')
67+
68+
69+
# --- MAIN FUNCTION ---
70+
def Main():
71+
print('\n' + '-' * 50 + '\n')
72+
print(' ' * 16 + 'TIMEZONE TOOL')
73+
print('\n' + '-' * 50 + '\n')
74+
75+
# --- SET DEFAULTS ---
76+
argument_list = sys.argv[1:]
77+
options = ''
78+
long_options = ['cu=', 'cm', 'cp=', 'help']
79+
80+
# --- TRY-CATCH BLOCK FOR ARGS ---
81+
try:
82+
args, _ = getopt.getopt(argument_list, shortopts=options, longopts=long_options)
83+
for current_argument, current_value in args:
84+
if current_argument in ('--help'):
85+
print_help()
86+
if current_argument in ('--cu'):
87+
get_custom_tz(current_value)
88+
if current_argument in ('--cm'):
89+
get_common_tz()
90+
if current_argument in ('--cp'):
91+
timezones = current_value.split('-')
92+
if len(timezones) == 2:
93+
get_comparisons(timezones[0], timezones[1])
94+
else:
95+
print(' ' * 5, '[+] Seperate the Timezones with \'-\'')
96+
print('\n' + '-' * 50 + '\n')
97+
98+
except getopt.error as error:
99+
print(str(error))
100+
101+
102+
if __name__ == '__main__':
103+
Main()

0 commit comments

Comments
 (0)