Skip to content

Commit

Permalink
Merge pull request #36 from ksmandersen/feature/duration-hours
Browse files Browse the repository at this point in the history
Added support for specifying duration in hours
  • Loading branch information
BrettRToomey authored Aug 3, 2017
2 parents 571caed + 30c6d1a commit 92bcca5
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Jobs.add(interval: .seconds(4)) {
```

## Intervals ⏲
The `Duration` enumeration currently supports `.seconds`, `.days` and `.weeks`.
The `Duration` enumeration currently supports `.seconds`, `hours`, `.days` and `.weeks`.
```swift
Jobs.add(interval: .days(5)) {
print("See you every 5 days.")
Expand All @@ -40,6 +40,7 @@ Jobs.add(interval: .days(5)) {
It's possible to create a `Duration` from an `Int` and a `Double`.
```swift
10.seconds // `Duration.seconds(10)`
4.hours // `Duration.hours(4)`
2.days // `Duration.days(2)`
3.weeks // `Duration.weeks(3)`
```
Expand Down
5 changes: 5 additions & 0 deletions Sources/Duration+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ extension Int {
public var seconds: Duration {
return .seconds(Double(self))
}

/// Converts the integer into an enum representation of hours.
public var hours: Duration {
return .hours(self)
}

/// Converts the integer into an enum representation of days.
public var days: Duration {
Expand Down
5 changes: 5 additions & 0 deletions Sources/Jobs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Foundation
/// Represents an amount of time.
public enum Duration {
case seconds(Double)
case hours(Int)
case days(Int)
case weeks(Int)
}
Expand All @@ -14,6 +15,10 @@ extension Duration {
switch self {
case .seconds(let count):
return count

case .hours(let count):
let secondsInHour = 3_600
return Double(count * secondsInHour)

case .days(let count):
let secondsInDay = 86_400
Expand Down
6 changes: 6 additions & 0 deletions Tests/JobsTests/jobsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class JobsTests: XCTestCase {
let fiveSeconds = Duration.seconds(5).unixTime
XCTAssertEqual(fiveSeconds, 5.0)

let threeHours = Duration.hours(3).unixTime
XCTAssertEqual(threeHours, 10800.0)

let nineDays = Duration.days(9).unixTime
XCTAssertEqual(nineDays, 777600.0)

Expand All @@ -30,6 +33,9 @@ class JobsTests: XCTestCase {
func testDurationExtensions() {
let seconds = 2.seconds
XCTAssertEqual(seconds.unixTime, 2.0)

let hours = 3.hours
XCTAssertEqual(hours.unixTime, 10800.0)

let days = 9.days
XCTAssertEqual(days.unixTime, 777600.0)
Expand Down

0 comments on commit 92bcca5

Please sign in to comment.