datetime - easy way to make it use local timezone?

Discussion in 'Python' started by bwooster47@gmail.com, Feb 21, 2007.

  1. Guest

    Is there an easy way to have an automatically constructed local time
    zone tzinfo object be used with datetime?

    Following code shows the problem - the time module works fine mostly
    (%Z works, but %z does not, but that may be the way it is), uses

    # The following python code outputs: note the time() modules print EST
    and EDT (daylight savings),
    # but datetime does not.

    #----------- t = time.time() = 1172170000
    # t tuple = (2007, 2, 22, 13, 46, 40, 3, 53, 0) %Z%z = EST+0000
    # dt tuple = (2007, 2, 22, 13, 46, 40, 3, 53, -1) %Z%z =

    #----------- t = time.time() = 1177340000
    # t tuple = (2007, 4, 23, 10, 53, 20, 0, 113, 1) %Z%z = EDT+0000
    # dt tuple = (2007, 4, 23, 10, 53, 20, 0, 113, -1) %Z%z =

    ---- python code-------------------------
    import time
    from datetime import datetime

    t = 1172170000 # Feb 22

    t_tuple = time.localtime(t)

    dt = datetime.fromtimestamp(t)
    dt_tuple = dt.timetuple()

    print "#-----------", "t = time.time() = ", t
    print "# t tuple = ", t_tuple, " %Z%z = ", time.strftime("%Z%z",
    t_tuple)
    print "# dt tuple = ", dt_tuple, " %Z%z = ", dt.strftime("%Z%z")

    t += 5170000 # Apr 23

    t_tuple = time.localtime(t)

    dt = datetime.fromtimestamp(t)
    dt_tuple = dt.timetuple()

    print "#-----------", "t = time.time() = ", t
    print "# t tuple = ", t_tuple, " %Z%z = ", time.strftime("%Z%z",
    t_tuple)
    print "# dt tuple = ", dt_tuple, " %Z%z = ", dt.strftime("%Z%z")
    , Feb 21, 2007
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christos TZOTZIOY Georgiou
    Replies:
    3
    Views:
    703
    Christos TZOTZIOY Georgiou
    Sep 13, 2003
  2. Tim Peters
    Replies:
    0
    Views:
    529
    Tim Peters
    Sep 9, 2003
  3. axrock
    Replies:
    8
    Views:
    952
    Roedy Green
    Mar 25, 2009
  4. Ami
    Replies:
    5
    Views:
    277
  5. Justin Voelker
    Replies:
    2
    Views:
    263
    Dr J R Stockton
    Dec 2, 2008
Loading...

Share This Page