1

I have time = '2020-06-24T13:30:00-04:00'. How can I change it to a dateTime object in UTC time. I would prefer not to use pd.Timestamp(time).tz_convert("UTC").to_pydatetime() because it returns a weird output that would look like this datetime.datetime(2020, 6, 24, 17, 30, tzinfo=<UTC>). As a result, when I check for equality with datetime.datetime(2020, 6, 24, 17, 30), it return False.

Edit:

import datetime
import pytz

time = '2020-06-24T13:30:00-04:00

dt = datetime.datetime(2020, 6, 24, 17, 30)
print("dt: ",dt)

so = datetime.datetime.strptime(time, '%Y-%m-%dT%H:%M:%S%z').astimezone(pytz.utc)
print("so:",so)

print(dt == so)

outputs

dt: 2020-06-24 17:30:00
so: 2020-06-24 17:30:00+00:00
False

How can I get it to properly evaluate to True?

0

2 Answers 2

3

#1 Since your string is ISO 8601 compatible, use fromisoformat() on Python 3.7+:

from datetime import datetime, timezone

s = '2020-06-24T13:30:00-04:00'

dtobj = datetime.fromisoformat(s)
# dtobj
# datetime.datetime(2020, 6, 24, 13, 30, tzinfo=datetime.timezone(datetime.timedelta(days=-1, seconds=72000)))

Note that this will give you a timezone-aware datetime object; the tzinfo property is a UTC offset. You can easily convert that to UTC using astimezone():

dtobj_utc = dtobj.astimezone(timezone.utc)
# dtobj_utc
# datetime.datetime(2020, 6, 24, 17, 30, tzinfo=datetime.timezone.utc)

#2 You can achieve the same with strptime (also Python3.7+ according to this):

dtobj = datetime.strptime(s, '%Y-%m-%dT%H:%M:%S%z')
dtobj_utc = dtobj.astimezone(timezone.utc)
# dtobj_utc
# datetime.datetime(2020, 6, 24, 17, 30, tzinfo=datetime.timezone.utc)

#3 If you want to turn the result into a naive datetime object, i.e. remove the tzinfo property, replace with None:

dtobj_utc_naive = dtobj_utc.replace(tzinfo=None)
# dtobj_utc_naive
# datetime.datetime(2020, 6, 24, 17, 30)

#4 For older Python versions, you should be able to use dateutil's parser:

from dateutil import parser
dtobj = parser.parse(s)
dtobj_utc = dtobj.astimezone(timezone.utc)
dtobj_utc_naive = dtobj_utc.replace(tzinfo=None)
# dtobj_utc_naive
# datetime.datetime(2020, 6, 24, 17, 30)
Sign up to request clarification or add additional context in comments.

2 Comments

Are there any benefits to a naive datetime object?
@Danlo9: good question... it depends. In principle, it is better to have a timezone info, unless you know (for sure) that all the datetime objects you work with belong to the same timezone. Also keep in mind that you shouldn't mix up naive and aware datetime objects - use one or the other consistently (to keep you sane).
0

Alright so my previous answer was sort of wack because I did not understand your issue entirely so I am rewriting it. You problem is that you are constructing a datetime object from a string and it is timezone aware(UTC). However, whenever you make a datetime object in python, dt = datetime.datetime(2020, 6, 24, 17, 30), it is creating it but with no timezone information (which you can check using .tzinfo on it). All you would need to do is make dt timezone aware when you first create it. See below my code snippit.


import datetime

time = '2020-06-24T13:30:00-04:00'

dt = datetime.datetime(2020, 6, 24, 17, 30, tzinfo=datetime.timezone.utc)

print("dt: ",dt.tzinfo)

so = datetime.datetime.strptime(time, '%Y-%m-%dT%H:%M:%S%z')

print("so:",so.tzinfo)

print(dt == so)

5 Comments

This doesn't change the time to UTC though... it returns 2020-06-24 13:30:00-04:00 but I need it to be 2020-06-24 17:30:00
I would suggest making what you are comparing it to timezone aware to then compare.
That entire answer was not optimal take a look at the new answer.
now you import pytz, which you also don't need since there is datetime.timezone.utc ;-) Sidenote: datetime.timezone.utc and pytz.utc do not compare equal.
Well that was from previous tests and I did not remove the timezone variable or the pytz import but the code does work properly and the explanation is correct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.