Answer the question
In order to leave comments, you need to log in
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")
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question