1

I have a script on Heroku, hence I do not know where it resides every time it reboots (-> I cannot manually set the datetime.timedelta), and I need to run a routine at 5pm Rome time. This script has always worked, at least this winter, but now it's starting one hour later, so at 6pm.

import datetime
import pytz


tz = pytz.timezone("Europe/Rome")

now = datetime.datetime.now(tz=tz)
start = datetime.datetime(now.year, now.month, now.day, hour=17, tzinfo=tz)

This answer shows that

using the tzinfo argument of the standard datetime constructors 'does not work' with pytz for many timezones

so I tried refactoring using tz.localize(start), but on the first reboot the system scheduled the routine at 3pm, two hours before. I'm getting very confused: how can I set a specific time in a specific timezone?

FObersteiner
  • 22,500
  • 8
  • 42
  • 72
davide m.
  • 452
  • 4
  • 19
  • Does this answer your question? [How can I force pytz to use currently standard timezones?](https://stackoverflow.com/questions/63359499/how-can-i-force-pytz-to-use-currently-standard-timezones) – FObersteiner Jun 30 '21 at 12:55
  • we have June 2021 - if you can, avoid `pytz`. use [zoneinfo](https://docs.python.org/3/library/zoneinfo.html) (standard lib with Python 3.9) - no such issues there. – FObersteiner Jun 30 '21 at 12:57
  • @MrFuppes upgrading to python 3.9 could be possible, but I'm afraid it would require some attention to other dependencies. for now, I'm considering any other possibility – davide m. Jun 30 '21 at 13:12
  • other possibilities would be using `zoneinfo` via [backports](https://pypi.org/project/backports.zoneinfo/) or using [dateutil](https://dateutil.readthedocs.io/en/stable/tz.html) (which transforms very well into zoneinfo module). Both possible < Python 3.9. – FObersteiner Jun 30 '21 at 13:25

1 Answers1

1

Since I needed to solve this problem quickly, I just accepted the tip @MrFuppes gave me to upgrade the version of Python to 3.9 and since I was using 3.7 this wasn't a big deal, at least for my dependencies.

Using ZoneInfo:

from zoneinfo import ZoneInfo
import datetime


tz = ZoneInfo("Europe/Rome")

now = datetime.datetime.now(tz=tz)
start = datetime.datetime(now.year, now.month, now.day, hour=17, tzinfo=tz)
davide m.
  • 452
  • 4
  • 19