SwiftDate icon indicating copy to clipboard operation
SwiftDate copied to clipboard

Debug View rounding bug (Xcode) for Date returned by endOf function

Open pgawlowski opened this issue 6 years ago • 3 comments

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

screenshot 2019-02-28 at 09 12 58

Same incorrect results for endOf month and year

pgawlowski avatar Feb 28 '19 08:02 pgawlowski

Have you tried to print the date? I've tried it and I found a curious behaviour Screenshot 2019-03-27 at 20 53 08

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?

malcommac avatar Mar 27 '19 19:03 malcommac

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.

pgawlowski avatar Mar 28 '19 12:03 pgawlowski

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

malcommac avatar Mar 31 '19 12:03 malcommac