Example #1
0
def add(timezone: str):
    """
    Add timezone to the config file.
    """
    utils.validate_timezone(timezone)
    config_file = variables.config_file

    if not utils.check_config():
        with open(config_file, "w+") as newfile:
            pass

    with open(config_file, "r+") as file:
        data: List = [line.rstrip() for line in file]

        if timezone in data:
            return console.print(
                f"[bold green]Timezone already exists:[/bold green] [bold red]{timezone}:x:[/bold red]"
            )

        # Read file
        file.read()

        # Add to the end of the file.
        file.write(f"{timezone}\n")
        console.print(
            f"[bold green]New timezone added successfully:[/bold green] [bold blue]{timezone}[/bold blue] :white_check_mark:"
        )
Example #2
0
def test_validate_timezone_failure():
    """
    Tests for invalid timezones.
    """
    with pytest.raises(SystemExit):
        assert utils.validate_timezone("Python/Tests")
        assert utils.validate_timezone("Asia/Catmandu")
Example #3
0
def utc(time, timezone):
    """
    Convert a specific time from any timezone to UTC.

    > Hours are calculated in 24 hours format. You can specify 'AM' or 'PM' if you are using 12 hours format.

    EXAMPLE: \n

    $ tz utc # show current system time in UTC.

    $ tz utc "8:15" "Asia/Kathmandu" # will be evaluated as AM, following the 24 hour format.

    $ tz utc "20:15" "Asia/Kathmandu" # will be evaluated as PM despite any suffix, following the 24 hour format.

    $ tz utc "8:15 PM" "Asia/Kathmandu" # will be evaluated as specified.
    """
    if not time or not timezone:
        get_local_utc_time()

    try:
        validate_timezone(timezone)
        hour, minute, time_suffix = validate_time(time)
    except Exception:
        console.print("[bold red]:x:Invalid input value[/bold red]")
        sys.exit(0)

    time = f"{str(hour).zfill(2)}:{str(minute).zfill(2)} {time_suffix}"

    return get_utc_time(hour, minute, timezone, time)
Example #4
0
def remove(name: Optional[str], interactive: bool):
    """
    Remove timezone to the config file.
    """
    exists = interactive or name

    if not exists:
        return utils.print_help_msg(remove)

    if name and interactive:
        return console.print("Cannot use both flags at the same time.:x:")

    if name:
        utils.validate_timezone(name)

    utils.remove_timezone(interactive, name)
Example #5
0
def test_validate_timezone():
    """
    Tests for valid timezone.
    """
    assert utils.validate_timezone("Asia/Kathmandu") == True
    assert utils.validate_timezone("Asia/Kolkata") == True