How do I convert a datetime string in local time to a string in UTC time?
I'm sure I've done this before, but can't find it and SO will hopefully help me (and others) do that in future.
Clarification: For example, if I have 2008-09-17 14:02:00
in my local timezone (+10
), I'd like to generate a string with the equivalent UTC
time: 2008-09-17 04:02:00
.
Also, from http://lucumr.pocoo.org/2011/7/15/eppur-si-muove/, note that in general this isn't possible as with DST and other issues there is no unique conversion from local time to UTC time.
if you prefer datetime.datetime:
How about -
if seconds is
None
then it converts the local time to UTC time else converts the passed in time to UTC.in this case ... pytz is best lib
Here's a summary of common Python time conversions.
Some methods drop fractions of seconds, and are marked with (s). An explicit formula such as
ts = (d - epoch) / unit
can be used instead (thanks jfs).calendar.timegm(struct_time)
calendar.timegm(stz.localize(dt, is_dst=None).utctimetuple())
(exception during DST transitions, see comment from jfs)
calendar.timegm(dt.utctimetuple())
calendar.timegm(dt.utctimetuple())
time.gmtime(t)
(see comment from jfs)
stz.localize(dt, is_dst=None).utctimetuple()
(exception during DST transitions, see comment from jfs)
dt.utctimetuple()
dt.utctimetuple()
datetime.fromtimestamp(t, None)
(may fail in certain conditions, see comment from jfs below)
datetime.datetime(struct_time[:6], tzinfo=UTC).astimezone(tz).replace(tzinfo=None)
(can't represent leap seconds, see comment from jfs)
dt.replace(tzinfo=UTC).astimezone(tz).replace(tzinfo=None)
dt.astimezone(tz).replace(tzinfo=None)
datetime.utcfromtimestamp(t)
datetime.datetime(*struct_time[:6])
(can't represent leap seconds, see comment from jfs)
stz.localize(dt, is_dst=None).astimezone(UTC).replace(tzinfo=None)
(exception during DST transitions, see comment from jfs)
dt.astimezone(UTC).replace(tzinfo=None)
datetime.fromtimestamp(t, tz)
(may fail for non-pytz timezones)
datetime.datetime(struct_time[:6], tzinfo=UTC).astimezone(tz)
(can't represent leap seconds, see comment from jfs)
stz.localize(dt, is_dst=None)
(exception during DST transitions, see comment from jfs)
dt.replace(tzinfo=UTC)
Source: taaviburns.ca
The datetime module's utcnow() function can be used to obtain the current UTC time.
As the link mentioned above by Tom: http://lucumr.pocoo.org/2011/7/15/eppur-si-muove/ says:
NOTE - If any of your data is in a region that uses DST, use
pytz
and take a look at John Millikin's answer.If you want to obtain the UTC time from a given string and your lucky enough to be in a region in the world that either doesn't use DST, or you have data that is only offset from UTC without DST applied:
--> using local time as the basis for the offset value:
--> Or, from a known offset, using datetime.timedelta():
If your ready to take on timezone conversions go read this:
https://medium.com/@eleroy/10-things-you-need-to-know-about-date-and-time-in-python-with-datetime-pytz-dateutil-timedelta-309bfbafb3f7
Source: http://feihonghsu.blogspot.com/2008/02/converting-from-local-time-to-utc.html
Example usage from bd808: If your source is a
datetime.datetime
objectt
, call as: