Skip to content

Commit

Permalink
Add some convenience helpers for Time/Date
Browse files Browse the repository at this point in the history
  • Loading branch information
hughbe committed Feb 7, 2018
1 parent bcbb7d3 commit ed9938d
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 41 deletions.
2 changes: 1 addition & 1 deletion DayDatePicker.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'DayDatePicker'
s.version = '1.2'
s.version = '1.3'
s.summary = 'A custom and customizable UIDatePicker which displays the day of the week alongside the day column'

s.description = <<-DESC
Expand Down
68 changes: 41 additions & 27 deletions DayDatePicker/Classes/DayDatePicker.Date.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,20 @@

extension DayDatePickerView {
public struct Date: Comparable {
public static func <(lhs: DayDatePickerView.Date, rhs: DayDatePickerView.Date) -> Bool {
if lhs.year < rhs.year {
return true
} else if lhs.year == rhs.year {
if lhs.month < rhs.month {
return true
} else if lhs.month == rhs.month && lhs.day < rhs.day {
return true
}
}

return false
}

public static func ==(lhs: DayDatePickerView.Date, rhs: DayDatePickerView.Date) -> Bool {
return lhs.year == rhs.year && lhs.month == rhs.month && lhs.day == rhs.day
public init(year: Int, month: Int, day: Int) {
self.year = year
self.month = month
self.day = day
}

public var date: Foundation.Date {
get {
var components = DateComponents()
components.year = year
components.month = month
components.day = day

return Calendar.current.date(from: components)!
}

public init(date: Foundation.Date) {
let components = Calendar.current.dateComponents([.year, .month, .day], from: date)

year = components.year ?? 0
month = components.month ?? 0
day = components.day ?? 0
}

public var year: Int {
willSet {
if let yearsInEra = Calendar.current.range(of: .year, in: .era, for: date) {
Expand All @@ -59,5 +44,34 @@ extension DayDatePickerView {
}
}
}

public var date: Foundation.Date {
get {
var components = DateComponents()
components.year = year
components.month = month
components.day = day

return Calendar.current.date(from: components)!
}
}

public static func <(lhs: DayDatePickerView.Date, rhs: DayDatePickerView.Date) -> Bool {
if lhs.year < rhs.year {
return true
} else if lhs.year == rhs.year {
if lhs.month < rhs.month {
return true
} else if lhs.month == rhs.month && lhs.day < rhs.day {
return true
}
}

return false
}

public static func ==(lhs: DayDatePickerView.Date, rhs: DayDatePickerView.Date) -> Bool {
return lhs.year == rhs.year && lhs.month == rhs.month && lhs.day == rhs.day
}
}
}
10 changes: 10 additions & 0 deletions DayDatePicker/Classes/DayDatePickerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ public class DayDatePickerView : UIControl {
let date = Date(year: year, month: month, day: day)
setDate(date: date, animated: animated)
}

public func setDate(date: Foundation.Date, animated: Bool) {
let dateDate = Date(date: date)
setDate(date: dateDate, animated: animated)
}

public func setDate(date: Date, animated: Bool) {
var date = date
Expand Down Expand Up @@ -78,6 +83,11 @@ public class DayDatePickerView : UIControl {
let minDate = Date(year: year, month: month, day: day)
setMinDate(minDate: minDate, animated: animated)
}

public func setMinDate(minDate: Foundation.Date, animated: Bool) {
let minDateDate = Date(date: minDate)
setMinDate(minDate: minDateDate, animated: animated)
}

public func setMinDate(minDate: Date?, animated: Bool) {
_minDate = minDate
Expand Down
58 changes: 47 additions & 11 deletions DayDatePicker/Classes/TimePickerView.Time.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,16 @@ import Foundation

public extension TimePickerView {
public struct Time: Comparable {
public static func <(lhs: TimePickerView.Time, rhs: TimePickerView.Time) -> Bool {
if lhs.hour < rhs.hour {
return true
} else if lhs.hour == rhs.hour && lhs.minute < rhs.minute {
return true
}

return false
public init(hour: Int, minute: Int) {
self.hour = hour
self.minute = minute
}

public static func ==(lhs: TimePickerView.Time, rhs: TimePickerView.Time) -> Bool {
return lhs.hour == rhs.hour && lhs.minute == rhs.minute

public init(date: Date) {
let components = Calendar.current.dateComponents([.hour, .minute], from: date)

self.hour = components.hour ?? 0
self.minute = components.minute ?? 0
}

public var hour: Int {
Expand All @@ -38,5 +36,43 @@ public extension TimePickerView {
}
}
}

public var date: Date? {
var components = DateComponents()
components.hour = hour
components.minute = minute

return Calendar.current.date(from: components)
}

public static func <(lhs: TimePickerView.Time, rhs: TimePickerView.Time) -> Bool {
if lhs.hour < rhs.hour {
return true
} else if lhs.hour == rhs.hour && lhs.minute < rhs.minute {
return true
}

return false
}

public static func ==(lhs: TimePickerView.Time, rhs: TimePickerView.Time) -> Bool {
return lhs.hour == rhs.hour && lhs.minute == rhs.minute
}

public func time(byAddingHour hour: Int, andMinutes minutes: Int) -> Time? {
guard let date = date else {
return nil
}

var components = DateComponents()
components.hour = hour
components.minute = minutes

guard let addedDate = Calendar.current.date(byAdding: components, to: date) else {
return nil
}

return Time(date: addedDate)
}
}
}
4 changes: 2 additions & 2 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PODS:
- DayDatePicker (0.1.0)
- DayDatePicker (1.3)
- FBSnapshotTestCase (2.1.4):
- FBSnapshotTestCase/SwiftSupport (= 2.1.4)
- FBSnapshotTestCase/Core (2.1.4)
Expand All @@ -15,7 +15,7 @@ EXTERNAL SOURCES:
:path: ../

SPEC CHECKSUMS:
DayDatePicker: 1e930a2bd6a083c2298e93582d71479aecc48dad
DayDatePicker: 9a6bdbde3d7ac5b7a649054951ad3bff8ad6c92a
FBSnapshotTestCase: 094f9f314decbabe373b87cc339bea235a63e07a

PODFILE CHECKSUM: 69fcf032d21f0c566bbf962e2e8c2116500e1eda
Expand Down

0 comments on commit ed9938d

Please sign in to comment.