NSDateFormatter vs NSCalendar for DST Transition Ambiguities
25.5K reputation · 14 Apr 2020, 00:21 UTC
Handling Regional Time Zone Conversions
When converting regional date and time data to a UTC-based NSDate, Objective-C provides two primary paths: NSDateFormatter for string-based parsing and NSCalendar with NSDateComponents for programmatic construction.
The DST Transition Constraint
A critical challenge arises during Daylight Saving Time (DST) transitions, specifically during 'fold' periods where a local time occurs twice or 'gap' periods where a local time does not exist. While NSDateFormatter simplifies the conversion by applying the NSTimeZone rules to a formatted string, NSCalendar offers more granular control over individual date components.
Because NSDate objects are time-zone agnostic and store only UTC, the method used to derive the date determines how these ambiguities are resolved. There is no universal framework policy to automatically enforce whether a duplicated hour refers to the first or second occurrence during a transition.
- Which approach provides more reliable behavior when parsing dates that fall exactly on a DST overlap?
- Does
NSCalendaroffer a mechanism to explicitly define the fold occurrence thatNSDateFormatterlacks?