Question

Wonder if I've found a bug with NSDateComponents.

When using NSDateComponents to calculate dates and switching between time zones, it's possible to get the wrong date on the simulator (not tested on device).

It seems that it's the right offset, but going in the wrong direction. Either that, or I'm fundamentally misunderstanding something (which is always a distinct possibility).

Sample:

Set the system time to a time zone other than UTC (mine was Mexico, UTC-05:00)

// Get the components for the current absolute time.
NSDateComponents *components = [[NSCalendar currentCalendar] components:(NSYearCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit | NSMinuteCalendarUnit | NSHourCalendarUnit | NSSecondCalendarUnit) fromDate:[NSDate date]];

// Get the calendar, which seems to set the time zone to the current time zone
components.calendar = [NSCalendar currentCalendar];
// Set the time zone to UTC
components.calendar.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];

// Log the date
NSLog(@"Date at time zone: %@ : %@", components.calendar.timeZone, [components date]);
// Outputs (e.g): "Date at time zone: GMT (GMT) offset 0 : 2013-06-21 14:55:56 +0000"
// This should be absolute time, so any changes in the time zone should offset
// relative to this.

// Set the time zone to the local time zone (e.g. Mexico, 5 hours behind)
components.calendar.timeZone = [NSTimeZone localTimeZone];
// Log the date
NSLog(@"Date at time zone: %@ : %@", components.calendar.timeZone, [components date]);
// Outputs (e.g.): "Date at time zone: Local Time Zone (America/Mexico_City (CDT) offset -18000 (Daylight)) :2013-06-21 19:55:56 +0000"

Assuming Mexico, shouldn't the second date be five hours behind the first?

Was it helpful?

Solution

After you have set components.calendar.timeZone to the Mexican time zone, components represents (in your example)

2013-06-21 14:55:56 in the Mexican time zone

therefore [components date] returns an NSDate for that point in time.

The NSLog output uses the description method of NSDate, and that prints the point in time always in the GMT time zone, which is

2013-06-21 19:55:56 +0000

So everything looks OK to me.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top