On Jan 5, 2010, at 1:02 pm, Quincey Morris wrote:
> The OP wanted to use NSDate objects to avoid the clunkiness of multiple
> values/variables with NSDateComponents. I'm arguing that avoiding
> NSDateComponents is a *lot* more work, and that using NSDate for this purpose
> is *very* easy to get wrong.
>
The issue with NSDateComponents is that they must be understood in the context
of a particular calendar.
An NSDate object represent a single point in time -- you can think of it
basically as a wrapper for an NSTimeInterval from the reference date. If you
want to create components from the date, then you must do so with respect to a
particular calendar *and time zone*... This is of course possible, but then
you have to be careful about always using the same combination of calendar and
time zone to create the components and recreate the date from the components.
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
[gregorian setTimeZone:[NSTimeZone
timeZoneWithName:@"America/Los_Angeles"]];
NSDateComponents *components1 = [[NSDateComponents alloc] init];
[components1 setWeekday:2];
[components1 setWeekdayOrdinal:1];
[components1 setMonth:5];
[components1 setYear:2008];
NSDate *date1 = [gregorian dateFromComponents:components1];
NSLog(@"date1: %@", date1);
[gregorian setTimeZone:[NSTimeZone timeZoneWithName:@"Pacific/Fiji"]];
date1 = [gregorian dateFromComponents:components1];
NSLog(@"date1: %@", date1);
mmalc
_______________________________________________
Cocoa-dev mailing list ([email protected])
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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com
This email sent to [email protected]