Thursday, June 13, 2013

NSCalendar NSTimeZoneCalendarUnit example in Objective C (iOS).


NSCalendar NSTimeZoneCalendarUnit

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 NSTimeZoneCalendarUnit]
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 NSTimeZoneCalendarUnit example.
// Now, an absolute date and time that represent now all around the world, that is made to play with
NSDate *now = [NSDate date];

// A specific calendar for a specific place in the world
NSCalendar* parisCalendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
[parisCalendar setTimeZone:[NSTimeZone timeZoneWithName:@"Europe/Paris"]];

// Now components seen from Paris
NSDateComponents* componentsNowInParis = [parisCalendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit|NSTimeZoneCalendarUnit fromDate:now];

// Tricking a copy of "Now components seen from Paris" to force 15:00:00, in Paris
NSDateComponents* componentsInParisAt15 = [[componentsNowInParis copy] autorelease];
[componentsInParisAt15 setHour:15];
[componentsInParisAt15 setMinute:0];
[componentsInParisAt15 setSecond:0];

// Getting an universal date reference that represent what could be 15:00:00 seen from paris, Or 19:00:00 from GMT+4
NSDate* dateAt15 = [parisCalendar dateFromComponents:componentsInParisAt15];

// We now have two universal dates that can be compared each other
// If "now" is 16:00:00, those date will show a 60 minutes difference all around the world
NSLog(@"%@", now);
NSLog(@"%@", dateAt15);

Example of [NSCalendar NSTimeZoneCalendarUnit].
NSDate *newDate;
NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:~NSTimeZoneCalendarUnit fromDate:[NSDate date]];

newDate = [[NSCalendar currentCalendar] dateFromComponents:dateComponents];
NSLog(@"newDate: %@", newDate);
NSLog(@"newDate: %.0f", [newDate timeIntervalSinceReferenceDate]);

NSCalendar NSTimeZoneCalendarUnit example.
- (NSArray *)createPaymentScheduleWithStartDate:(NSDate *)startDate numberOfPayments:(NSUInteger)payments {
    NSUInteger count = 0;
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSMutableArray *schedule = [NSMutableArray arrayWithCapacity:payments];

    // break down the start date for our future payments
    unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit
            | NSTimeZoneCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit;
    NSDateComponents *comps = [cal components:unitFlags fromDate:startDate];
    NSInteger month = [comps month];
    NSInteger day = [comps day];
    NSInteger year = [comps year];

    // For simplicity sake, adjust day larger than 28
    // this way we don't have to test each time we create a date
    // if it valid.
    if (day > 28)
        day = 28;

    // NSDate *a = startDate;
    // because we want all payments to have the same time
    // create the first again
    NSDate *a = [cal dateFromComponents:comps];

    do {
        month++;
        if (month > 12) {
            year++;
            month = 1;
        }

        // create the next month payment date
        comps.month = month;
        comps.day = day;
        comps.year = year;
        NSDate *b = [cal dateFromComponents:comps];

        // find the middle date
        NSTimeInterval m = [b timeIntervalSinceDate:a];
        NSDate *c = [a dateByAddingTimeInterval:m / 2];

        // add dates to our schedule array
        [schedule addObject:a];
        [schedule addObject:c];
        count += 2;

        // adjust to next group of payments
        a = b;

    } while (count < (payments + 5));


    // because we add two payments at a time,
    // we have to adjust that extra payment
    if ([schedule count] > payments) {
        // create range of our excess payments
        NSRange range = NSMakeRange(payments, [schedule count] - payments);
        [schedule removeObjectsInRange:range];
    }

    NSLog(@"Schedule: %@", schedule);
    return [NSArray arrayWithArray:schedule];
}

End of NSCalendar NSTimeZoneCalendarUnit example article.