Given a TZ-aware datetime
, I want to find the datetime of the same time on the previous day in the same timezone (not necessarily same offset).
I came up with the solution below.
tz = pytz.timezone("Europe/Paris")
myDatetime = tz.localize(datetime.datetime(year=2019, month=10, day=27, hour=22))
print(myDatetime) # 2019-10-27 22:00:00+01:00
# separate into date and time
mydate = myDatetime.date()
time = myDatetime.time()
# find previous day
previous_date = mydate - datetime.timedelta(days=1)
print(previous_date) # 2019-10-26
previous_day = tz.localize(datetime.datetime.combine(date=previous_date, time=time))
print(previous_day) # 2019-10-26 22:00:00+02:00
Is there a simpler, better tested, more standard way of doing the same?
Library?