datetime - easy way to make it use local timezone?

B

bwooster47

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")
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top