Fix issues about weekday & monthday calculating

This commit is contained in:
QuentinJin 2018-08-26 22:37:51 +08:00
parent fa17c31a88
commit 92f0c3894a
3 changed files with 28 additions and 25 deletions

View File

@ -2,4 +2,5 @@ ignore:
- "Tests/" - "Tests/"
- "Schedule.playground" - "Schedule.playground"
comment: off comment:
layout: header, changes, diff

View File

@ -268,6 +268,9 @@ extension Schedule {
} }
} }
} }
}
extension Schedule {
/// Creates a schedule that executes the task immediately. /// Creates a schedule that executes the task immediately.
public static var now: Schedule { public static var now: Schedule {
@ -286,17 +289,17 @@ extension Schedule {
} }
} }
/// Creates a schedule that executes the task at the specific date.
public static func at(_ date: Date) -> Schedule {
return Schedule.of(date)
}
/// Creates a schedule that executes the task after delay then repeat /// Creates a schedule that executes the task after delay then repeat
/// every interval. /// every interval.
public static func after(_ delay: Interval, repeating interval: Interval) -> Schedule { public static func after(_ delay: Interval, repeating interval: Interval) -> Schedule {
return Schedule.after(delay).concat(Schedule.every(interval)) return Schedule.after(delay).concat(Schedule.every(interval))
} }
/// Creates a schedule that executes the task at the specific date.
public static func at(_ date: Date) -> Schedule {
return Schedule.of(date)
}
/// Creates a schedule that executes the task every period. /// Creates a schedule that executes the task every period.
public static func every(_ period: Period) -> Schedule { public static func every(_ period: Period) -> Schedule {
return Schedule.make { () -> AnyIterator<Interval> in return Schedule.make { () -> AnyIterator<Interval> in
@ -337,20 +340,15 @@ extension Schedule {
/// Returns a schedule at the specific time. /// Returns a schedule at the specific time.
public func at(_ time: Time) -> Schedule { public func at(_ time: Time) -> Schedule {
return Schedule.make { () -> AnyIterator<Date> in var interval = time.intervalSinceZeroClock
let iterator = self.schedule.dates.makeIterator() return Schedule.make { () -> AnyIterator<Interval> in
let calendar = Calendar.gregorian let it = self.schedule.makeIterator()
var last: Date!
return AnyIterator { return AnyIterator {
last = last ?? Date() if let next = it.next() {
guard let date = iterator.next(), defer { interval = 0.nanoseconds }
let next = calendar.nextDate(after: date.zeroClock(), return next + interval
matching: time.toDateComponents(),
matchingPolicy: .strict) else {
return nil
} }
defer { last = next } return nil
return next
} }
} }
} }
@ -392,7 +390,7 @@ extension Schedule {
var date: Date! var date: Date!
return AnyIterator<Date> { return AnyIterator<Date> {
if weekday.isToday { if weekday.isToday {
date = Date() date = Date().zeroClock()
} else if date == nil { } else if date == nil {
date = calendar.nextDate(after: Date(), matching: components, matchingPolicy: .strict) date = calendar.nextDate(after: Date(), matching: components, matchingPolicy: .strict)
} else { } else {
@ -423,7 +421,7 @@ extension Schedule {
var date: Date! var date: Date!
return AnyIterator<Date> { return AnyIterator<Date> {
if monthDay.isToday { if monthDay.isToday {
date = Date() date = Date().zeroClock()
} else if date == nil { } else if date == nil {
date = calendar.nextDate(after: Date(), matching: components, matchingPolicy: .strict) date = calendar.nextDate(after: Date(), matching: components, matchingPolicy: .strict)
} else { } else {

View File

@ -21,6 +21,10 @@ final class SchedulesTests: XCTestCase {
let s3 = Schedule.from([d0, d1, d2, d3]) let s3 = Schedule.from([d0, d1, d2, d3])
XCTAssertTrue(s2.makeIterator().isAlmostEqual(to: intervals, leeway: leeway)) XCTAssertTrue(s2.makeIterator().isAlmostEqual(to: intervals, leeway: leeway))
XCTAssertTrue(s3.makeIterator().isAlmostEqual(to: intervals, leeway: leeway)) XCTAssertTrue(s3.makeIterator().isAlmostEqual(to: intervals, leeway: leeway))
let longTime = (100 * 365).days
XCTAssertTrue(Schedule.distantPast.makeIterator().next()!.isLonger(than: longTime))
XCTAssertTrue(Schedule.distantFuture.makeIterator().next()!.isLonger(than: longTime))
} }
func testDates() { func testDates() {
@ -93,7 +97,7 @@ final class SchedulesTests: XCTestCase {
} }
func testEveryPeriod() { func testEveryPeriod() {
let s = Schedule.every(1.year).first(10) let s = Schedule.every("1 year").first(10)
var date = Date() var date = Date()
for i in s.dates { for i in s.dates {
XCTAssertEqual(i.dateComponents.year!, date.dateComponents.year! + 1) XCTAssertEqual(i.dateComponents.year!, date.dateComponents.year! + 1)
@ -104,17 +108,17 @@ final class SchedulesTests: XCTestCase {
} }
func testEveryWeekday() { func testEveryWeekday() {
let s = Schedule.every(.friday).at("11:11:00").first(5) let s = Schedule.every(.friday, .monday).at("11:11:00").first(5)
for i in s.dates { for i in s.dates {
XCTAssertEqual(i.dateComponents.weekday, 6) XCTAssertTrue(i.dateComponents.weekday == 6 || i.dateComponents.weekday == 2)
XCTAssertEqual(i.dateComponents.hour, 11) XCTAssertEqual(i.dateComponents.hour, 11)
} }
} }
func testEveryMonthday() { func testEveryMonthday() {
let s = Schedule.every(.april(1)).at(11, 11).first(5) let s = Schedule.every(.april(1), .october(1)).at(11, 11).first(5)
for i in s.dates { for i in s.dates {
XCTAssertEqual(i.dateComponents.month, 4) XCTAssertTrue(i.dateComponents.month == 4 || i.dateComponents.month == 10)
XCTAssertEqual(i.dateComponents.day, 1) XCTAssertEqual(i.dateComponents.day, 1)
XCTAssertEqual(i.dateComponents.hour, 11) XCTAssertEqual(i.dateComponents.hour, 11)
} }