We know that NSDate is UTC/GMT. In order words, no time zone info is associated with it.
There are high level classes such as NSCalendar and NSTimeZone and NSDateComponents which do take timezone into account.
Based on that, I am using the following code to calculate the difference in days between 2 dates. However the result I am getting seems as the calculation is done ignoring the time altogether. I am wondering if I am doing something wrong or if this is by design for some reason.
Here is the code and data:
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comp = [calendar components:NSDayCalendarUnit fromDate:startDate toDate:stopDate options:0];
NSInteger day = [comp day];
Debug output:
(gdb) po startDate
2011-04-01 03:52:13 +0000
(gdb) po stopDate
2011-04-03 15:52:13 +0000
(gdb) p (int) day
$1 = 2
The timezone is Pacific Standard Time and the dates above converted to PST are:
startDate = 2011-03-31 8:52:13 -7
stopDate = 2011-04-03 8:52:13 -7
Therefore I would expect 4 days between them, not 2. (31st, 1st, 2nd, and 3rd)
The reason I think the hours are ignored is because if hours are added to the calculations we get 2 days and 12 hours.
Am I doing something wrong or is this how this is designed to work?
Thank you, Vance