473,387 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 4052
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: pduncan | last post by:
Hi all, I need to update client machines with the correct time if it is inaccurate. The server I'm using will always have the correct time so I'll be using that as the source of correct time. ...
1
by: Adam Monsen | last post by:
Say I have the following datetime object: >>> from datetime import datetime >>> d = datetime(2005, 8, 10, 15, 43) I happen to know this is a local time from the Netherlands, so I assume the...
2
by: Samuel | last post by:
Hello, I am trying to convert a local time into UTC ISO8601, then parse it back into local time. I tried the following: ---------------------- #!/usr/bin/python import time import datetime...
2
by: andrew lowe | last post by:
Hi, Please bear with me on this problem, first I'll give you some background: I have an object that contains a DateTime field which i pass to a webservice public class Foo { public DateTime...
9
by: Phil B | last post by:
I am having a problem with a datetime from a web services provider The provider is sending the following SOAP response <?xml version="1.0" encoding="utf-8"?> <soap:Envelope...
5
by: ns21 | last post by:
Our application is windows desktop application. We are using VS.Net 2003, C#, Framework 1.1, SQL 2000. We use webservices to add/update/select objects. We are using XML Serialization. Following is...
3
by: NateM | last post by:
How do I convert any given date into a milliseconds value that represents the number of milliseconds that have passed since January 1, 1970 00:00:00.000 GMT? Is there an easy way to do this like...
9
by: Abhishek | last post by:
Hi I am trying to deserialize/ Parse a datetime object with the below string "2007-05-14T08:00:00.000+02:30" . If i am in GMT + 2.30 time zone everything's fine however if i am in GMT + 5.30 i...
11
by: Keith Hughitt | last post by:
Hi, I am having a little trouble figuring out how to convert a python datetime to UTC. I have a UTC date (e.g. 2008-07-11 00:00:00). I would like to create a UTC date so that when I send it to...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.