58 lines
1.4 KiB
Python
Executable File
58 lines
1.4 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
"""Convert time between zones"""
|
|
|
|
from sys import argv
|
|
import re
|
|
|
|
|
|
def get_int(prompt, argnum):
|
|
"""Gets number
|
|
|
|
If var is a number, return it. Else, get number from input until it
|
|
doesn't fail.
|
|
"""
|
|
try:
|
|
number = int(argv[argnum])
|
|
except (ValueError, IndexError):
|
|
number = None
|
|
while number is None:
|
|
try:
|
|
number = int(input(prompt))
|
|
except ValueError:
|
|
number = None
|
|
return number
|
|
|
|
|
|
# They all return a value. Pylint is wrong
|
|
# pylint: disable=inconsistent-return-statements
|
|
def get_time(prompt, argnum):
|
|
"""Gets a time, in the same vein as get_int"""
|
|
time_regex = re.compile("([01]?[0-9]|2[0-3]):[0-5][0-9]")
|
|
try:
|
|
if re.match(time_regex, argv[argnum]):
|
|
return argv[argnum]
|
|
except (ValueError, IndexError):
|
|
time_inp = ""
|
|
while not re.match(time_regex, time_inp):
|
|
time_inp = input(prompt)
|
|
return time_inp
|
|
|
|
|
|
tz_from = get_int("From: ", 1)
|
|
tz_to = get_int("To: ", 2)
|
|
conv_time = get_time("Time: ", 3).split(':')
|
|
|
|
hours = conv_time[0]
|
|
minutes = conv_time[1]
|
|
|
|
leading_zero = len(hours) == 2
|
|
|
|
# `hours' is NOT a constant
|
|
# pylint: disable=invalid-name
|
|
hours = str((int(hours) + abs(tz_from - tz_to)) % 24)
|
|
if leading_zero and int(hours) < 10:
|
|
hours = "0" + hours
|
|
|
|
print(f"{hours}:{minutes}")
|