Re: NSDateComponents question
Re: NSDateComponents question
- Subject: Re: NSDateComponents question
- From: Christopher Kane <email@hidden>
- Date: Tue, 1 Jul 2008 09:10:15 -0700
On Jul 1, 2008, at 1:08 AM, mmalc crawford wrote:
On Jun 30, 2008, at 10:27 AM, Jason Wiggins wrote:
Thanks mmalc for your reply. What you say makes sense. So comps is
the whole date (now) minus 3 days. I wasn't expecting that.
I should've made it clear what I was trying to achieve. I want to
set the start date to the start of the week, hence line 11. 3 was
just an arbitrary figure that happens to be today (localised in
Sydney at 3:20am)
I want to get the weekday and subtract *that* from the current
date. I can't get a weekday figure without line 7. Any suggestions?
Assuming you want just the actual day of the beginning of the week,
rather than a specific time on the day (and assuming a Gregorian
calendar and that you want the week to start on a Sunday):
NSDate *currentDate = [NSDate date];
/*
Get the components required to:
(a) determine the current day of the week, and
(b) create the day of the beginning of the week.
*/
NSDateComponents *components = [gregorian
components:NSWeekdayCalendarUnit | NSYearCalendarUnit |
NSMonthCalendarUnit | NSDayCalendarUnit fromDate:currentDate];
/*
Update the components to represent the beginning of the week by
subtracting the weekday number from the current day.
Weekday for Sunday in the Gregorian calendar is 1, so subtract 1
from the number of days you want to subtract from the date in
question. (If today's Sunday, subtract 0 days.)
*/
[components setDay:([components day] - ([components weekday] -
1))];
It is probably a little bit better to also set the weekday to
undefined before the next computation:
[components setWeekday: NSUndefinedDateComponent];
so that the dateFromComponents: operation doesn't try to use the
weekday info, which now is not in agreement with the day, so "who
knows what might happen".
The more subtle and serious issue is that Sunday in the current week
might be in the previous Month (so, Day < Weekday), or even previous
Year, so setting the Day to a negative value and keeping the Month and
Year the same will have ... "who knows what effect".
A more robust approach would be to just get the weekday value:
NSDateComponents *components = [gregorian
components:NSWeekdayCalendarUnit fromDate: currentDate];
and subtract WD-1 days, e.g. (in the most straightforward way):
NSDateComponents *toSubtract = [[NSDateComponents new]
autorelease];
[toSubtract setDay: - ([components weekday] - 1)];
NSDate * beginningOfWeek = [currentDate dateByAddingComponents:
toSubtract toDate: currentDate options:0];
That gets you to Sunday, at least, though not necessarily the first
day of the week in the user's current locale.
Note that in this case, beginningOfWeek has the same hour, minute, and
second as the original date currentDate. Another go with
components:fromDate: and dateFromComponents: is one straightforward
way to normalize those to midnight:
components = [gregorian components: NSYearCalendarUnit |
NSMonthCalendarUnit | NSDayCalendarUnit fromDate: beginningOfWeek];
beginningOfWeek = [gregorian dateFromComponents: components];
Fetching hour, minute, and second into the original 'components' and
subtracting them would have been another way, though DST transitions
might be a "gotcha" there, spoiling that approach (you'd subtract one
too many or few hours to actually get back to midnight). Probably
better to explicitly state what you want in that case (ie, use
dateFromComponents: not dateByAddingComponents:...).
Chris Kane
Cocoa Frameworks, Apple
// Create the day for the beginning of the week from the updated
components.
NSDate *beginningOfWeek = [gregorian
dateFromComponents:components];
mmalc
_______________________________________________
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