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

January not shown for some years in en_GB #18

Closed
percysnoodle opened this issue Mar 16, 2013 · 1 comment
Closed

January not shown for some years in en_GB #18

percysnoodle opened this issue Mar 16, 2013 · 1 comment

Comments

@percysnoodle
Copy link
Contributor

I have a calendar view which I set up like so:

        _calendarView = [[TSQCalendarView alloc] init];
        _calendarView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        _calendarView.delegate = self;
        _calendarView.rowCellClass = [C3CCalendarRowCell class];
        _calendarView.pagingEnabled = YES;
        _calendarView.firstDate = [NSDate dateWithTimeIntervalSince1970:0];
        _calendarView.lastDate = [NSDate dateWithTimeIntervalSince1970:(36525 + 364) * kSecondsInADay];
        _calendarView.selectedDate = selectedDate;
        [self.contentView addSubview:_calendarView];

C3CCalendarRowCell just defines some images, as per the example app. When I scroll to the start of 2010, I see this:

Screen Shot 2013-03-16 at 17 51 33

Looking in the debugger, I see that something weird is going in inside -[tableView:numberOfRowsInSection:]. firstWeek is 3, but nextToLastWeek is 53, so that section has minus 47 rows.

Could this be because my calendar locale is en_GB? It's possible we have different rules about week numbers in Britain; I have no idea.

@percysnoodle
Copy link
Contributor Author

Looking further into this, it also affects 2011 and 2012, but not 2009 or 2008. Not sure what makes them different.

I notice there's a comment in the code:

    // -[NSDateComponents weekOfYear] doesn't explicitly specify what its valid range is. In the Gregorian case, it appears to be [1,52], which means the last day of December is probably going to be week 1 of next year. The same logic extends to other calendars.

so I added this code to -[tableView:numberOfRowsInSection:] to test out the range methods on NSCalendar:

    if ((section == 480) || (section == 504))
    {
        NSLog(@"section %d", section);
        NSLog(@"firstWeek %d", firstWeek);
        NSLog(@"nextToLastWeek %d", nextToLastWeek);
        NSRange range;

        range = [self.calendar minimumRangeOfUnit:NSWeekOfYearCalendarUnit];
        NSLog(@"minimumRangeOfUnit:[%d, %d]", range.location, range.location + range.length - 1);

        range = [self.calendar maximumRangeOfUnit:NSWeekOfYearCalendarUnit];
        NSLog(@"maximumRangeOfUnit:[%d, %d]", range.location, range.location + range.length - 1);

        range = [self.calendar rangeOfUnit:NSWeekOfYearCalendarUnit inUnit:NSYearCalendarUnit forDate:firstOfMonth];
        NSLog(@"rangeOfUnit:inUnit:[%d, %d]", range.location, range.location + range.length - 1);
    }

I got this output:

2013-03-16 18:42:31.324 MyApp[95353:c07] section 480
2013-03-16 18:42:31.324 MyApp[95353:c07] firstWeek 53
2013-03-16 18:42:31.324 MyApp[95353:c07] nextToLastWeek 3
2013-03-16 18:42:31.324 MyApp[95353:c07] minimumRangeOfUnit:[1, 52]
2013-03-16 18:42:31.324 MyApp[95353:c07] maximumRangeOfUnit:[1, 53]
2013-03-16 18:42:31.324 MyApp[95353:c07] rangeOfUnit:inUnit:[0, 52]
2013-03-16 18:42:31.325 MyApp[95353:c07] section 504
2013-03-16 18:42:31.326 MyApp[95353:c07] firstWeek 52
2013-03-16 18:42:31.326 MyApp[95353:c07] nextToLastWeek 4
2013-03-16 18:42:31.326 MyApp[95353:c07] minimumRangeOfUnit:[1, 52]
2013-03-16 18:42:31.326 MyApp[95353:c07] maximumRangeOfUnit:[1, 53]
2013-03-16 18:42:31.326 MyApp[95353:c07] rangeOfUnit:inUnit:[0, 53]

Hopefully that's useful in figuring out the valid range. I also found that I can work around the problem by adding this just before the return:

    if (firstWeek > nextToLastWeek)
    {
        // The first week must have been the end of last year.
        firstWeek = 0;
    }

but I'm not sure I understand what I've done well enough to say that that's fixed the problem. I could still put it in a pull request, if you'd like (I've signed the CLA for KIF - do I need to sign it again for TS?).

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

1 participant