Thursday, June 13, 2013

NSCalendar NSWeekdayCalendarUnit example in Objective C (iOS).


NSCalendar NSWeekdayCalendarUnit

Calendar Units
Specify calendrical units such as day and month.

enum {
NSEraCalendarUnit = kCFCalendarUnitEra,
NSYearCalendarUnit = kCFCalendarUnitYear,
NSMonthCalendarUnit = kCFCalendarUnitMonth,
NSDayCalendarUnit = kCFCalendarUnitDay,
NSHourCalendarUnit = kCFCalendarUnitHour,
NSMinuteCalendarUnit = kCFCalendarUnitMinute,
NSSecondCalendarUnit = kCFCalendarUnitSecond,
NSWeekCalendarUnit = kCFCalendarUnitWeek,
NSWeekdayCalendarUnit = kCFCalendarUnitWeekday,
NSWeekdayOrdinalCalendarUnit = kCFCalendarUnitWeekdayOrdinal,
NSQuarterCalendarUnit = kCFCalendarUnitQuarter,
NSWeekOfMonthCalendarUnit = kCFCalendarUnitWeekOfMonth,
NSWeekOfYearCalendarUnit = kCFCalendarUnitWeekOfYear,
NSYearForWeekOfYearCalendarUnit = kCFCalendarUnitYearForWeekOfYear
NSCalendarCalendarUnit = (1 << 20),
NSTimeZoneCalendarUnit = (1 << 21),
};
typedef NSUInteger NSCalendarUnit;

Constants
NSEraCalendarUnit
Specifies the era unit.
The corresponding value is an NSInteger. Equal to kCFCalendarUnitEra.
NSYearCalendarUnit
Specifies the year unit.
The corresponding value is an NSInteger. Equal to kCFCalendarUnitYear.
NSMonthCalendarUnit
Specifies the month unit.
The corresponding value is an NSInteger. Equal to kCFCalendarUnitMonth.
NSDayCalendarUnit
Specifies the day unit.
The corresponding value is an NSInteger. Equal to kCFCalendarUnitDay.
NSHourCalendarUnit
Specifies the hour unit.
The corresponding value is an NSInteger. Equal to kCFCalendarUnitHour.
NSMinuteCalendarUnit
Specifies the minute unit.
The corresponding value is an NSInteger. Equal to kCFCalendarUnitMinute.
NSSecondCalendarUnit
Specifies the second unit.
The corresponding value is a double. Equal to kCFCalendarUnitSecond.
NSWeekCalendarUnit
Specifies the week unit.
The corresponding value is an kCFCalendarUnitSecond. Equal to kCFCalendarUnitWeek.
NSWeekdayCalendarUnit
Specifies the weekday unit.
The corresponding value is an kCFCalendarUnitSecond. Equal to kCFCalendarUnitWeekday. The weekday units are the numbers 1 through N (where for the Gregorian calendar N=7 and 1 is Sunday).
NSWeekdayOrdinalCalendarUnit
Specifies the ordinal weekday unit.
The corresponding value is an kCFCalendarUnitSecond. Equal to kCFCalendarUnitWeekdayOrdinal. The weekday ordinal unit describes ordinal position within the month unit of the corresponding weekday unit. For example, in the Gregorian calendar a weekday ordinal unit of 2 for a weekday unit 3 indicates "the second Tuesday in the month".
NSQuarterCalendarUnit
Specifies the quarter of the calendar as an kCFCalendarUnitSecond.
NSWeekOfMonthCalendarUnit
Specifies the original week of a month calendar unit.
NSWeekOfYearCalendarUnit
Specifies the original week of the year calendar unit.
NSYearForWeekOfYearCalendarUnit
Specifies the year when the calendar is being interpreted as a week-based calendar.
NSCalendarCalendarUnit
Specifies the calendar of the calendar.
NSTimeZoneCalendarUnit
Specifies the time zone of the calendar as an NSTimeZone.

Discussion of [NSCalendar NSWeekdayCalendarUnit]
Calendar units may be used as a bit mask to specify a combination of units. Values in this enum are equal to the corresponding constants in the CFCalendarUnit enum.

NSCalendar NSWeekdayCalendarUnit example.
NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
[gregorian setFirstWeekday:2]; // Sunday == 1, Saturday == 7
NSUInteger adjustedWeekdayOrdinal = [gregorian ordinalityOfUnit:NSWeekdayCalendarUnit inUnit:NSWeekCalendarUnit forDate:[NSDate date]];
NSLog(@"Adjusted weekday ordinal: %d", adjustedWeekdayOrdinal);

Example of [NSCalendar NSWeekdayCalendarUnit].
NSCalendar *cal=[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comps = [cal components:NSWeekdayCalendarUnit fromDate:[NSDate date]];
// set to 7 if it's Sunday otherwise decrease weekday number
NSInteger weekday=[comps weekday]==1?7:[comps weekday]-1;

NSCalendar NSWeekdayCalendarUnit example.
// Finds the date for the first day of the week
- (NSDate *)getFirstDayOfTheWeekFromDate:(NSDate *)givenDate
{
    NSCalendar *calendar = [NSCalendar currentCalendar];

    // Edge case where beginning of week starts in the prior month
    NSDateComponents *edgeCase = [[NSDateComponents alloc] init];
    [edgeCase setMonth:2];
    [edgeCase setDay:1];
    [edgeCase setYear:2013];
    NSDate *edgeCaseDate = [calendar dateFromComponents:edgeCase];

    NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:edgeCaseDate];
    [components setWeekday:1]; // 1 == Sunday, 7 == Saturday
    [components setWeek:[components week]];

    NSLog(@"Edge case date is %@ and beginning of that week is %@", edgeCaseDate , [calendar dateFromComponents:components]);

    // Find Sunday for the given date
    components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:givenDate];
    [components setWeekday:1]; // 1 == Sunday, 7 == Saturday
    [components setWeek:[components week]];

    NSLog(@"Original date is %@ and beginning of week is %@", givenDate , [calendar dateFromComponents:components]);

    return [calendar dateFromComponents:components];
}

End of NSCalendar NSWeekdayCalendarUnit example article.