|
| 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