I have a background task that I want to schedule to run once the NYSE is closed.
My background scheduler (redis-rq) requires the time will be in UTC and I’m using python.
The task is scheduled during the trading day, so it means that I can’t use offset or specific dates because it can be called dynamically.
So, what I just want is that once a trigger is called, I want to schedule the task to run at the end of the current trading day, which again is 16:00 EST time.
I’ve tried several options like getting the time in EST with:
datetime.time(16, 0, tzinfo=pytz.timezone('US/Eastern'))
But I can’t figure out how to convert it to UTC, I tried stuff like .replace() with no success.
Thanks in advance.
2
Answers
You can use today’s date, combine with the desired NY time, set the time zone to New York (US Eastern), then convert to UTC:
Note on time zone handling:
pytz
you cannot replace the tzinfo, use e.g.pytz.timezone("America/New_York").localize(...
insteadtzdata
package installed and up-to-dateTry to use pytz library: https://pypi.org/project/pytz/ .
The code with this library can solve your task: