Parse Datetime with +0 timezone - python

I have the following Datetime string: Dec 03 2020 01: +0 which I want to parse into a datetime object.
dtObj = datetime.strptime("Dec 03 2020 01: +0", '%b %d %Y %I: %z')
Checking the Documentation, this should work but I get the following error:
ValueError: time data 'Dec 03 2020 01: +0' does not match format '%b %d %Y %I: %z'
Any ideas what I have overseen?
Thanks in advance

Any ideas what I have overseen?
strftime.org claims that %z
UTC offset in the form ±HHMM[SS[.ffffff]] (empty string if the object
is naive).
this mean that it must contain at least 4 digits after + or - (HHMM part, which is compulsory), taking this is account Dec 03 2020 01: +0 is not compliant with used format string, whilst Dec 03 2020 01: +0000 is
import datetime
dtObj = datetime.datetime.strptime("Dec 03 2020 01: +0000", '%b %d %Y %I: %z')
print(dtObj)
gives output
2020-12-03 01:00:00+00:00

Related

Converting epoch to human-readable date doesn't work in python

I am using time module to convert epoch into human readable date using the code below.
import time
datetime = time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.localtime(1609740000000))
print(datetime)
>>> Thu, 17 Aug 52980 20:00:00 +0000
The output is incorrect when I check it on https://www.epochconverter.com
Correct output should be Wed, 04 Aug 2021 21:49:24 +0000
time.localtime takes time in seconds. You presumably pass time in milliseconds.
datetime = time.strftime("%a, %d %b %Y %H:%M:%S +0000",
time.localtime(1609740000000 // 1000))
#'Mon, 04 Jan 2021 01:00:00 +0000'
The answer from epochconverter.com is the same. Your "correct output" is incorrect.

How to parse Fri, 25 Jun 2021 12:06:16 +0000 (UTC) to datetime object?

If there was no (UTC), it would be:
%a, %d %b %Y %X %z
but server is sending with additional (UTC) or (CDT) how do you parse it?
%a, %d %b %Y %X %z (%Z) doesn't work.
How does it not work? Which version of python are you using?
I just ran the following in python 3.9:
datetime.datetime.strptime("Fri, 25 Jun 2021 12:06:16 +0000 (UTC)", "%a, %d %b %Y %X %z (%Z)")
Which created this datetime object:
datetime.datetime(2021, 6, 25, 12, 6, 16, tzinfo=datetime.timezone(datetime.timedelta(0), 'UTC'))

I am getting the error while giving the timezone as IST.Why?

I am getting the error while giving the time zone as IST but when it is changed to GMT it is working can I know why it is happening and one more doubt can we compare two different time zones.Please check the code here which i have written.
from datetime import datetime
s1 = 'Oct 06 09:42:21 IST 2020'
d1 = datetime.strptime(s1, '%b %d %H:%M:%S %Z %Y')
print(d1)
s2 = 'Oct 06 09:42:26 2020 IST'
d2 = datetime.strptime(s2, '%b %d %H:%M:%S %Y %Z')
print(d2)
print(d1 < d2)
One of the other way to acheive the same is
from datetime import datetime
s1 = 'Oct 06 09:42:21 2020 +0530'
d1 = datetime.strptime(s1, '%b %d %H:%M:%S %Y %z')
print(d1)
print(d1.tzinfo)
s2 = 'Oct 06 09:42:26 2020 +0530'
d2 = datetime.strptime(s2, '%b %d %H:%M:%S %Y %z')
print(d2)
print(d2.tzinfo)
print(d1 < d2)
Output
2020-10-06 09:42:21+05:30
UTC+05:30
2020-10-06 09:42:26+05:30
UTC+05:30
True
The way you are trying to acheive this is not possible in my opinion, Here is a bug raised for the same

Python date time parsing

Why is this failing:
datetime.datetime.strptime(
date_string, ' %A %d %B %Y : %I:%M %p')
ValueError("time data ' Tuesday 08 September 2020 : 00:07 AM' does not match format ' %A %d %B %Y : %I:%M %p'")
when this works:
datetime.datetime.strptime(' Wednesday 02 September 2020 : 2:54 AM', ' %A %d %B %Y : %I:%M %p')
I think it must have to do with the 00 hours, but what it is exactly I do not know.
From datetime docs:
%H Hour (24-hour clock) as a zero-padded decimal number. 00, 01, …, 23
%I Hour (12-hour clock) as a zero-padded decimal number. 01, 02, …, 12
So you would need input like 12:07 am for the time to be valid (there is no 0 o'clock in a 12 hour clock)
>> datetime.datetime.strptime(' Tuesday 01 September 2020 : 12:07 AM', ' %A %d %B %Y : %I:%M %p')
datetime.datetime(2020, 9, 1, 0, 7)
Or use %H for 24 hour clock, in which case you'd likely want to drop the %p since it's meaningless in this case
>> datetime.datetime.strptime(' Tuesday 01 September 2020 : 00:07', ' %A %d %B %Y : %H:%M')
datetime.datetime(2020, 9, 1, 0, 7)

Python Code to convert Wed, 06 Feb 2019 20:47:46 GMT to 2019-02-06 15:47:46 EST or EDT

I am trying to convert GMT time to EST/EDT format in Python.
GMT Format: Wed, 06 Feb 2019 20:47:46 GMT
Required Format: 2019-02-06 15:47:46 EST (when daylight time starts it should be EDT)
I need this without doing any additional pip install.
Currently, I am using below code which is giving EST time as : 2019-02-06 15:47:46-05:00
import datetime
from datetime import datetime
from dateutil import tz
enable_date = response["ResponseMetadata"]["HTTPHeaders"]["date"]
print "Enable Date in GMT: {0}".format(enable_date)
from_zone = tz.gettz('UTC')
to_zone = tz.gettz('US/Eastern')
utc = datetime.strptime(enable_date, '%a, %d %b %Y %H:%M:%S GMT')
utc = utc.replace(tzinfo=from_zone)
central = utc.astimezone(to_zone)
print "Enable Date in EST: {0}".format(central)
output:
Enable Date in GMT: Wed, 06 Feb 2019 20:47:46 GMT
Enable Date in EST: 2019-02-06 15:47:46-05:00
desired output:
Enable Date in EST: 2019-02-06 15:47:46 EST
Variable enable_date has value Wed, 06 Feb 2019 20:47:46 GMT
gmt_date = "Thu, 07 Feb 2019 15:33:28 GMT"
def date_convert():
print "Date in GMT: {0}".format(gmt_date)
# Hardcode from and to time zones
from_zone = tz.gettz('GMT')
to_zone = tz.gettz('US/Eastern')
# gmt = datetime.gmtnow()
gmt = datetime.strptime(gmt_date, '%a, %d %b %Y %H:%M:%S GMT')
# Tell the datetime object that it's in GMT time zone
gmt = gmt.replace(tzinfo=from_zone)
# Convert time zone
eastern_time = str(gmt.astimezone(to_zone))
# Check if its EST or EDT
if eastern_time[-6:] == "-05:00":
print "Date in US/Eastern: " +eastern_time.replace("-05:00"," EST")
elif eastern_time[-6:] == "-04:00":
print "Date in US/Eastern: " +eastern_time.replace("-04:00"," EDT")
return
Last line should be
print "Enable Date in EST: {0}".format(central.strftime('%Y-%m-%d %H:%M:%S %Z'))

Categories

Resources