Most often, it is important that a date also near-precisely represent a point in time. In Swift, for that, we have Foundation.Date
. TimeIndependentDate
fills a gap where, in limited circumstances, we need to deal with a date independent of time. This is particularly useful when interfacing with third party systems which supply dates independent of time.
I often find myself needing a time-independent date in Swift. So I've open-sourced a TimeIndependentDate
type for anyone else that might need one: https://github.com/hwjeremy/time-independent-date-swift
Example Usage
// Initialise a date, throwing a `TimeIndependentDate` error if the supplied
// literal values are invalid.
let timeIndependentDate = try TimeIndependentDate(
year: 2020,
month: .january,
day: 1
)
// Parse a date from a string. Adjust encoding order as desired. A
// `TimeIndependentDateError` is thrown if the supplied string
// is not a valid date.
let timeIndependentDate = try TimeIndependentDate.from(
"2021-01-01",
encodingOrder: .dayFirst
)
Top comments (0)