converting datetime object in UTC to local time

Discussion in 'Python' started by Matt, Jul 4, 2007.

  1. Matt

    Matt Guest

    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.utctimetuple())
    - 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_modified_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
    Matt, Jul 4, 2007
    #1
    1. Advertising

  2. Matt

    i3dmaster Guest

    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 <> 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.utctimetuple())
    > - 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
    i3dmaster, Jul 6, 2007
    #2
    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. =?Utf-8?B?aGVuaw==?=

    Covert UTC String to UTC Datetime

    =?Utf-8?B?aGVuaw==?=, May 7, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    18,710
    Egbert Nierop \(MVP for IIS\)
    May 7, 2006
  2. Christos TZOTZIOY Georgiou
    Replies:
    3
    Views:
    691
    Christos TZOTZIOY Georgiou
    Sep 13, 2003
  3. Tim Peters
    Replies:
    0
    Views:
    524
    Tim Peters
    Sep 9, 2003
  4. Replies:
    1
    Views:
    777
    Paul Boddie
    Mar 11, 2007
  5. Jonathan Wood
    Replies:
    1
    Views:
    2,184
    Jonathan Wood
    Nov 1, 2009
Loading...

Share This Page