Z
Z
zencd2012-05-22 18:25:26
Python
zencd, 2012-05-22 18:25:26

Parsing datetime to UTC with timezone information

There is a string with a "naive" local date of the form 2008-09-17 14:04:00. It needs to be parsed to UTC datetime with time zone information included. (The end goal is to print the date with a suffix like UTC / UTC+04 separated by %Z, %z).

Here is such a simple code that gives the date 4 hours more (less) than it was originally - everything is correct. But such an object does not contain information about the time zone . At least %Z with %z produces a void.

dt = datetime.strptime("2008-09-17 14:04:00","%Y-%m-%d %H:%M:%S")
utc_struct_time = time.gmtime(time.mktime(dt.timetuple()))
utc_dt = datetime.fromtimestamp(time.mktime(utc_struct_time))
print dt.strftime("%Y-%m-%d %H:%M:%S")


I don’t want to use libraries like pytz - the task is already, consider, solved. And whether they will master such complexity - I still do not know.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
TROODON, 2012-05-22
@zencd

stackoverflow.com/questions/2089706/printing-correct-time-using-timezones-python

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question