• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSDateComponents question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSDateComponents question


  • Subject: Re: NSDateComponents question
  • From: Jason Wiggins <email@hidden>
  • Date: Wed, 2 Jul 2008 10:20:17 +1000

It look like what I thought was going to be something simple, has turned out to be less so and fraught with danger programatically.
I was just thinking though, would it be easier just to covert the weekday value into seconds (weekday * 60 * 60 * 24) and subtract that from the current date? This would save you from the issue of year and month boundaries. Following code is untested (written in Mail):


NSDateComponents *components = [[NSDateComponents alloc] init];
NSDate *startOfTheWeek;
NSDate *now = [NSDate date];
components = [calendar components:NSWeekdayCalendarUnit fromDate:now];
startOfTheWeek = [now addTimeInterval:-([components weekday] * 60 * 60 * 24)];
NSLog(@"The start of the week is %@", startOfTheWeek);


JJ



On 02/07/2008, at 4:37 AM, mmalc crawford wrote:


On Jul 1, 2008, at 10:44 AM, Chris Kane wrote:

My apologies; I did do some testing and found that the resultant date was always correct.
Could you elaborate on what circumstances this might not be correct?
Did you try starting with a starting date of Jan 1, 2009? The Weekday would be 5, the Day 1.

Rats, no; I tried the dates at the beginning of 2008, 2003, and 1999...

The previous Sunday is December 28, 2008.

... although, hmm, yes, that's the result I get(*).  Nevertheless...

Passing a Year, Month, Day of (2009, 1, -3) (-3 == 1 - (5 - 1) in your original computation) is passing an out-of-bounds value with dateWithComponents:, with who-knows-what effect. It might be well- defined, it might not be. The result might change between OS releases. In other words, it seems a bit ambiguous, so best to just avoid it.

... point taken.  I'll update the documentation accordingly.

mmalc


(*) NSDateComponents *components = [[NSDateComponents alloc] init]; [components setYear:2009]; [components setMonth:1]; [components setDay:1]; NSDate *testDate = [gregorian dateFromComponents:comps]; [components release];

NSDateComponents *components = [gregorian components:NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:testDate];

NSLog(@"day: %d, weekDay: %d, delta: %d", [components day], [components weekday], [components day] - [components weekday]);
// day: 1, weekDay: 5, delta: -4


[components setDay:([components day] - ([components weekday] - 1))];
[components setWeekday:NSUndefinedDateComponent];


NSDate *beginningOfWeek = [gregorian dateFromComponents:components];
NSLog(@"beginningOfWeek: %@", beginningOfWeek);
// beginningOfWeek: 2008-12-28 00:00:00 -0800


_______________________________________________

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

_______________________________________________

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


  • Follow-Ups:
    • Re: NSDateComponents question
      • From: mmalc crawford <email@hidden>
References: 
 >Re: NSDateComponents question (From: Jason Wiggins <email@hidden>)
 >Re: NSDateComponents question (From: mmalc crawford <email@hidden>)
 >Re: NSDateComponents question (From: Christopher Kane <email@hidden>)
 >Re: NSDateComponents question (From: mmalc crawford <email@hidden>)
 >Re: NSDateComponents question (From: Chris Kane <email@hidden>)
 >Re: NSDateComponents question (From: mmalc crawford <email@hidden>)

  • Prev by Date: Re: exec(uting) Safari - How (newbie)
  • Next by Date: Re: NSDateComponents question
  • Previous by thread: Re: NSDateComponents question
  • Next by thread: Re: NSDateComponents question
  • Index(es):
    • Date
    • Thread