Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > converting datetime object in UTC to local time

Reply
Thread Tools

converting datetime object in UTC to local time

 
 
Matt
Guest
Posts: n/a
 
      07-04-2007
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

 
Reply With Quote
 
 
 
 
i3dmaster
Guest
Posts: n/a
 
      07-06-2007
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.com> wrote:
> 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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert UTC DateTime to Local Time (Not My Local Time) Jonathan Wood ASP .Net 1 11-01-2009 12:08 AM
convert time string in UTC to time in local time davelist@mac.com Python 1 03-11-2007 12:57 AM
Covert UTC String to UTC Datetime =?Utf-8?B?aGVuaw==?= ASP .Net 3 05-07-2006 06:25 PM
datetime: .datetime-.datetime = .timedelta, .time-.time=TypeError ? Christos TZOTZIOY Georgiou Python 3 09-13-2003 10:44 AM
RE: datetime: .datetime-.datetime = .timedelta, .time-.time=TypeError ? Tim Peters Python 0 09-09-2003 12:57 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57