from typing import Any
import socket
import struct
import time


def ntp(server:str='a.st1.ntp.br')->Any:
    """
    Get current time from a ntp server
    """
    
    REFERENCE_TIME = 2208988800
    port           = 123
    buffer         = 1024
    data           = '\x1b' + 47 * '\0'

    try:
        client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        client.sendto(data.encode('utf-8'),(server, port))
        msg, address = client.recvfrom(buffer)
        if msg:
            t = struct.unpack( '!12I',msg)[10]
            t -= REFERENCE_TIME
            return time.ctime(t).replace("  "," ")
        return "Data not found"
    except:
        return "Error connecting to server"