SwiftDate
SwiftDate copied to clipboard
Debug View rounding bug (Xcode) for Date returned by endOf function
I am using:
let yearArray = [1970, 1991, 1999, 2005, nil].map( { transformEndDateOfYear($0) })
func transformEndDateOfYear(_ year: Int?) -> Date? {
guard let year = year else {
return nil
}
let d = Date(year: year, month: 12, day: 31, hour: 0, minute: 0)
return d.dateAtEndOf(.day)
}
results are totally wrong

Same incorrect results for endOf month and year
Have you tried to print the date?
I've tried it and I found a curious behaviour

The date seems rounded in debug viewer but the value is actually correct. Generation code subtract the smallest unit possible from the date; that's the code: https://github.com/malcommac/SwiftDate/blob/master/Sources/SwiftDate/DateInRegion/DateInRegion%2BCreate.swift#L202:
let endOfThisUnit = Date(timeInterval: /*-0.001*/ -1, since: startOfNextUnit as Date)
If I try to increase the number of timeInterval to subtract to 1 even the debug viewer works fine.
let endOfThisUnit = Date(timeInterval: -1, since: startOfNextUnit as Date)
Can you confirm it?
I would like to say it is correct however during tests I was doing comparison between transformEndDateOfYear and manually created dates (with using Date.init()) and it was constantly failing. It's been a while since I was working with this and I made a workaround here. I will try to reproduce as soon as possible.
Ok I'll wait for you. We can decide to remove an entire second but if we found better alternatives I'll be more satisfied. Thanks for your support