How to convert local time string to UTC?
Asked Answered
F

26

426

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.

Franckot answered 17/9, 2008 at 3:52 Comment(2)
Please be aware the mktime() method take a "local time" as input which may not be what you are expected, I used it and it messed up everything. Please take a glance at this linkChiles
in reverse: How to convert a python utc datetime to a local datetime using only python standard library?Cornstarch
F
71

Thanks @rofly, the full conversion from string to string is as follows:

import time
time.strftime("%Y-%m-%d %H:%M:%S", 
              time.gmtime(time.mktime(time.strptime("2008-09-17 14:04:00", 
                                                    "%Y-%m-%d %H:%M:%S"))))

My summary of the time/calendar functions:

time.strptime
string --> tuple (no timezone applied, so matches string)

time.mktime
local time tuple --> seconds since epoch (always local time)

time.gmtime
seconds since epoch --> tuple in UTC

and

calendar.timegm
tuple in UTC --> seconds since epoch

time.localtime
seconds since epoch --> tuple in local timezone

Franckot answered 17/9, 2008 at 4:15 Comment(3)
(always local time) seems to be wrong: the input to mktime() is local time, the output is seconds since epoch (1970-01-01 00:00:00 +0000 (UTC)) that doesn't depend on timezone.Cornstarch
Also there is 50% chance it fails during DST transition. See Problems with LocaltimeCornstarch
strptime won't return timetuple without .timetuple() method being called explicitlyBrazilin
H
380

First, parse the string into a naive datetime object. This is an instance of datetime.datetime with no attached timezone information. See its documentation.

Use the pytz module, which comes with a full list of time zones + UTC. Figure out what the local timezone is, construct a timezone object from it, and manipulate and attach it to the naive datetime.

Finally, use datetime.astimezone() method to convert the datetime to UTC.

Source code, using local timezone "America/Los_Angeles", for the string "2001-2-3 10:11:12":

from datetime import datetime   
import pytz

local = pytz.timezone("America/Los_Angeles")
naive = datetime.strptime("2001-2-3 10:11:12", "%Y-%m-%d %H:%M:%S")
local_dt = local.localize(naive, is_dst=None)
utc_dt = local_dt.astimezone(pytz.utc)

From there, you can use the strftime() method to format the UTC datetime as needed:

utc_dt.strftime("%Y-%m-%d %H:%M:%S")
Haftarah answered 17/9, 2008 at 4:7 Comment(12)
Please edit your answer with the following code: [code]local.localize(naive)[/code] See the document: link Unfortunately using the tzinfo argument of the standard datetime constructors ‘’does not work’’ with pytz for many timezones. >>> datetime(2002, 10, 27, 12, 0, 0, tzinfo=amsterdam).strftime(fmt) '2002-10-27 12:00:00 AMT+0020'Flyleaf
Apparently the step "figure out what the local timezone is" proves harder than it sounds (practically impossible).Almazan
Use the local.localize as suggested by @SamStoelinga, or else you won't take into account daylight savings time.Prejudge
This answer has been very helpful to me, but I'd like to point out a pitfall I encountered. If you provide the time but not the date, you may get unexpected results. So be sure you provide a full timestamp.Rubefaction
you could use use tzlocal.get_localzone() to get the local timezone as pytz tzinfo objectCornstarch
This works, thanks. Depending on needs, the solution provided by @monkut is less verbose. Also, tzlocal.get_localzone().zone is what provides the string value parameter for pytz.timezone() above. Eg. 'Asia/Tokyo'Shelleyshellfire
@arcseldon: monkut's answer is plain wrong. It doesn't matter how long it is. Read my comment thereCornstarch
Using python-dateutil will save you from strptime misery! ` from dateutil import parser local_dt = parser.parse("2001-2-3 10:11:12") utc_dt = local_dt.astimezone(pytz.utc) `Terris
Note that as from python 3.9 (zoneinfo) or 3.6+ (backports.zoneinfo) can replace pytz for IANA timezoneSthenic
I am unclear if this answer is DST corrected? It appears as if the status of DST is hard coded to None. If so, then how can you tell what the proper value for is_dst ? I am getting dates from a wide range of applications in the format of date, time and timezone ( 01/01/2021 20:00 "America / Los Angeles") I am unsure if this answer is proper since is_dst would change for each date?Jobholder
Downvoted since you don't use the standard librarySilica
pytz is deprecatedHygro
N
248

NOTE -- As of 2020 you should not be using .utcnow() or .utcfromtimestamp(xxx). As you've presumably moved on to python3,you should be using timezone aware datetime objects.

>>> from datetime import timezone
>>> 
>>> # alternative to '.utcnow()'
>>> dt_now = datetime.datetime.now(datetime.timezone.utc)
>>>
>>> # alternative to '.utcfromtimestamp()'
>>> dt_ts = datetime.fromtimestamp(1571595618.0, tz=timezone.utc)

For details see: https://blog.ganssle.io/articles/2019/11/utcnow.html

original answer (from 2010):

The datetime module's utcnow() function can be used to obtain the current UTC time.

>>> import datetime
>>> utc_datetime = datetime.datetime.utcnow()
>>> utc_datetime.strftime("%Y-%m-%d %H:%M:%S")
'2010-02-01 06:59:19'

As the link mentioned above by Tom: http://lucumr.pocoo.org/2011/7/15/eppur-si-muove/ says:

UTC is a timezone without daylight saving time and still a timezone without configuration changes in the past.

Always measure and store time in UTC.

If you need to record where the time was taken, store that separately. Do not store the local time + timezone information!

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 you're 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:

>>> # Obtain the UTC Offset for the current system:
>>> UTC_OFFSET_TIMEDELTA = datetime.datetime.utcnow() - datetime.datetime.now()
>>> local_datetime = datetime.datetime.strptime("2008-09-17 14:04:00", "%Y-%m-%d %H:%M:%S")
>>> result_utc_datetime = local_datetime + UTC_OFFSET_TIMEDELTA
>>> result_utc_datetime.strftime("%Y-%m-%d %H:%M:%S")
'2008-09-17 04:04:00'

--> Or, from a known offset, using datetime.timedelta():

>>> UTC_OFFSET = 10
>>> result_utc_datetime = local_datetime - datetime.timedelta(hours=UTC_OFFSET)
>>> result_utc_datetime.strftime("%Y-%m-%d %H:%M:%S")
'2008-09-17 04:04:00'

UPDATE:

Since python 3.2 datetime.timezone is available. You can generate a timezone aware datetime object with the command below:

import datetime

timezone_aware_dt = datetime.datetime.now(datetime.timezone.utc)

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

Neckerchief answered 1/2, 2010 at 7:0 Comment(10)
That only converts the current time, I need to take any given time (as a string) and convert to UTC.Franckot
I don't think it should matter here, if your using standard offset values. local_time = utc_time + utc_offset AND utc_time = local_time - utc_offset.Neckerchief
It only works with current time because past and future timestamps may have different UTC offset due to DST.Jolley
good point... if you have to deal with DST you should probably be using pytz as mentioned by John Millikin.Neckerchief
There is a sporadic delta between calls to utcnow() and now(). That's a dangerous code line that could lead to strange errors later like tzinfo.utcoffset() must return a whole number of minutes and so on.Huth
I haven't had a problem with it in the past, but you're right. You could always re-cast the delta as: clean_delta = datetime.timedelta(hours=int(UTC_OFFSET_TIMEDELTA.total_seconds() /60.0/60.0)) But keep in mind that there are places like India (UTC+05:30).Neckerchief
1. utc offset for a local timezone can depend on datetime; therefore it might be incorrect to apply the utc offset computed for current time to other moments in future/past e.g., before/after DST transition. 2. there is no such thing as local UTC unless you're talking about computer clock synchronization that is unrelated to the question. "What time is it in UTC" has the same single answer for any place on Earth (around 7pm at the time of the comment).Cornstarch
Another way to get rid of the delta bewteen utcnow() and now() is this: UTC_OFFSET_TIMEDELTA = datetime.utcnow().replace(minute=0, second=0, microsecond=0) - datetime.now().replace(minute=0, second=0, microsecond=0). Note: it might still go wrong occasionally when one time is from before the minute and one after.Parka
Your new answer instantiates dt_now and then doesn't use itJemadar
@PavelVlasov, I noticed that time could change between calls to now utcnow (with bad results). ErikvanOosten, that's a step in the right direction. Here is a good answer, but it has this flaw. I usually take now out if it and use "UTC_OFFSET_TIMEDELTA = datetime.datetime.utcfromtimestamp(0) - datetime.datetime.fromtimestamp(0)". monkut, this is a great answer and deserves to have this issue addressed.Masticate
F
71

Thanks @rofly, the full conversion from string to string is as follows:

import time
time.strftime("%Y-%m-%d %H:%M:%S", 
              time.gmtime(time.mktime(time.strptime("2008-09-17 14:04:00", 
                                                    "%Y-%m-%d %H:%M:%S"))))

My summary of the time/calendar functions:

time.strptime
string --> tuple (no timezone applied, so matches string)

time.mktime
local time tuple --> seconds since epoch (always local time)

time.gmtime
seconds since epoch --> tuple in UTC

and

calendar.timegm
tuple in UTC --> seconds since epoch

time.localtime
seconds since epoch --> tuple in local timezone

Franckot answered 17/9, 2008 at 4:15 Comment(3)
(always local time) seems to be wrong: the input to mktime() is local time, the output is seconds since epoch (1970-01-01 00:00:00 +0000 (UTC)) that doesn't depend on timezone.Cornstarch
Also there is 50% chance it fails during DST transition. See Problems with LocaltimeCornstarch
strptime won't return timetuple without .timetuple() method being called explicitlyBrazilin
T
41

An option available since Python 3.6: datetime.astimezone(tz=None) can be used to get an aware datetime object representing local time (docs). This can then easily be converted to UTC.

from datetime import datetime, timezone
s = "2008-09-17 14:02:00"

# to datetime object:
dt = datetime.fromisoformat(s) # Python 3.7

# I'm on time zone Europe/Berlin; CEST/UTC+2 during summer 2008
dt = dt.astimezone()
print(dt)
# 2008-09-17 14:02:00+02:00

# ...and to UTC:
dtutc = dt.astimezone(timezone.utc)
print(dtutc)
# 2008-09-17 12:02:00+00:00
  • Note: while the described conversion to UTC works perfectly fine, .astimezone() sets tzinfo of the datetime object to a timedelta-derived timezone - so don't expect any "DST-awareness" from it. Be careful with timedelta arithmetic here. Unless you convert to UTC first of course.
  • related: Get local time zone name on Windows (Python 3.9 zoneinfo)
Tutto answered 28/9, 2020 at 7:9 Comment(1)
Since all the other answers are pretty old and this is solution without any external packages (datetime is builtin) this answer deserves more credit.Saltwater
P
40

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).

  • struct_time (UTC) → POSIX (s):
    calendar.timegm(struct_time)
  • Naïve datetime (local) → POSIX (s):
    calendar.timegm(stz.localize(dt, is_dst=None).utctimetuple())
    (exception during DST transitions, see comment from jfs)
  • Naïve datetime (UTC) → POSIX (s):
    calendar.timegm(dt.utctimetuple())
  • Aware datetime → POSIX (s):
    calendar.timegm(dt.utctimetuple())
  • POSIX → struct_time (UTC, s):
    time.gmtime(t)
    (see comment from jfs)
  • Naïve datetime (local) → struct_time (UTC, s):
    stz.localize(dt, is_dst=None).utctimetuple()
    (exception during DST transitions, see comment from jfs)
  • Naïve datetime (UTC) → struct_time (UTC, s):
    dt.utctimetuple()
  • Aware datetime → struct_time (UTC, s):
    dt.utctimetuple()
  • POSIX → Naïve datetime (local):
    datetime.fromtimestamp(t, None)
    (may fail in certain conditions, see comment from jfs below)
  • struct_time (UTC) → Naïve datetime (local, s):
    datetime.datetime(struct_time[:6], tzinfo=UTC).astimezone(tz).replace(tzinfo=None)
    (can't represent leap seconds, see comment from jfs)
  • Naïve datetime (UTC) → Naïve datetime (local):
    dt.replace(tzinfo=UTC).astimezone(tz).replace(tzinfo=None)
  • Aware datetime → Naïve datetime (local):
    dt.astimezone(tz).replace(tzinfo=None)
  • POSIX → Naïve datetime (UTC):
    datetime.utcfromtimestamp(t)
  • struct_time (UTC) → Naïve datetime (UTC, s):
    datetime.datetime(*struct_time[:6])
    (can't represent leap seconds, see comment from jfs)
  • Naïve datetime (local) → Naïve datetime (UTC):
    stz.localize(dt, is_dst=None).astimezone(UTC).replace(tzinfo=None)
    (exception during DST transitions, see comment from jfs)
  • Aware datetime → Naïve datetime (UTC):
    dt.astimezone(UTC).replace(tzinfo=None)
  • POSIX → Aware datetime:
    datetime.fromtimestamp(t, tz)
    (may fail for non-pytz timezones)
  • struct_time (UTC) → Aware datetime (s):
    datetime.datetime(struct_time[:6], tzinfo=UTC).astimezone(tz)
    (can't represent leap seconds, see comment from jfs)
  • Naïve datetime (local) → Aware datetime:
    stz.localize(dt, is_dst=None)
    (exception during DST transitions, see comment from jfs)
  • Naïve datetime (UTC) → Aware datetime:
    dt.replace(tzinfo=UTC)

Source: taaviburns.ca

Preengage answered 26/10, 2012 at 9:38 Comment(6)
(1) fromtimestamp(t, None) may fail if the local timezone had different utc offset at t and C library has no access to the tz database on the given platform. You could use tzlocal.get_localzone() to provide tzdata in a portable way. (2) fromtimestamp(t, tz) may fail for non-pytz timezones. (3) datetime(*struct_time[:6]) you are missing *. (4) timegm(), utctimetuple(), struct_time -based solutions drop fractions of a second. You could use an explicit formula such as: ts = (d - epoch) / unit instead.Cornstarch
(5) stz.localize(dt, is_dst=None) raises an exception for ambiguous or non-existent local times e.g., during DST transitions. To avoid the exception, use stz.normalize(stz.localize(dt)) that may return imprecise results. (6) datetime() can't represent a leap second. Convert struct_time to "seconds since epoch" as a workaround first. (7) time.gmtime(t) unlike calendar.timegm() may expect non-POSIX input if "right" timezone is used. Use the explicit formula if the input is POSIX instead: gmtime = lambda t: datetime(1970,1,1, tzinfo=utc) + timedelta(seconds=t)Cornstarch
i wish this was in a table, easier to read (it is in the link)Mcsweeney
@MichaelChirico, this answer states that "We do not (and will not) allow <table> tags. Sorry. This is intentional and by design. If you need a quick and dirty "table", use <pre> and ASCII layout." I don't believe an ASCII table would be more readable than the list above.Preengage
well that's unfortunate. you have my upvote anyway... maybe reference the table in the link within your answer?Mcsweeney
@MichaelChirico, the answer already contains a link to the original table by Taavi Burns.Preengage
G
26

I'm having good luck with dateutil (which is widely recommended on SO for other related questions):

from datetime import *
from dateutil import *
from dateutil.tz import *

# METHOD 1: Hardcode zones:
utc_zone = tz.gettz('UTC')
local_zone = tz.gettz('America/Chicago')
# METHOD 2: Auto-detect zones:
utc_zone = tz.tzutc()
local_zone = tz.tzlocal()

# Convert time string to datetime
local_time = datetime.strptime("2008-09-17 14:02:00", '%Y-%m-%d %H:%M:%S')

# Tell the datetime object that it's in local time zone since 
# datetime objects are 'naive' by default
local_time = local_time.replace(tzinfo=local_zone)
# Convert time to UTC
utc_time = local_time.astimezone(utc_zone)
# Generate UTC time string
utc_string = utc_time.strftime('%Y-%m-%d %H:%M:%S')

(Code was derived from this answer to Convert UTC datetime string to local datetime)

Gentlewoman answered 19/12, 2011 at 14:58 Comment(4)
dateutil can fail for past dates if local timezone had different utc offset then (unrelated to DST transitions) on systems where the implementation doesn't use a historical timezone database (notably Windows). pytz + tzlocal could be used instead.Cornstarch
@JFSebastian- Hadn't heard of that- where can we get more info?Gentlewoman
take Windows machine, set any timezone that had different utc offset in the past e.g., Europe/Moscow. Compare results with pytz. Plus, you could test this bug that fails even on Ubuntu though I'm not sure about the correct API usage in this case.Cornstarch
What's the output? Please post the output.Metencephalon
M
24
def local_to_utc(t):
    secs = time.mktime(t)
    return time.gmtime(secs)

def utc_to_local(t):
    secs = calendar.timegm(t)
    return time.localtime(secs)

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 object t, call as:

local_to_utc(t.timetuple())
Mythologize answered 17/9, 2008 at 3:54 Comment(3)
If your source is a datetime.datetime object t, call as: local_to_utc(t.timetuple())Ammamaria
.timetuple() call sets tm_isdst to -1; there is 50% chance mktime() fails during DST transition.Cornstarch
The solution by Chuck loses millisecond information.Turnstile
S
20

One more example with pytz, but includes localize(), which saved my day.

import pytz, datetime
utc = pytz.utc
fmt = '%Y-%m-%d %H:%M:%S'
amsterdam = pytz.timezone('Europe/Amsterdam')

dt = datetime.datetime.strptime("2012-04-06 10:00:00", fmt)
am_dt = amsterdam.localize(dt)
print am_dt.astimezone(utc).strftime(fmt)
'2012-04-06 08:00:00'
Schafer answered 6/4, 2012 at 7:54 Comment(0)
C
12

I've had the most success with python-dateutil:

from dateutil import tz

def datetime_to_utc(date):
    """Returns date in UTC w/o tzinfo"""
    return date.astimezone(tz.gettz('UTC')).replace(tzinfo=None) if date.tzinfo else date
Costermonger answered 29/8, 2012 at 21:40 Comment(1)
dateutil can fail for past datesCornstarch
D
8
import time

import datetime

def Local2UTC(LocalTime):

    EpochSecond = time.mktime(LocalTime.timetuple())
    utcTime = datetime.datetime.utcfromtimestamp(EpochSecond)

    return utcTime

>>> LocalTime = datetime.datetime.now()

>>> UTCTime = Local2UTC(LocalTime)

>>> LocalTime.ctime()

'Thu Feb  3 22:33:46 2011'

>>> UTCTime.ctime()

'Fri Feb  4 05:33:46 2011'
Deface answered 4/2, 2011 at 5:38 Comment(1)
mktime(dt.timetuple()) can fail during DST transition. There is LocalTimezone in the docs (it works for the most recent timezone definition but can fail for past dates (unrelated to DST))Cornstarch
L
7

Here's an example with the native zoneinfo module in Python3.9:

from datetime import datetime
from zoneinfo import ZoneInfo

# Get timezone we're trying to convert from
local_tz = ZoneInfo("America/New_York")
# UTC timezone
utc_tz = ZoneInfo("UTC")

dt = datetime.strptime("2021-09-20 17:20:00","%Y-%m-%d %H:%M:%S")
dt = dt.replace(tzinfo=local_tz)
dt_utc = dt.astimezone(utc_tz)

print(dt.strftime("%Y-%m-%d %H:%M:%S"))
print(dt_utc.strftime("%Y-%m-%d %H:%M:%S"))

This may be preferred over just using dt.astimezone() in situations where the timezone you're converting from isn't reflective of your system's local timezone. Not having to rely on external libraries is nice too.

Note: This may not work on Windows systems, since zoneinfo relies on an IANA database that may not be present. The tzdata package can be installed as a workaround. It's a first-party package, but is not in the standard library.

Loomis answered 20/9, 2021 at 21:39 Comment(2)
Good modern answer, I hope this can rise to the top one day.Actinoid
To be clear: In my case this applied a DST conversion method, when given a date in summer. I set the timezone to "Europe/Amsterdam", and 04:00 Amsterdam became 02:00 in the output, so it adjusted for DST in summer.Desex
S
6

if you prefer datetime.datetime:

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")
Smythe answered 27/2, 2010 at 16:12 Comment(3)
Sure, but I can't see why you'd prefer that. It requires an extra import and 3 more function calls than my version...Franckot
%Z and %z prints white spaces still.Huth
it is incorrect. It fails if the local timezone is not UTC. mktime() expects local time as the input. fromtimestamp() returns local time, not utc. If you fix it then see these additional issues (like dateutil, stdlib-only solution may fail)Cornstarch
R
6

Simple

I did it like this:

>>> utc_delta = datetime.utcnow()-datetime.now()
>>> utc_time = datetime(2008, 9, 17, 14, 2, 0) + utc_delta
>>> print(utc_time)
2008-09-17 19:01:59.999996

Fancy Implementation

If you want to get fancy, you can turn this into a functor:

class to_utc():
    utc_delta = datetime.utcnow() - datetime.now()

    def __call__(cls, t):
        return t + cls.utc_delta

Result:

>>> utc_converter = to_utc()
>>> print(utc_converter(datetime(2008, 9, 17, 14, 2, 0)))
2008-09-17 19:01:59.999996
Remount answered 2/5, 2018 at 15:50 Comment(2)
this doesn't work for daylight savings time - e.g. if it's currently summer and the date you're converting is in winter. and the question was about converting dates stored as strings...Franckot
FYI. The biggest problem with this method (besides daylight savings) is the few milliseconds the delta will be off by. All my calendar invites are showing up as off by 1 minute.Underrate
M
4

How about -

time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime(seconds))

if seconds is None then it converts the local time to UTC time else converts the passed in time to UTC.

Mathematician answered 23/9, 2009 at 6:28 Comment(0)
S
4

You can do it with:

>>> from time import strftime, gmtime, localtime
>>> strftime('%H:%M:%S', gmtime()) #UTC time
>>> strftime('%H:%M:%S', localtime()) # localtime
Sickler answered 21/8, 2012 at 16:30 Comment(0)
M
3

In python 3.9.0, after you've parsed your local time local_time into datetime.datetime object, just use local_time.astimezone(datetime.timezone.utc).

Madisonmadlen answered 31/10, 2020 at 20:23 Comment(1)
this answer would be better if you showed how to parse the local time into a datetime objectMazuma
T
2

For getting around day-light saving, etc.

None of the above answers particularly helped me. The code below works for GMT.

def get_utc_from_local(date_time, local_tz=None):
    assert date_time.__class__.__name__ == 'datetime'
    if local_tz is None:
        local_tz = pytz.timezone(settings.TIME_ZONE) # Django eg, "Europe/London"
    local_time = local_tz.normalize(local_tz.localize(date_time))
    return local_time.astimezone(pytz.utc)

import pytz
from datetime import datetime

summer_11_am = datetime(2011, 7, 1, 11)
get_utc_from_local(summer_11_am)
>>>datetime.datetime(2011, 7, 1, 10, 0, tzinfo=<UTC>)

winter_11_am = datetime(2011, 11, 11, 11)
get_utc_from_local(winter_11_am)
>>>datetime.datetime(2011, 11, 11, 11, 0, tzinfo=<UTC>)
Thersathersites answered 9/11, 2011 at 16:59 Comment(0)
L
2

Using http://crsmithdev.com/arrow/

arrowObj = arrow.Arrow.strptime('2017-02-20 10:00:00', '%Y-%m-%d %H:%M:%S' , 'US/Eastern')

arrowObj.to('UTC') or arrowObj.to('local') 

This library makes life easy :)

Lid answered 20/2, 2017 at 15:32 Comment(1)
arrow is awesome: arrow.get(datetime.now(), 'local').to('utc').naiveAutoclave
H
2

I have this code in one of my projects:

from datetime import datetime
## datetime.timezone works in newer versions of python
try:
    from datetime import timezone
    utc_tz = timezone.utc
except:
    import pytz
    utc_tz = pytz.utc

def _to_utc_date_string(ts):
    # type (Union[date,datetime]]) -> str
    """coerce datetimes to UTC (assume localtime if nothing is given)"""
    if (isinstance(ts, datetime)):
        try:
            ## in python 3.6 and higher, ts.astimezone() will assume a
            ## naive timestamp is localtime (and so do we)
            ts = ts.astimezone(utc_tz)
        except:
            ## in python 2.7 and 3.5, ts.astimezone() will fail on
            ## naive timestamps, but we'd like to assume they are
            ## localtime
            import tzlocal
            ts = tzlocal.get_localzone().localize(ts).astimezone(utc_tz)
    return ts.strftime("%Y%m%dT%H%M%SZ")
Hygro answered 6/6, 2020 at 20:25 Comment(0)
M
1

I found the best answer on another question here. It only uses python built-in libraries and does not require you to input your local timezone (a requirement in my case)

import time
import calendar

local_time = time.strptime("2018-12-13T09:32:00.000", "%Y-%m-%dT%H:%M:%S.%f")
local_seconds = time.mktime(local_time)
utc_time = time.gmtime(local_seconds)

I'm reposting the answer here since this question pops up in google instead of the linked question depending on the search keywords.

Mcleod answered 13/12, 2018 at 10:53 Comment(0)
R
1

For anyone who is confused with the most upvoted answer. You can convert a datetime string to utc time in python by generating a datetime object and then you can use astimezone(pytz.utc) to get datetime in utc.

For eg.

let say we have local datetime string as 2021-09-02T19:02:00Z in isoformat

Now to convert this string to utc datetime. we first need to generate datetime object using this string by

dt = datetime.strptime(dt,'%Y-%m-%dT%H:%M:%SZ')

this will give you python datetime object, then you can use astimezone(pytz.utc) to get utc datetime like

dt = datetime.strptime(dt,'%Y-%m-%dT%H:%M:%SZ') dt = dt.astimezone(pytz.utc)

this will give you datetime object in utc, then you can convert it to string using dt.strftime("%Y-%m-%d %H:%M:%S")

full code eg:

from datetime import datetime
import pytz

def converLocalToUTC(datetime, getString=True, format="%Y-%m-%d %H:%M:%S"):
    dt = datetime.strptime(dt,'%Y-%m-%dT%H:%M:%SZ')
    dt = dt.astimezone(pytz.utc)
    
    if getString:
        return dt.strftime(format)
    return dt

then you can call it as

converLocalToUTC("2021-09-02T19:02:00Z")

took help from https://mcmap.net/q/25350/-how-to-convert-local-time-string-to-utc

Reglet answered 2/9, 2021 at 13:45 Comment(0)
W
1

The default datetime object will have no timezone set up (tzinfo property will be None), so after parsing the string into the datetime object, the timezone should be convert to UTC:

from datetime import datetime, timezone

def convert_local_time_to_utc(dt: str, format="%Y-%m-%d %H:%M:%S.%f"):
    dt = datetime.strptime(dt, format).astimezone(tz=timezone.utc)
    return dt

local_datetime_string = "2021-01-01 01:23:45.678910"
utc_datetime = convert_local_time_to_utc(dt=local_datetime_string)
print(utc_datetime.strftime("%Y-%m-%d %H:%M:%S.%f"))
# 2020-12-31 19:23:45.678910
Waw answered 29/5, 2023 at 12:24 Comment(0)
F
0

If you already have a datetime object my_dt you can change it to UTC with:

datetime.datetime.utcfromtimestamp(my_dt.timestamp())
Fascinate answered 9/7, 2020 at 14:17 Comment(0)
S
0

Briefly, to convert any datetime date to UTC time:

from datetime import datetime

def to_utc(date):
    return datetime(*date.utctimetuple()[:6])

Let's explain with an example. First, we need to create a datetime from the string:

>>> date = datetime.strptime("11 Feb 2011 17:33:54 -0800", "%d %b %Y %H:%M:%S %z")

Then, we can call the function:

>>> to_utc(date)
datetime.datetime(2011, 2, 12, 1, 33, 54)

Step by step how the function works:

>>> date.utctimetuple()
time.struct_time(tm_year=2011, tm_mon=2, tm_mday=12, tm_hour=1, tm_min=33, tm_sec=54, tm_wday=5, tm_yday=43, tm_isdst=0)
>>> date.utctimetuple()[:6]
(2011, 2, 12, 1, 33, 54)
>>> datetime(*date.utctimetuple()[:6])
datetime.datetime(2011, 2, 12, 1, 33, 54)
Sabrinasabsay answered 10/7, 2020 at 18:28 Comment(0)
P
-1

In python3:

pip install python-dateutil

from dateutil.parser import tz

mydt.astimezone(tz.gettz('UTC')).replace(tzinfo=None) 
Persuade answered 11/1, 2018 at 9:3 Comment(2)
this doesn't seem to work, it raises a ValueError exception: ValueError: astimezone() cannot be applied to a naive datetimeConjuration
It should be: from dateutil import tz Fonsie
B
-2

How about -

time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime(seconds))

if seconds is None then it converts the local time to UTC time else converts the passed in time to UTC.

Burro answered 6/11, 2010 at 16:2 Comment(1)
This doesn't help, the question was to convert a given local time string to a utc string.Franckot

© 2022 - 2025 — McMap. All rights reserved.