Created
July 18, 2020 02:33
-
-
Save ohlulu/33cb78e298744d6fc4c8fae23e6fdd65 to your computer and use it in GitHub Desktop.
Swift @propertyWrapper for Date
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
extension Date { | |
func add(_ component: Calendar.Component, value: Int) -> Date { | |
return Calendar.current.date(byAdding: component, value: value, to: self)! | |
} | |
} | |
@propertyWrapper | |
struct BeginEndDate { | |
enum `Type` { | |
case begin, end | |
} | |
private var date: Date = Date() | |
private var type: Type = .begin | |
var wrappedValue: Date { | |
get { | |
if type == .begin { | |
return Calendar.current.startOfDay(for: date) | |
} else { | |
var newDate = date.add(.day, value: 1) | |
newDate = Calendar.current.startOfDay(for: newDate) | |
newDate = newDate.add(.second, value: -1) | |
return newDate | |
} | |
} | |
set { date = newValue } | |
} | |
init(date: Date, type: Type) { | |
self.date = date | |
self.type = type | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment