[Python] Converting timezone-aware datetime to local time in Python

Discussão em 'Python' iniciado por Stack, Maio 7, 2025.

  1. Stack

    Stack Membro Participativo

    How do you convert a timezone-aware datetime object to the equivalent non-timezone-aware datetime for the local timezone?

    My particular application uses Django (although, this is in reality a generic Python question):

    import iso8601


    ....

    date_str="2010-10-30T17:21:12Z"


    ....

    d = iso8601.parse_date(date_str)

    foo = app.models.FooModel(the_date=d)
    foo.save()


    This causes Django to throw an error:

    raise ValueError("MySQL backend does not support timezone-aware datetimes.")


    What I need is:

    d = iso8601.parse_date(date_str)
    local_d = SOME_FUNCTION(d)
    foo = app.models.FooModel(the_date=local_d)


    What would SOME_FUNCTION be?

    Continue reading...

Compartilhe esta Página