My function takes a UTC datetime string as a parameter, converts it to a datetime object which I then need to offset to EST(GMT -4:00). For example, passing “2019-07-01T00:00:00Z” to the function should return “06/30/2019 08:00:00”.
Here's how I implemented the subclass for 'tzinfo' and its standard methods.
from datetime import datetime, tzinfo, timedelta
class EST(tzinfo):
def tzname(self, dt):
return "US/Eastern"
def utcoffset(self, dt):
return timedelta(hours=-4) + self.dst(dt)
def dst(self, dt):
return timedelta(0)
TEST_UTC = "2019-07-01T00:00:00Z"
dt_object = datetime.strptime(TEST_UTC, "%Y-%m-%dT%H:%M:%SZ") # String to datetime object -> 2019-07-01 00:00:00
print(dt_object.astimezone(EST()))
The expected output for the last print statement is 2019-06-30 20:00:00-04:00 but the program returns 2019-06-30 14:30:00-04:00. It shows the expected result only if I set the values in the 'timedelta' of the 'utcoffset' function to timedelta(hours=1, minutes=30)
.
Please note I want to implement this using 'datetime' library only.