Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(every): adding every N minutes or hours #31

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions Sources/Jobs/Scheduler/Schedule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,39 @@ public struct Schedule: Sendable {
.init(second: .specific(second))
}

/// Return schedule that generates a Date for minutes, hours or seconds
/// - Parameter minute: minute value e.g 5
/// - Parameter hour: hour value e.g 3
/// - Parameter timeZone defaults to current timezone
public static func every(minute: Int = 5, hour: Int = 0, timeZone: TimeZone = .current) -> Self {
let maxNumber = if minute > 0, hour == 0 {
60 / minute
} else {
24 / hour
}

var multiples: [Int] = []
var index = 0
let number = hour > 0 ? hour : minute
repeat {
let multiple = number * index

if hour > 0, multiple > 24 {
let remainder = multiple % 24
multiples.append(remainder - hour)
} else {
multiples.append(multiple)
}

index = index + 1
} while index < maxNumber

if hour == 0 {
return .onMinutes(multiples)
}
return .onHours(multiples, timeZone: timeZone)
}

/// Return Schedule that generates a Date for a selection of minutes
/// - Parameters
/// - minutes: Array of minutes if should return Dates for
Expand Down
9 changes: 9 additions & 0 deletions Tests/JobsTests/JobSchedulerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ final class JobSchedulerTests: XCTestCase {
try self.testSchedule(start: "1999-12-31T23:59:25Z", expectedEnd: "2000-01-01T00:00:15Z", schedule: .everyMinute(second: 15))
}

func testEverySchedule() throws {
try self.testSchedule(start: "2021-06-21T21:00:00Z", expectedEnd: "2021-06-21T22:00:00Z", schedule: .every(minute: 5))
try self.testSchedule(start: "2021-06-21T21:10:15Z", expectedEnd: "2021-06-21T22:00:00Z", schedule: .every(minute: 5))
try self.testSchedule(start: "2024-02-22T20:00:00Z", expectedEnd: "2024-02-23T00:00:00Z", schedule: .every(hour: 3))
try self.testSchedule(start: "2024-02-22T20:00:00Z", expectedEnd: "2024-02-23T00:00:00Z", schedule: .every(hour: 12))
try self.testSchedule(start: "2024-02-22T00:00:00Z", expectedEnd: "2024-02-23T00:00:00Z", schedule: .every(hour: 12))
try self.testSchedule(start: "2024-02-22T01:00:00Z", expectedEnd: "2024-02-23T00:00:00Z", schedule: .every(hour: 24))
}

func testHourlySchedule() throws {
try self.testSchedule(start: "2021-06-21T21:10:15Z", expectedEnd: "2021-06-21T21:58:00Z", schedule: .hourly(minute: 58))
try self.testSchedule(start: "1999-12-31T23:59:25Z", expectedEnd: "2000-01-01T00:01:00Z", schedule: .hourly(minute: 1))
Expand Down