pythondatetimeutcpython-arrow

Python Arrow datetime function returns a timestamp with seconds field value more than 60


I have a function in my code for taking a UTC time argument like - 2023-11-06T20:53:39.062Z, convert it to EST time and then return in a format as 'MM-DD-YYYY HH:MM:SS'.

def date_conv(time):
    est = zoneinfo.ZoneInfo('America/Toronto')
    est_time = arrow.get(time).astimezone(est)
    return str(arrow.get(est_time).format("MM-DD-YYYY HH:MM:SS"))

However this sometimes returns a time as 16-11-2023 15:11:79with seconds field > 60, which is not desirable. What is exactly wrong in the code?


Solution

  • Use "MM-DD-YYYY HH:mm:ss" for the format.

    MM is for month (and you are using it for the minutes) and SS is for subseconds, that's why you get a value greater than 60.

    See full docs to know all tokens: https://arrow.readthedocs.io/en/latest/guide.html#supported-tokens