471,123 Members | 871 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,123 software developers and data experts.

datetime - easy way to make it use local timezone?

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 '07 #1
0 1019

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Adam Monsen | last post: by
1 post views Thread by James | last post: by
2 posts views Thread by Navin Mishra | last post: by
3 posts views Thread by Mark | last post: by
1 post views Thread by Tino Lange | last post: by
9 posts views Thread by Abhishek | last post: by
7 posts views Thread by Looch | last post: by
2 posts views Thread by Richard Rossel | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.