Swift Foundation Date Representation

We've been discussing this in Vapor and on the Swift Slack as well. But essentially Date is not fit for purpose in Swift 6. Swift 6 introduced added precision which has hit up against Double precision errors causing all sorts of issues and changed the underlying sys call to get the current time.

The trouble with this approach is that the API really pushes you to just compare the date directly. It's much easier to just do if date == someOtherDate than to break it down into different components.

Additionally I don't think it's an unfair expectation to create two dates from the same TimeInterval and compare them and expect them to be the same - this does not always happen due to rounding issues with doubles and has caused a lot of issues in things like tests that used to work in Swift 5.

Ideally Date would switch the internal representation of time to something that doesn't arbitrarily have a loss of precision. Storing nanoseconds since 1970 as a Int64 would give you a date range up to the year 2262 which isn't great. Int128 would be better but I'm unsure if there are any compatibility/performance issues with using it.

This could be done in a purely additive way, by changing the internal representation and keeping the existing API and converting between the two with new APIs where it made sense (such as nanosecondsSince1970). That should be possible without breaking the ABI right?

7 Likes