Thursday, June 13, 2013

NSCalendar NSMinuteCalendarUnit example in Objective C (iOS).


NSCalendar NSMinuteCalendarUnit

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 NSMinuteCalendarUnit]
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 NSMinuteCalendarUnit example.
unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;
NSDateComponents *comps = [[NSCalendar currentCalendar] components:unitFlags fromDate:day1];
NSDate *day3 = [[NSCalendar currentCalendar] dateFromComponents:comps];

unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
comps = [[NSCalendar currentCalendar] components:unitFlags fromDate:day2];
day3 = [[NSCalendar currentCalendar] dateByAddingComponents:comps toDate:day3 options:0];

Example of [NSCalendar NSMinuteCalendarUnit].
// Seconds per day (24h * 60m * 60s)
#define kSecondsPerDay 86400.0f

+ (BOOL) dateIsToday:(NSDate*)dateToCheck
{
    // Split today into components
    NSCalendar* gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents* comps = [gregorian components:(NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit)
                                        fromDate:[NSDate date]];

    // Set to this morning 00:00:00
    [comps setHour:0];
    [comps setMinute:0];
    [comps setSecond:0];
    NSDate* theMidnightHour = [gregorian dateFromComponents:comps];
    [gregorian release];

    // Get time difference (in seconds) between date and then
    NSTimeInterval diff = [dateToCheck timeIntervalSinceDate:theMidnightHour];
    return ( diff>=0.0f && diff<kSecondsPerDay );
}

NSCalendar NSMinuteCalendarUnit example.
@implementation NSDate (Additions)

- (void)assignFlags:(NSUInteger)unitFlags to:(NSDateComponents *)components {
  NSDateComponents *myComponents =
        [[NSCalendar currentCalendar] components: unitFlags
                                        fromDate: self];
  if (unitFlags & NSYearCalendarUnit)
    [components setYear: [myComponents year]];
  if (unitFlags & NSMonthCalendarUnit)
    [components setMonth: [myComponents month]];
  if (unitFlags & NSDayCalendarUnit)
    [components setDay: [myComponents day]];
  if (unitFlags & NSHourCalendarUnit)
    [components setHour: [myComponents hour]];
  if (unitFlags & NSMinuteCalendarUnit)
    [components setMinute: [myComponents minute]];
  if (unitFlags & NSSecondCalendarUnit)
    [components setSecond: [myComponents second]];
}

- (NSDate *)dateByTakingComponents:(NSUInteger)unitFlags fromDate:(NSDate *)date {

  NSUInteger allFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit |
    NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;

  NSDateComponents *allComponents = [[NSDateComponents alloc] init];
  [date assignFlags: unitFlags to: allComponents];
  [self assignFlags: allFlags & ~unitFlags to: allComponents];

  NSDate *result = [[NSCalendar currentCalendar] dateFromComponents: allComponents];
  [allComponents release];

  return result;
}

End of NSCalendar NSMinuteCalendarUnit example article.