Skip to content

Commit

Permalink
Add ability to use custom Calendar in ScheduleBuilder (#113)
Browse files Browse the repository at this point in the history
Add Calendar to ScheduleBuilder
  • Loading branch information
dylanshine authored Aug 8, 2022
1 parent eab1970 commit e2f6c71
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 31 deletions.
10 changes: 8 additions & 2 deletions Sources/Queues/ScheduleBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import struct Foundation.Calendar

/// An object that can be used to build a scheduled job
public final class ScheduleBuilder {

/// Months of the year
public enum Month: Int {
case january = 1
Expand Down Expand Up @@ -374,13 +375,16 @@ public final class ScheduleBuilder {
if let month = self.month {
components.month = month.rawValue
}
return Calendar.current.nextDate(
return calendar.nextDate(
after: current,
matching: components,
matchingPolicy: .strict
)
}

/// The caledar used to compute the next date
let calendar: Calendar

/// Date to perform task (one-off job)
var date: Date?
var month: Month?
Expand All @@ -391,7 +395,9 @@ public final class ScheduleBuilder {
var second: Second?
var millisecond: Int?

public init() { }
public init(calendar: Calendar = .current) {
self.calendar = calendar
}

// MARK: Helpers

Expand Down
60 changes: 31 additions & 29 deletions Tests/QueuesTests/ScheduleBuilderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,27 @@ final class ScheduleBuilderTests: XCTestCase {
Date(year: 2020, month: 5, day: 23, hour: 14, minute: 58)
)
}

func testCustomCalendarBuilder() throws {

let est = Calendar.calendar(timezone: "EST")
let mst = Calendar.calendar(timezone: "MST")

// Create a date at 8:00pm EST
let estDate = Date(calendar: est, hour: 20, minute: 00)

// Schedule it for 7:00pm MST
let builder = ScheduleBuilder(calendar: mst)
builder.daily().at("7:00pm")

XCTAssertEqual(
builder.nextDate(current: estDate),
// one hour later
Date(calendar: est, hour: 21, minute: 00)
)

}

}


Expand All @@ -144,35 +165,8 @@ final class Cleanup: ScheduledJob {
}

extension Date {
var year: Int {
Calendar.current.component(.year, from: self)
}

var month: Int {
Calendar.current.component(.month, from: self)
}

var weekday: Int {
Calendar.current.component(.weekday, from: self)
}

var day: Int {
Calendar.current.component(.day, from: self)
}

var hour: Int {
Calendar.current.component(.hour, from: self)
}

var minute: Int {
Calendar.current.component(.minute, from: self)
}

var second: Int {
Calendar.current.component(.second, from: self)
}

init(
calendar: Calendar = .current,
year: Int = 2020,
month: Int = 1,
day: Int = 1,
Expand All @@ -181,8 +175,16 @@ extension Date {
second: Int = 0
) {
self = DateComponents(
calendar: .current,
calendar: calendar,
year: year, month: month, day: day, hour: hour, minute: minute, second: second
).date!
}
}

extension Calendar {
fileprivate static func calendar(timezone identifier: String) -> Calendar {
var calendar = Calendar.current
calendar.timeZone = TimeZone(identifier: identifier)!
return calendar
}
}

0 comments on commit e2f6c71

Please sign in to comment.