-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from drekka/develop
More functions
- Loading branch information
Showing
15 changed files
with
306 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// | ||
// Day+Hashable.swift | ||
// | ||
// | ||
// Created by Derek Clarkson on 10/1/2024. | ||
// | ||
|
||
import Foundation | ||
|
||
extension Day: Hashable { | ||
public func hash(into hasher: inout Hasher) { | ||
hasher.combine(daysSince1970) | ||
} | ||
} | ||
|
||
extension Day: Equatable { | ||
public static func == (lhs: Day, rhs: Day) -> Bool { | ||
lhs.daysSince1970 == rhs.daysSince1970 | ||
} | ||
} | ||
|
||
extension Day: Comparable { | ||
public static func < (lhs: Day, rhs: Day) -> Bool { | ||
lhs.daysSince1970 < rhs.daysSince1970 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// | ||
// Day+CustomStringConvertable.swift | ||
// | ||
// | ||
// Created by Derek Clarkson on 15/1/2024. | ||
// | ||
|
||
import Foundation | ||
|
||
extension Day: CustomStringConvertible { | ||
public var description: String { | ||
self.formatted() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// | ||
// Day+Functions.swift | ||
// | ||
// | ||
// Created by Derek Clarkson on 16/1/2024. | ||
// | ||
|
||
import Foundation | ||
|
||
public extension Day { | ||
|
||
/// Adds the specific value to the current `Day` and returns a new `Day`. | ||
/// | ||
/// This is convenient for doing things like generating dates for the same day of each month. Note though that | ||
/// if the added value would produce an invaid date, the actual `Day` returned contains a valid date produced by | ||
/// rolling the values through the subsequent days and months. ie. if you do `Day(2001,2,3).day(byAdding: .day, value: 55)` you will get | ||
/// | ||
func day(byAdding component: Day.Component, value: Int) -> Day { | ||
let components = self.dayComponents() | ||
return Day(components.year + (component == .year ? value : 0), components.month + (component == .month ? value : 0), components.day + (component == .day ? value : 0)) | ||
} | ||
|
||
|
||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// | ||
// Day+Stridable.swift | ||
// | ||
// | ||
// Created by Derek Clarkson on 15/1/2024. | ||
// | ||
|
||
import Foundation | ||
|
||
extension Day: Strideable { | ||
|
||
public func distance(to other: Day) -> Int { | ||
other.daysSince1970 - self.daysSince1970 | ||
} | ||
|
||
public func advanced(by n: Int) -> Day { | ||
self + n | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// | ||
// DayOperationsTests.swift | ||
// | ||
// | ||
// Created by Derek Clarkson on 12/12/2023. | ||
// | ||
|
||
import DayType | ||
import Nimble | ||
import XCTest | ||
|
||
class DayHashableTests: XCTestCase { | ||
|
||
func testHash() { | ||
var days: Set = [Day(2020, 01, 11), Day(2020, 01, 12)] | ||
expect(days.contains(Day(2020, 01, 13))) == false | ||
expect(days.contains(Day(2020, 01, 12))) == true | ||
|
||
// Modify and try again. | ||
days.insert(Day(2020, 01, 13)) | ||
expect(days.contains(Day(2020, 01, 13))) == true | ||
expect(days.contains(Day(2020, 01, 12))) == true | ||
|
||
// Duplicate check. | ||
days.insert(Day(2020, 01, 11)) | ||
expect(days.count) == 3 | ||
} | ||
} | ||
|
||
class DayEquatableTests: XCTestCase { | ||
|
||
func testEquals() { | ||
expect(Day(2020, 3, 12) == Day(2020, 3, 12)) == true | ||
expect(Day(2020, 3, 12) == Day(2001, 1, 5)) == false | ||
} | ||
|
||
func testNotEquals() { | ||
expect(Day(2020, 3, 12) != Day(2001, 1, 5)) == true | ||
expect(Day(2020, 3, 12) != Day(2020, 3, 12)) == false | ||
} | ||
} | ||
|
||
class DayComparableTests: XCTestCase { | ||
|
||
func testGreaterThan() { | ||
expect(Day(2020, 3, 12) > Day(2020, 3, 11)) == true | ||
expect(Day(2020, 3, 12) > Day(2020, 3, 12)) == false | ||
expect(Day(2020, 3, 12) > Day(2020, 3, 13)) == false | ||
} | ||
|
||
func testGreaterThanEquals() { | ||
expect(Day(2020, 3, 12) >= Day(2020, 3, 11)) == true | ||
expect(Day(2020, 3, 12) >= Day(2020, 3, 12)) == true | ||
expect(Day(2020, 3, 12) >= Day(2020, 3, 13)) == false | ||
} | ||
|
||
func testLessThan() { | ||
expect(Day(2020, 3, 12) < Day(2020, 3, 11)) == false | ||
expect(Day(2020, 3, 12) < Day(2020, 3, 12)) == false | ||
expect(Day(2020, 3, 12) < Day(2020, 3, 13)) == true | ||
} | ||
|
||
func testLessThanEquals() { | ||
expect(Day(2020, 3, 12) <= Day(2020, 3, 11)) == false | ||
expect(Day(2020, 3, 12) <= Day(2020, 3, 12)) == true | ||
expect(Day(2020, 3, 12) <= Day(2020, 3, 13)) == true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// | ||
// DayCustomStringConvertableTests.swift | ||
// | ||
// | ||
// Created by Derek Clarkson on 15/1/2024. | ||
// | ||
|
||
import DayType | ||
import Nimble | ||
import XCTest | ||
|
||
class DayCustomStringConvertableTests: XCTestCase { | ||
func testyDescription() { | ||
let date = DateComponents(calendar: .current, year: 2001, month: 2, day: 3).date! | ||
expect(Day(2001, 2, 3).description) == date.formatted(date: .abbreviated, time: .omitted) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// | ||
// DayFunctionsTests.swift | ||
// | ||
// | ||
// Created by Derek Clarkson on 16/1/2024. | ||
// | ||
|
||
import DayType | ||
import Nimble | ||
import XCTest | ||
|
||
class DayFunctionsTests: XCTestCase { | ||
|
||
func testDayByAdding() { | ||
expect(Day(2001, 2, 3).day(byAdding: .day, value: 3)) == Day(2001, 2, 6) | ||
expect(Day(2001, 2, 3).day(byAdding: .month, value: 3)) == Day(2001, 5, 3) | ||
expect(Day(2001, 2, 3).day(byAdding: .year, value: 3)) == Day(2004, 2, 3) | ||
} | ||
|
||
func testDayByAddingRolling() { | ||
expect(Day(2001, 2, 3).day(byAdding: .day, value: 55)) == Day(2001, 3, 30) | ||
expect(Day(2001, 2, 3).day(byAdding: .month, value: 55)) == Day(2005, 9, 10) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.