NSCalendar dateByAddingComponents: yields inconsistent results
NSCalendar dateByAddingComponents: yields inconsistent results
- Subject: NSCalendar dateByAddingComponents: yields inconsistent results
- From: Jim Thomason <email@hidden>
- Date: Sat, 19 Feb 2011 13:16:51 -0600
I'm utterly stumped.
I haven't managed to boil this down to a succinct test case yet. It
only intermittently appears, basically, after I end up doing a "lot"
of date calculations on a separate thread. For some nebulous value of
a "lot".
The gyst is this - on occasion, NSCalendar's
dateByAddingComponents:toDate:options: is returning a nonsense value.
With the sample data I'm working with, I'm just creating a date
component with day value of 1 and everything else explicitly
initialized to zero.
For whatever it's worth, here's the method I'm using:
-(NSDate*) dateByAddingYears:(NSInteger) years months:(NSInteger)
months days:(NSInteger) days toDate:(NSDate*) date {
NSDateComponents* delta = [NSDateComponents
deltaDateComponentWithYears:years months:months days:days];
NSDate* rv = [[self gmtCalendar] dateByAddingComponents:delta
toDate:date options:0];
if ([date compare:rv] == NSOrderedDescending && years == 0 && months
== 0 && days == 1) {
NSLog(@"TOTAL FAILURE"); //breakpoint here
}
return rv;
}
Sometimes (not even close to always, or even often), I bomb into the
total failure log statement and stop at the breakpoint.
At that point, inspection in the debugger yields nonsense results:
(gdb) print-object date
2013-10-23 00:00:00 +0000
(gdb) print-object rv
4713-02-19 00:00:00 +0000
(gdb) print (int) years
$4 = 0
(gdb) print (int) months
$5 = 0
(gdb) print (int) days
$6 = 1
(gdb) print (int) [delta year]
$7 = 0
(gdb) print (int) [delta month]
$8 = 0
(gdb) print (int) [delta day]
$9 = 1
(gdb) print-object [[self gmtCalendar] dateByAddingComponents:delta
toDate:date options:0]
2013-10-24 00:00:00 +0000
the return value ("rv") is inconsistent - in this case it happens to
show up in the year 4713. It frequently shows up at year 0, but again,
not always.
As you can see, I'm handing in 0,0,1, and my NSDateComponents object
ends up with just a change of +1 days.
Finally, note that if I try creating a new date, it succeeds. Further,
it -always- succeeds this second time in the debugger.
My gmtCalendar and deltaDateComponents:... are just wrapper methods -
I return a static NSCalendar set to GMT, and a pre-existing
NSDateComponents if I've already made one. Those are static class
variables that get looked up.
It almost seems to be related to my gmtCalendar method. If I take it
out, and instead just create a new one each time, I can't make it fail
here. When I put it back in, it will fail occasionally as before. I
don't see anything suspect with my gmt method.
Here's my gmtCalendar method:
static NSCalendar* gmtCalendar = nil;
-(NSCalendar*) gmtCalendar {
@synchronized(self) {
if (gmtCalendar == nil) {
NSLog(@"NEEDS NEW GMT!");
NSCalendar* newGMTCalendar = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
[newGMTCalendar setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
gmtCalendar = newGMTCalendar;
}
}
return gmtCalendar;
}
I'm completely stumped for ideas. Has anybody ever seen any odd
behavior like this with NSDateComponents?
-Jim....
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden