I'm trying to adapt code found here to make parsing dates more performant. Doing the following and then attempting to print the values is giving me massive numbers, rather than the underlying real value (I'm assuming it is a memory address).
private static var components = DateComponents()
private static var year = UnsafeMutablePointer<Int>.allocate(capacity: 1)
private static let month = UnsafeMutablePointer<Int>.allocate(capacity: 1)
private static let day = UnsafeMutablePointer<Int>.allocate(capacity: 1)
private static let hour = UnsafeMutablePointer<Int>.allocate(capacity: 1)
private static let minute = UnsafeMutablePointer<Int>.allocate(capacity: 1)
private static let second = UnsafeMutablePointer<Float>.allocate(capacity: 1)
private static let hourOffset = UnsafeMutablePointer<Int>.allocate(capacity: 1)
private static let minuteOffset = UnsafeMutablePointer<Int>.allocate(capacity: 1)
let parseCount = withVaList(
[year, month, day, hour, minute, second, hourOffset, minuteOffset],
{ pointer -> Int32 in
vsscanf("2022-09-21T09:01:00+01:00", "%d-%d-%dT%d:%d:%f%d:%dZ", pointer)
}
)
components.year = year.pointee
components.minute = minute.pointee
components.day = day.pointee
components.hour = hour.pointee
components.month = month.pointee
components.second = Int(second.pointee)
dump(components)
The resulting dump looks like this:
year: 5764607523034236902 month: 5764607523034234889 day: 5764607523034234901 hour: 5764607523034234889 minute: 5764607523034234881 second: 0 isLeapMonth: false
Now keep in mind that this actually works for dates that have the correct formatter passed in (i.e. it can create the date from the from the components). However, where the formatter is incorrect, I cannot see what the current value is for each UnsafeMutablePointer
. This makes debugging the formatter really difficult. Anyone got any ideas why I can't see the value of the pointee?
Note: Casting the other values as Int() does not work, unfortunately.
Update: I've even played around with this in C++ here. The parsing does seem to work. I just want to inspect what the value of val6
and val7
are - but in the Xcode debugger rather than using a playpen for C++.