Am I missing something? It seems that the method provided by Apple only works for UTC, regardless of the default time for the machine or what you installed it on.
Here's the output I get:
Output: 2013-02-01 10:41:24.152 Scratch[17640:c07] cal=gregorian, cal.timeZone=America/Los_Angeles (PST) offset -28800 2013-02-01 10:41:24.154 Scratch[17640:c07] date_Feb1_1400PST=2013-02-01 14:00 -0800 2013-02-01 10:41:24.156 Scratch[17640:c07] date_Feb2_1200PST=2013-02-02 12:00 -0800 2013-02-01 10:41:24.157 Scratch[17640:c07] midnights between=1 2013-02-01 10:41:24.158 Scratch[17640:c07] and then... 2013-02-01 10:41:24.159 Scratch[17640:c07] date_Feb1_2000PST=2013-02-01 22:00 -0800 2013-02-01 10:41:24.161 Scratch[17640:c07] date_Feb2_1000PST=2013-02-02 10:00 -0800 2013-02-01 10:41:24.161 Scratch[17640:c07] midnights between=0
What I really want to know is “how many midnight” (ie how many days on the calendar) between two days for a given time zone (local or otherwise, not necessarily UTC)
This seems like such a general and reasonably simple question that I am surprised to see how disorderly and difficult to understand.
I'm not looking for an answer that includes "mod 86400" or something so dirty. The structure should be able to tell me this seriously.
- (void)doDateComparisonStuff { NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; cal.timeZone = [NSTimeZone timeZoneWithName:@"America/Los_Angeles"]; NSLog(@"cal=%@, cal.timeZone=%@", cal.calendarIdentifier, cal.timeZone); NSDate *date_Feb1_1400PST = [self dateFromStr:@"20130201 1400"]; NSLog(@"date_Feb1_1400PST=%@", [self stringFromDate:date_Feb1_1400PST]); NSDate *date_Feb2_1200PST = [self dateFromStr:@"20130202 1200"]; NSLog(@"date_Feb2_1200PST=%@", [self stringFromDate:date_Feb2_1200PST]); NSLog(@"midnights between=%d", [self daysWithinEraFromDate:date_Feb1_1400PST toDate:date_Feb2_1200PST usingCalendar:cal]); NSLog(@"and then..."); NSDate *date_Feb1_2000PST = [self dateFromStr:@"20130201 2200"]; NSLog(@"date_Feb1_2000PST=%@", [self stringFromDate:date_Feb1_2000PST]); NSDate *date_Feb2_1000PST = [self dateFromStr:@"20130202 1000"]; NSLog(@"date_Feb2_1000PST=%@", [self stringFromDate:date_Feb2_1000PST]); NSLog(@"midnights between=%d", [self daysWithinEraFromDate:date_Feb1_2000PST toDate:date_Feb2_1000PST usingCalendar:cal]); }
source share