from . import *
from telethon import events
from datetime import datetime
from pytz import timezone

@ultroid_cmd(pattern="timeconv (.*)")
async def timeconv(e):
    input_str = e.pattern_match.group(1)
    input_str = input_str.split(" ")
    if len(input_str) != 3:
        await eor(e, "Please enter the command in the format: .timeconv hh:mm:ss from_timezone to_timezone")
        return
    time = input_str[0]
    from_tz = input_str[1]
    to_tz = input_str[2]
    try:
        from_zone = timezone(from_tz)
        to_zone = timezone(to_tz)
    except Exception as ex:
        await eor(e, f"Invalid timezone: {str(ex)}")
        return
    try:
        time = datetime.strptime(time, '%H:%M:%S')
        from_time = from_zone.localize(time)
        to_time = from_time.astimezone(to_zone)
        await eor(e, f"The time in {to_tz} timezone is {to_time.strftime('%H:%M:%S')}")
    except Exception as ex:
        await eor(e, f"Error: {str(ex)}")