471,354 Members | 2,085 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

converting datetime object in UTC to local time

Hi all,

So a lot of digging on doing this and still not a fabulous solution:

import time

# this takes the last_modified_date naive datetime, converts it to a
# UTC timetuple, converts that to a timestamp (seconds since the
# epoch), subtracts the timezone offset (in seconds), and then
converts
# that back into a timetuple... Must be an easier way...
mytime = time.localtime(time.mktime(last_modified_date.utct imetuple())
- time.timezone)

lm_date_str = time.strftime("%m/%d/%Y %I:%M %p %Z", mytime)

last_modified_date is a naive datetime.datetime object
A previous version gave me something like:

mytime =
datetime.datetime.fromtimestamp(time.mktime(last_m odified_date.utctimetuple())
- time.timezone)

lm_date_str = mytime.strftime("%m/%d/%Y %I:%M %p %Z")

But this gave me no timezone since the datetime object is still
naive. And I'm going from a datetime to a timetuple to a timestamp
back to a datetime...

All this seems like a lot of monkeying around to do something that
should be simple -- is there a simple way to do this without requiring
some other module?

thx

Matt

Jul 4 '07 #1
1 3935
How about subclass datetime.tzinfo? That way you can use asttimezone
to transfer utc to localtime. It requires an aware object though not
naive. A bit more coding, but a lot less converting...

Jim

On Jul 3, 5:16 pm, Matt <m...@vazor.comwrote:
Hi all,

So a lot of digging on doing this and still not a fabulous solution:

import time

# this takes the last_modified_date naivedatetime, converts it to a
# UTC timetuple, converts that to a timestamp (seconds since the
# epoch), subtracts the timezone offset (in seconds), and then
converts
# that back into a timetuple... Must be an easier way...
mytime = time.localtime(time.mktime(last_modified_date.utct imetuple())
- time.timezone)

lm_date_str = time.strftime("%m/%d/%Y %I:%M %p %Z", mytime)

last_modified_date is a naivedatetime.datetimeobject

A previous version gave me something like:

mytime =datetime.datetime.fromtimestamp(time.mktime(last_ modified_date.utctimetuple())
- time.timezone)

lm_date_str = mytime.strftime("%m/%d/%Y %I:%M %p %Z")

But this gave me no timezone since thedatetimeobject is still
naive. And I'm going from adatetimeto a timetuple to a timestamp
back to adatetime...

All this seems like a lot of monkeying around to do something that
should be simple -- is there a simple way to do this without requiring
some other module?

thx

Matt

Jul 6 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by pduncan | last post: by
1 post views Thread by Adam Monsen | last post: by
2 posts views Thread by andrew lowe | last post: by
9 posts views Thread by Phil B | last post: by
3 posts views Thread by NateM | last post: by
9 posts views Thread by Abhishek | last post: by
11 posts views Thread by Keith Hughitt | 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.