Esempio n. 1
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)
Esempio n. 2
0
def add(timezone: str):
    """
    Add timezone to the config file.
    """
    validate_timezone(timezone)
    config_file = variables.config_file

    if not 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:"
        )
Esempio n. 3
0
def search(query: str):
    """
    Get time based on the entered timezone.
    """
    try:
        # Search with user query.
        # @TODO: Handle list with multiple data.
        data: List = pycountry.countries.search_fuzzy(query)

        # extract alpha2 value
        _, _, alpha_2, _ = extract_fuzzy_country_data(data)

        # Get a list of timezone names.
        result = get_timezones(alpha_2)

        payload: List = []

        # If length is greater than one, show terminal menu.
        if len(result) > 1:
            entry = handle_interaction(result)

            payload.append(entry)

            return get_local_time(payload)
    except LookupError:
        return console.print(
            "Couldn't resolve your query, please try other keywords.:x:"
        )

    return get_local_time(result)
Esempio n. 4
0
def select():
    """
    Interactively select the timezone from your config file to get local time.
    """
    config_file = variables.config_file
    entry = []

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

        if not len(data):
            return console.print("Config file contains no timezone:x:")

        entry.append(handle_interaction(data))

        return get_local_time(entry)
Esempio n. 5
0
def remove(name: Optional[str], interactive: bool):
    """
    Remove timezone to the config file.
    """
    exists = interactive or name

    if not exists:
        return print_help_msg(remove)

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

    if name:
        validate_timezone(name)

    remove_timezone(interactive, name)
Esempio n. 6
0
def show():
    """
    Show time based on the defaults at .tz-cli file.
    """
    check_config: Union[List, bool] = check_configuration()

    if not check_config:
        console.print(
            "File is empty or No configuration file is present in your system.:x:\n",
            style="bold red",
        )
        console.print(
            "Use `tz add` to create and add timezone to your config file.:memo:\n",
            style="bold green",
        )

        console.print(
            f"Your system datetime is: {get_system_time()}",
            style="bold yellow",
        )
        sys.exit()

    return get_local_time(check_config)