WenchaoD / FSCalendar

A fully customizable iOS calendar library, compatible with Objective-C and Swift

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Incorrect index calculation when calendar.firstWeekday = 0 (Saturday)

yccheok opened this issue · comments

Hi,

I would like to point out incorrect index calculation, when calendar.firstWeekday = 0 (Saturday)

// 0 = Starts at Saturday
// 1 = Starts at Sunday
// 2 = Starts at Monday
calendar.firstWeekday = 0

Based on 2.8.3

https://github.com/WenchaoD/FSCalendar/blob/2.8.3/FSCalendar/FSCalendarWeekdayView.m#L106

When i is 0 and calendar.firstWeekday, i will end up as 1 during 1st iteration. This is the unexpected outcome, when "Mon" is displayed instead of "Sat"

BUG

I would like to propose the following fix, by changing

NSInteger index = (i + self.calendar.firstWeekday-1) % 7;

to

NSInteger index = (i + self.calendar.firstWeekday-1);

if (index < 0) {
    // Avoid index from becoming -ve, by making it exhibits circular behavior.
    // index -1 will be translated to index 6.
    // index -2 will be translated to index 5.
    // ...
    index = 7 + index;
}

// If index is 7, translate it back to 0.
index = index % 7;

Here's the correct outcome

FIX

Thank you very much.

Actually 1 means Sunday, while 2 means Monday and so on.

Sorry. Now only I realize calendar.firstWeekday = 7 means Saturday. My bad.