Motivation
I often see myself needing e.g. the first or last day of a month or a year for generating statistics or e.g. EOM trigger.
In Swift, I find such date handling pretty complicated, as you have to use Calendar and DateComponents to get the wanted behavior which always requires multiple lines of code. While this can be worked around with using extensions (which I use as a standard in all my projects) I think it would be nice to see such features integrated into Foundation.
Proposal
For this reason I propose adding methods to the Date type to shortcut such behavior.
While this is a pitch, and and detailed implementation is of course open to discussion I was thinking of adding something like
let midnight = Date().date(second: 0, minute: 0, hour: 0) // 05.07.2024 00:00:00
let firstDayOfMonth = Date().date(day: 1) // 01.07.2024…
let firstDayOfYear = Date().date(day: 1, month: 1) // 01.01.2024…
…
All the parameters should have a default value for the current date, which would mean that, at least internally, DateComponents like day need to be directly accessible via date, but something like
let day = Date().day // 1
would also be nice to have publicly exposed, if the easiest implementation of my idea would require it anyway.
Internally, it can still use Calendar and DateComponents, per default I would suggest using Calendar.current but it is a parameter, to customize the Calendar, if the user desires so.
This pitch would make some methods inside DateComponents obsolete, but for backwards compatibility, I would not remove these methods.