import requests


def get_time(msg):
    """
    Sample function that returns the current time for a provided timezone
    :param incoming_msg: The incoming message object from Teams
    :return: A Response object based reply
    """
    # Extract the message content, without the command "/time"
    message = msg.text.split()
    timezone = message[2]
    print(timezone)

    # Craft REST API URL to retrieve current time
    #   Using API from http://worldclockapi.com
    base_url = "http://worldtimeapi.org/api/timezone/"
    url = f"{base_url}{timezone}"

    r = requests.get(url)

    # print(r)
    # print(r.text)
    # print(dir(r))
    # print(json.dumps(r.json(), indent=4))
    # print(r.ok)

    if r.ok:
        resp_json = r.json()
        # Format of returned data is "YYYY-MM-DDTHH:MM<OFFSET>"
        #   Example "2018-11-11T22:09-05:00"
        returned_data = resp_json["datetime"].split("T")
        cur_date = returned_data[0]
        cur_time = returned_data[1][:5]

        # Craft a reply string.
        reply = f"Timezone: {resp_json['timezone']} \n\tCurrent time is {cur_time} \n\tCurrent date is {cur_date} " \
                f"\n\tUTC Offset is {resp_json['utc_offset']}" \
                f"\n\tWeek Number is {resp_json['week_number']}"
    else:
        reply = f"ERROR! Response: {r}\nPlease use the correct timezone.  " \
                f"See [Canonical Timezone List](http://worldtimeapi.org/timezones)."
    # print(reply)

    return reply