Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't advance to February when Jan 29 or 30 is selected. #33

Closed
markkrenek opened this issue Jan 29, 2014 · 4 comments
Closed

Can't advance to February when Jan 29 or 30 is selected. #33

markkrenek opened this issue Jan 29, 2014 · 4 comments

Comments

@markkrenek
Copy link

In CKCalendarView, if Jan 29th or 30th is selected and you tap the forward button, you do not advance to February. Instead, Jan 30th is selected.

The problem is this calculation in forwardTapped:

    //  Otherwise, add a month and then go to the first of the month
    else{
        NSUInteger day = [[self calendar] daysInDate:date];
        date = [[self calendar] dateByAddingMonths:1 toDate:date];              //  Add a month
        date = [[self calendar] dateBySubtractingDays:day-1 fromDate:date];     //  Go to the first of the month
    }
@MosheBerman
Copy link
Owner

Thanks for the feedback. Do you have a fix that you'd like to suggest?

@markkrenek
Copy link
Author

The source of the bug is the use of dateBySubtractingDays, passing it the number of days in the current month. Since all months don't have the same number of days, you end up with problems.

I had my own category on NSDate that performed various date calculations, so I used them to get around this. Here is that solution. Instead of the 3 lines noted above, use this:

date = [[date dateAtStartOfMonth] dateByAddingNumberOfMonths:1];

where dateAtStartOfMonth and dateByAddingNumberOfMonths are defined like this:

- (NSDate*) dateAtStartOfMonth
{
    NSDate* startOfMonth = nil;
    NSTimeInterval interval = 0;
    BOOL ok = [[NSCalendar currentCalendar] rangeOfUnit:NSMonthCalendarUnit startDate:&startOfMonth interval:&interval forDate:self];
    if (ok)
    {
        if ([self isEqualToDate:startOfMonth])
            return self;
        else
            return startOfMonth;
    }
    return NULL;
}

- (NSDate*) dateByAddingNumberOfMonths:(NSInteger)months
{
    NSDateComponents* components = [[NSDateComponents alloc] init];
    [components setMonth:months];
    NSDate* newDate = [[NSCalendar currentCalendar] dateByAddingComponents:components toDate:self options:0];
    return newDate;
}

I didn't delve into it enough to figure out if there was a fix using the NSCalendar+DateManipulation category.

@MosheBerman
Copy link
Owner

Your comment noting that "all months don't have the same number of days" was on target. I believe that ticking the month forward first before subtracting the days fixes the problem. The logic of "subtract x days of the month except one" should work, but x days of the month needs be calculated using the target month, not the month we're jumping from.

The original code looked like this, as you noted above:

        NSUInteger day = [[self calendar] daysInDate:date];
        date = [[self calendar] dateByAddingMonths:1 toDate:date];              //  Add a month
        date = [[self calendar] dateBySubtractingDays:day-1 fromDate:date];     //  Go to the first of the month

The reality is that it should look like this:

            date = [[self calendar] dateByAddingMonths:1 toDate:date];              //  Add a month
            NSUInteger day = [[self calendar] daysInDate:date];                     //  Only then go to the first of the next month.
            date = [[self calendar] dateBySubtractingDays:day-1 fromDate:date];

See the difference? It seems to solve the problem.

@MosheBerman
Copy link
Owner

Fixed in 8cdc4ed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants