具有不同时区的Quartz.Net DailyTimeIntervalTriggerImpl

时间:2013-01-09 01:08:15

标签: c# .net quartz-scheduler quartz.net

是的,这有点棘手。

我想创建一个每日触发器(DailyTimeIntervalTriggerImpl),以便它每天从 07:30 运行到 23:45 。 问题是,它必须在用户指定的时区运行,在这种情况下,让我们说东京时间。

根据我的理解,我需要将时间转换为utc。

下面的示例工作正常,但触发器将在endTimeUTC之后到期。如何确保触发器连续运行?

注意:我尝试使用DailyTimeIntervalTriggerImpl的 StartTimeOfDay EndTimeOfDay 属性,但是当我将它们转换为UTC时,我会遇到奇怪的开火时间。 我相信的问题是因为:

a)utc形式的开始时间(在本例中) 12/31/2012 10:30:00 PM

b)utc中的结束时间 1/1/2013 2:30:00 PM 第二天)。

DailyTimeIntervalTriggerImpl StartTimeOfDay EndTimeOfDay 仅接受TimeOfDay对象,该对象仅考虑时间而非日期。

所以它将是StartTimeOfDay = 10:30 PM和EndTimeOfDay = 2:30 PM,这真的没有意义。因为EndTimeOfDay应该是第二天下午2:30。

有什么想法吗?

 var startTime = DateTime.Parse("2013/01/01 07:30:00");
 var endTime = DateTime.Parse("2013/01/01 23:45:00");

 var tokyoTimeZoneId = "Tokyo Standard Time";

 TimeZoneInfo timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(tokyoTimeZoneId);

 var startDateTimeUtc = TimeZoneInfo.ConvertTimeToUtc(startTime, timeZoneInfo);
 var endDateTimeUtc = TimeZoneInfo.ConvertTimeToUtc(endTime, timeZoneInfo);

 var dailyTrigger = new DailyTimeIntervalTriggerImpl
  {
    RepeatIntervalUnit = IntervalUnit.Hour,
    DaysOfWeek = GetDays(), // method to get a Quartz.Collection.HashSet list
    RepeatInterval = 3, // every 3 hours
    Key = new TriggerKey(Guid.NewGuid().ToString(), "my-group"),
    StartTimeUtc = startDateTimeUtc,
    EndTimeUtc = endDateTimeUtc,
  };

  // Compute fire times just to show simulated fire times
  IList<DateTimeOffset> fireTimes = ComputeFireTimes(dailyTrigger, null, 10);

  foreach (var dateTimeOffset in fireTimes)
  {
    Console.WriteLine("utc:{0} tokyo:{1}", dateTimeOffset,
      TimeZoneInfo.ConvertTimeFromUtc(dateTimeOffset.DateTime, timeZoneInfo));
  }

1 个答案:

答案 0 :(得分:0)

试试这个:

var daysOfWeek = new Quartz.Collection.HashSet<System.DayOfWeek>() { System.DayOfWeek.Wednesday, System.DayOfWeek.Thursday, System.DayOfWeek.Friday };

DateTimeOffset startTime = DateBuilder.DateOf(0, 0, 0, 14, 1, 2013);
DateTimeOffset endTime = DateBuilder.DateOf(0, 0, 0, 20, 1, 2013);

TimeOfDay startTimeOfDay = TimeOfDay.HourMinuteAndSecondOfDay(7, 30, 0);
TimeOfDay endTimeOfDay = TimeOfDay.HourMinuteAndSecondOfDay(23, 45, 0);

var tokyoTimeZoneId = "Tokyo Standard Time";
TimeZoneInfo timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(tokyoTimeZoneId);

var dailyTrigger = new DailyTimeIntervalTriggerImpl
{
StartTimeUtc = startTime.ToUniversalTime(),
EndTimeUtc = endTime.ToUniversalTime(),
StartTimeOfDay = startTimeOfDay,
EndTimeOfDay = endTimeOfDay,
RepeatIntervalUnit = IntervalUnit.Hour,
DaysOfWeek = daysOfWeek,
RepeatInterval = 3,             // every 3 hours
TimeZone = timeZoneInfo,
Key = new TriggerKey(Guid.NewGuid().ToString(), "my-group"),
};

// Compute fire times just to show simulated fire times
IList<DateTimeOffset> fireTimes = TriggerUtils.ComputeFireTimes(dailyTrigger, null, 1000);

foreach (var dateTimeOffset in fireTimes)
{
Console.WriteLine("utc:{0} - tokyo:{1}", dateTimeOffset,
  TimeZoneInfo.ConvertTimeFromUtc(dateTimeOffset.DateTime, timeZoneInfo));
}

注意

我建议你使用最新版本:2.1.1