[Python] Daylight savings time in Python

Discussão em 'Python' iniciado por Stack, Abril 18, 2025.

  1. Stack

    Stack Membro Participativo

    I am writing a program which deals a lot with timezones and crossing them. The two things I deal with most are creating a datetime object from "now" and then localizing a naive datetime object.

    To create a datetime object from now in the pacific timezone, I am currently doing this (python 2.7.2+)

    from datetime import datetime
    import pytz
    la = pytz.timezone("America/Los_Angeles")
    now = datetime.now(la)


    Is this correct with regards to DST? If not, I suppose I should be doing:

    now2 = la.localize(datetime.now())


    My question is why? Can anyone show me a case where the first is wrong and the seconds is right?

    As for my seconds question, suppose I had a naive date and time from some user input for 9/1/2012 at 8:00am in Los Angeles, CA. Is the right way to make the datetime like this:

    la.localize(datetime(2012, 9, 1, 8, 0))


    If not, how should I be building these datetimes?

    Continue reading...

Compartilhe esta Página