Skip to content

Commit 8b29195

Browse files
committed
Added timezone_cli files
1 parent 45d4ef4 commit 8b29195

File tree

8 files changed

+128
-0
lines changed

8 files changed

+128
-0
lines changed

timezone_cli/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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)

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

0 commit comments

Comments
 (0)