Skip to content

Commit b030eac

Browse files
sherlock1982Nikolai Orekhov
andauthored
Allow to execute a job if remaining day time equals interval (quartznet#2726)
Co-authored-by: Nikolai Orekhov <[email protected]>
1 parent 275a99f commit b030eac

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

src/Quartz.Tests.Unit/DailyTimeIntervalScheduleBuilderTest.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#region License
1+
#region License
22

33
/*
44
* All content copyright Marko Lahma, unless otherwise indicated. All rights reserved.
@@ -296,6 +296,31 @@ public void TestEndingAtAfterCountEndTimeOfDayValidation()
296296
Assert.DoesNotThrow(trigger.Validate, "We should accept EndTimeOfDay specified by EndingDailyAfterCount(x).");
297297
}
298298

299+
[Test]
300+
public void TestEndingAtAfterCountEndTimeOfDayLastIntervalValidation()
301+
{
302+
Assert.DoesNotThrow(() => TriggerBuilder.Create()
303+
.WithIdentity("testTrigger")
304+
.ForJob("testJob")
305+
.WithDailyTimeIntervalSchedule(x =>
306+
x.StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(23, 59))
307+
.EndingDailyAfterCount(1))
308+
.Build(), "We should accept if remaining time equals to the interval of 1 minute");
309+
}
310+
311+
[Test]
312+
public void TestEndingAtAfterCountEndTimeOfDayLastIntervalValidationFailed()
313+
{
314+
Assert.Throws<ArgumentException>(() => TriggerBuilder.Create()
315+
.WithIdentity("testTrigger")
316+
.ForJob("testJob")
317+
.WithDailyTimeIntervalSchedule(x =>
318+
x.StartingDailyAt(TimeOfDay.HourMinuteAndSecondOfDay(23, 59, 59))
319+
.WithInterval(1, IntervalUnit.Minute)
320+
.EndingDailyAfterCount(1))
321+
.Build(), "We should not accept if remaining time less than the interval of 1 minute");
322+
}
323+
299324
[Test]
300325
public void TestCanSetTimeZone()
301326
{

src/Quartz/DailyTimeIntervalScheduleBuilder.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#region License
1+
#region License
22

33
/*
44
* All content copyright Marko Lahma, unless otherwise indicated. All rights reserved.
@@ -366,14 +366,14 @@ public DailyTimeIntervalScheduleBuilder EndingDailyAfterCount(int count)
366366

367367
DateTimeOffset today = SystemTime.UtcNow();
368368
DateTimeOffset startTimeOfDayDate = startTimeOfDayUtc.GetTimeOfDayForDate(today);
369-
DateTimeOffset maxEndTimeOfDayDate = TimeOfDay.HourMinuteAndSecondOfDay(23, 59, 59).GetTimeOfDayForDate(today);
369+
DateTimeOffset tomorrow = startTimeOfDayDate.AddDays(1).UtcDateTime.Date;
370370

371371
//apply proper offsets according to timezone
372372
TimeZoneInfo targetTimeZone = timeZone ?? TimeZoneInfo.Local;
373373
startTimeOfDayDate = new DateTimeOffset(startTimeOfDayDate.DateTime, TimeZoneUtil.GetUtcOffset(startTimeOfDayDate.DateTime, targetTimeZone));
374-
maxEndTimeOfDayDate = new DateTimeOffset(maxEndTimeOfDayDate.DateTime, TimeZoneUtil.GetUtcOffset(maxEndTimeOfDayDate.DateTime, targetTimeZone));
374+
tomorrow = new DateTimeOffset(tomorrow.DateTime, TimeZoneUtil.GetUtcOffset(tomorrow.DateTime, targetTimeZone));
375375

376-
TimeSpan remainingMillisInDay = maxEndTimeOfDayDate - startTimeOfDayDate;
376+
TimeSpan remainingMillisInDay = tomorrow - startTimeOfDayDate;
377377
TimeSpan intervalInMillis;
378378
if (intervalUnit == IntervalUnit.Second)
379379
{
@@ -406,7 +406,7 @@ public DailyTimeIntervalScheduleBuilder EndingDailyAfterCount(int count)
406406
TimeSpan incrementInMillis = TimeSpan.FromTicks((count - 1) * intervalInMillis.Ticks);
407407
DateTimeOffset endTimeOfDayDate = startTimeOfDayDate.Add(incrementInMillis);
408408

409-
if (endTimeOfDayDate > maxEndTimeOfDayDate)
409+
if (endTimeOfDayDate >= tomorrow)
410410
{
411411
throw new ArgumentException("The given count " + count + " is too large! The max you can set is " + maxNumOfCount);
412412
}

0 commit comments

Comments
 (0)