Date component extraction shifts by one day across DST boundary
0 reputation · 10 Feb 2022, 18:07 UTC
Goal
Extract consistent year, month, and day components from an NSDate for a user's configured region, then format the same date for display using NSDateFormatter.
Constraints and uncertainty
NSCalendar and NSDateFormatter each accept an NSTimeZone independently. When the calendar's time zone differs from the formatter's time zone — for example, a calendar pinned to UTC while the formatter uses the device's local zone — component extraction can yield a day value that disagrees with the formatted string during daylight-saving transitions. The documentation does not prescribe a canonical pattern for keeping these two time zones in sync, and legacy codebases often mix GMT and UTC identifiers, which behave differently on older Foundation releases.
Questions
- What is the recommended strategy to guarantee that
NSCalendarcomponent extraction andNSDateFormatterstring output always agree on the calendar day for a givenNSDate? - Should the calendar's time zone always mirror the formatter's time zone, or is there a scenario where they must diverge?
- How does the
GMTversusUTCidentifier choice affect this alignment on macOS 10.14 and iOS 12?