from datetime import datetime


def ts_format(timestamp):
    """ Format unix timestamp to human readable. Automatically detects timestamps with seconds or milliseconds. """

    if len(str(timestamp)) > 11:
        return datetime.fromtimestamp(timestamp/1000).strftime('%x %X %z')
    else:
        return datetime.fromtimestamp(timestamp).strftime('%x %X %z')