英文:
How to get the datetime of NYSE close time (4.00 PM EST) in UTC
问题
我有一个后台任务,我想要安排在纽约证券交易所(NYSE)关闭后运行。
我的后台调度程序(redis-rq)要求时间必须是协调世界时(UTC),我正在使用Python。
这个任务在交易日期间安排,所以我不能使用偏移或特定日期,因为它可以动态调用。
所以,我只想要的是一旦触发器被调用,我希望安排任务在当前交易日结束时运行,再次是美国东部时间下午4:00。
我尝试了几个选项,比如使用以下代码获取东部标准时间(EST):
datetime.time(16, 0, tzinfo=pytz.timezone('US/Eastern'))
但我不知道如何将其转换为UTC,我尝试了使用.replace()
等方法,但没有成功。
提前感谢您的帮助。
英文:
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.
答案1
得分: 2
You can use today's date, combine it with the desired New York time, set the time zone to New York (US Eastern), and then convert it to UTC:
from datetime import date, datetime, time
from zoneinfo import ZoneInfo # Python 3.9
t_utc = (
datetime.combine(
date.today(),
time(16,0),
)
.replace(tzinfo=ZoneInfo("America/New_York"))
.astimezone(ZoneInfo("UTC"))
)
print(t_utc)
2023-05-10 20:00:00+00:00
Note on time zone handling:
- With the deprecated
pytz
, you cannot replace the tzinfo. Use, for example,pytz.timezone("America/New_York").localize(...)
instead. - For
zoneinfo
to work properly on Windows, make sure to have thetzdata
package installed and up-to-date.
英文:
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:
from datetime import date, datetime, time
from zoneinfo import ZoneInfo # Python 3.9
t_utc = (
datetime.combine(
date.today(),
time(16,0),
)
.replace(tzinfo=ZoneInfo("America/New_York"))
.astimezone(ZoneInfo("UTC"))
)
print(t_utc)
2023-05-10 20:00:00+00:00
Note on time zone handling:
- with the deprecated
pytz
you cannot replace the tzinfo, use e.g.pytz.timezone("America/New_York").localize(...
instead - for zoneinfo to work properly on Windows, make sure to have
tzdata
package installed and up-to-date
答案2
得分: 0
Try to use pytz library: https://pypi.org/project/pytz/ .
The code with this library can solve your task:
import pytz
from datetime import datetime, time
nyse_close_time_et = time(16, 0)
now_et = datetime.now(pytz.timezone('US/Eastern'))
nyse_close_datetime_et = datetime.combine(now_et.date(), nyse_close_time_et)
nyse_close_datetime_utc = nyse_close_datetime_et.astimezone(pytz.utc)
print(nyse_close_datetime_utc)
英文:
Try to use pytz library: https://pypi.org/project/pytz/ .
The code with this library can solve your task:
import pytz
from datetime import datetime, time
nyse_close_time_et = time(16, 0)
now_et = datetime.now(pytz.timezone('US/Eastern'))
nyse_close_datetime_et = datetime.combine(now_et.date(), nyse_close_time_et)
nyse_close_datetime_utc = nyse_close_datetime_et.astimezone(pytz.utc)
print(nyse_close_datetime_utc)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论