NSCalendar date calculation anomaly
NSCalendar date calculation anomaly
- Subject: NSCalendar date calculation anomaly
- From: Scott Ribe <email@hidden>
- Date: Tue, 27 Apr 2010 21:45:07 -0600
I have an int representing the number of days since 1/1/2001, and wish to get an NSDate representing that date in the system's local time zone:
NSDateComponents *dc = [[[NSDateComponents alloc] init] autorelease];
[dc setDay: numdays];
NSDate * cd = [[NSCalendar currentCalendar] dateByAddingComponents: dc toDate: [NSDate dateWithString: @"2001-01-01"] options: 0];
NSLog( @"date 1: %@", [cd description] );
[dc setDay: 0];
[dc setSecond: -[[NSTimeZone systemTimeZone] secondsFromGMTForDate: cd]];
cd = [[NSCalendar currentCalendar] dateByAddingComponents: dc toDate: cd options: 0];
NSLog( @"date 2: %@", [cd description] );
If for example numdays is the number of days from 1/1/2001 to 5/4/2010, then the output is this:
2010-04-27 21:33:55.803 PedCard[95387:a0f] date 1: 2010-05-03 17:00:00 -0600
2010-04-27 21:33:55.804 PedCard[95387:a0f] date 2: 2010-05-03 23:00:00 -0600
And, BTW, my current time zone is MDT, so -0600 is not correct, it should be -0700. So what's going on? I would expect those dates to be:
2010-05-03 17:00:00 -0700
2010-05-04 00:00:00 -0700
It seems as though some calls are using one time zone and some are using another??? I do not do anything in my app to set a local or default time zone, and I have not changed the time zone in my system preferences in a very long time. (Multiple reboots ago.) Add to that, the code did produce the correct date a few times earlier tonight... Also, it doesn't seem to make a difference whether I use systemTimeZone or localTimeZone.
OS X 10.6.3, Xcode 3.2.2
--
Scott Ribe
email@hidden
http://www.elevated-dev.com/
(303) 722-0567 voice
_______________________________________________
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