.net中的日期时间和偏移量

时间:2012-05-04 15:46:57

标签: .net datetime quartz.net datetimeoffset quartz.net-2.0

如何安排后台工作在哥伦比亚时间每天晚上9点开始?我正在使用quartz.net

public class Program
    {
        static void Main(string[] args)
        {
            // construct a scheduler

            var schedulerFactory = new StdSchedulerFactory();
            var scheduler = schedulerFactory.GetScheduler();
            scheduler.Start();


            TimeZoneInfo colombianTimezone = TimeZoneInfo.FindSystemTimeZoneById("SA Pacific Standard Time");     
            var colombianTimeNow = TimeZoneInfo.ConvertTime(DateTime.Now, TimeZoneInfo.Local,
                                                            colombianTimezone);    

            var job = JobBuilder.Create<EmailsJob>().Build();
            var now = DateTime.Now;

            var trigger = TriggerBuilder.Create()
                            .StartAt(//INeedThisToStartEverydayat9pmColombianTime)
                            .WithSimpleSchedule(x => x.WithIntervalInHours(24).RepeatForever())
                            .Build();

            scheduler.ScheduleJob(job, trigger);
        }
    }

StartAt方法采用DateTimeOffset对象。

请帮忙

2 个答案:

答案 0 :(得分:2)

基于Cron的调度可能是满足您需求的更好选择。

TimeZoneInfo colombianTimezone = TimeZoneInfo.FindSystemTimeZoneById("SA Pacific Standard Time");     

var job = JobBuilder.Create<EmailsJob>().Build();

var trigger = TriggerBuilder.Create()
                .WithCronSchedule("0 0 21 * * ?", x => x.InTimeZone(colombianTimezone))
                .Build();

scheduler.ScheduleJob(job, trigger);

答案 1 :(得分:1)

这应该为今天晚上9点提供DateTimeOffset,转换为UniversalTime传递给quartz。

TimeZoneInfo colombianTimezone = TimeZoneInfo.FindSystemTimeZoneById("SA Pacific Standard Time");
DateTime columbianTime9pm = TimeZoneInfo.ConvertTime(DateTime.Today.AddHours(21), TimeZoneInfo.Local,
                                                colombianTimezone);
DateTimeOffset startAt = new DateTimeOffset(columbianTime9pm).ToUniversalTime();

我还没有测试过,但我认为这也会定义你想要的触发器。

TimeZoneInfo colombianTimezone = TimeZoneInfo.FindSystemTimeZoneById("SA Pacific Standard Time");
DateTime startTime = TimeZoneInfo.ConvertTime(DateTime.Today.AddHours(21), TimeZoneInfo.Local,
                                                colombianTimezone);
SimpleTrigger trigger = new SimpleTrigger("myTrigger",
                                            null,
                                            startTime,
                                            null,
                                            SimpleTrigger.RepeatIndefinitely,
                                            TimeSpan.FromHours(24));