How to get the datetime of NYSE close time (4.00 PM EST) in UTC

huangapple go评论58阅读模式
英文:

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 the tzdata 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)

huangapple
  • 本文由 发表于 2023年5月10日 21:31:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76219067.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定