如何每天在给定的时间执行代码? [英] How to execute code at a given time every day?

查看:61
本文介绍了如何每天在给定的时间执行代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向我的程序中添加一项功能,该功能将在芝加哥期货市场关闭后每天执行代码。这意味着我想在东部时间5:35(市场所在的中央时间4:35)附近运行代码。当然,随着夏时制的发生,这将在EST(UTC -5)和EST(UTC -4)之间来回切换。

I am trying to add a feature to my program that will execute code every day after the closing of the futures markets in Chicago. This means that I want to run code at around 5:35 Eastern Time (4:35 Central Time where the markets are located). This of course will be switching back and forth between EST (UTC -5) and EST (UTC -4) as Daylight Saving Time occurs.

我知道有很多类似的方法问题,但是它们似乎都没有提供我可以使用的解决方案。主要建议似乎是使用Task Scheduler或Quartz,但是,我无法在程序中实现这些功能。我认为最有前途的解决方案是结合使用 TimeZoneInfo DateTime TimeSpan 安排每天的计时器,该计时器将在适当的时间触发。我目前有的解决方案是:

I know there are many similar questions, however none of them seem to offer a solution that I can use. The primary suggestions seem to be to use the Task Scheduler or Quartz, however, I am unable to implement those in my program. I think that the most promising solution would be to use a combination of TimeZoneInfo, DateTime, and TimeSpan to schedule a timer every day that will trigger at the right time. The solution I currently have is this:

    DateTime now = DateTime.Now;
    DateTime currentDateTime = now.DateTime.Date;
    DateTime expiryDateTime = currentDateTime
        .AddHours(17)
        .AddMinutes(35)
        .AddDays(
            now.DateTime.Hour >= 18 + utcOffset 
            || (now.DateTime.Hour == 17 && now.DateTime.Minute >= 35) ? 1 : 0);
    Timer timer = new Timer(
         ...,
         null,
         expiryDateTime - DateTime.Now,
         ...);

我认为这会崩溃,但是,如果我的代码在非时区运行东部时间。我还担心在时区从EST转换为EDT以及反之亦然时发生的23或25个小时内,这将无法正常工作。

I think this this will fall apart, however, if my code is run in a time zone other than Eastern Time. I'm also concerned that this will not behave properly on the 23 or 25 hour days that occur when the time zone switches from EST to EDT and visa versa.

比我目前正在处理调度的方法更好的方法?如何使此代码更健壮,以处理在任何时区运行但始终在东部时间同时执行?

Is there a better way than what I am currently doing to handle the scheduling? How can I make this code more robust to handle being run in any time zone but always execute at the same time in Eastern Time?

编辑:如上所述,任务计划程序和Quartz不是选项。 Quartz已退出,因为我无法包含第三方库。任务计划程序已退出,因为我需要从程序中访问许多内部值。启动另一个应用程序并将这些值暴露给该应用程序比我认为的优点要复杂得多。

As mentioned above, Task Scheduler and Quartz are not options. Quartz is out because I am unable to include third party libraries. Task Scheduler is out because I need access to a lot of internal values from my program. Launching another application and exposing those values to that application adds way more complexity than I think this merits.

推荐答案

我感谢所有其他答案的反馈,并向那些偶然发现这个问题的人指出如果您可以使用> Quartz或Windows Task Scheduler,则确实是更好的选择。但是,我没有那种选择,所以这就是我最终实现每次寻找正确时间并安排计时器运行的方式:

I appreciate the feedback of all the other answers and would like to point out to anybody who has stumbled upon this that Quartz or Windows Task Scheduler are indeed better options if you can use them. I didn't have that option, however, so here is how I ended up implementing finding the right time every time and scheduling the timer to run:

    //This code block is designed to schedule a timer for 5:35pm Eastern Time, regardless of whether is is Daylight Savings (EDT, UTC-4) or not (EST, UTC-5). 
    //  When understanding this code it is important to remember that a DateTime in UTC time represents an ABSOLUTE point in time. Any UTC time can only occur once. 
    //  A date time measured in local time (here we force Eastern Standard) is NOT ABSOLUTE. The same local time can occur twice (2am Nov 3nd 2013 for example)
    //Gets Eastern Timezone to be used in conversions
    TimeZoneInfo easternTimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
    //Grabs the current time (UTC) 
    DateTime utcNowDateTime = DateTime.Now.ToUniversalTime();
    //Gets current time in Eastern Time (could be UTC-4 or -5 depending on time of year)
    DateTime easternNowDateTime = TimeZoneInfo.ConvertTime(utcNowDateTime, easternTimeZoneInfo);
    //Gets todays date
    DateTime easternNowDate = easternNowDateTime.Date;
    //Gets 5:35pm today or tomorrow depending on whether we are past 5:35pm or not. 
    //  Even though there are actually 18 hours from midnight to 5:35pm on Nov 2 2014 and 16 hours from midnight to 5:35pm on March 9 2014, 
    //  this will still end up at 5:35pm on those days because DateTime DOESNOT take into account the movement of the clocks (foreward or backward) when calling 
    //  .AddHours(), etc
    DateTime easternExpiryDateTime = easternNowDate.AddHours(17).AddMinutes(35).AddDays(easternNowDateTime.Hour >= 18 || (easternNowDateTime.Hour == 17 && easternNowDateTime.Minute >= 35) ? 1 : 0);
    //Convert the Easter Time date time to UTC. When subtracting this time from another UTC DateTime you will get the correct TimeSpan for use with a timer
    //  (even on 23 such as March 10th and 25 hour days such as November 3rd 2013)
    DateTime utcExpiryDateTime = easternExpiryDateTime.ToUniversalTime();
    //Set the timer to trigger at the desired point in the future.
    Timer timer = new Timer(
       ...,
       null,
       utcExpiryDateTime - utcNowDateTime ,
       ...);

这篇关于如何每天在给定的时间执行代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆