Created
December 21, 2023 21:40
-
-
Save leonid-shevtsov/960dcf9af4fd3846be8bb3dec5602da0 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
enum DateIndex { | |
// All dates are integers counting from this date | |
static let zeroDate = Calendar.current.date(from: .init(year: 1970, month: 1, day: 1))! | |
static func fromDate(_ date: Date) -> Int { | |
Calendar.current.dateComponents([.day], from: zeroDate, to: date).day! | |
} | |
static func toDate(_ index: Int) -> Date { | |
Calendar.current.date(byAdding: .day, value: index, to: zeroDate)! | |
} | |
static func today() -> Int { | |
fromDate(Date()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment