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

Fiscal Months Calendar #225

Open
Astrovic opened this issue Oct 17, 2012 · 3 comments
Open

Fiscal Months Calendar #225

Astrovic opened this issue Oct 17, 2012 · 3 comments

Comments

@Astrovic
Copy link

I noticed that there is a problem with the fiscal calendar, maybe the dates are different from those of the Apple fiscal calendar? The amounts do not coincide with those received.

@AlessandroPappalardo
Copy link

The problem is December 2011 for the Apple has 5 weeks instead of 4 as expected.
Here is a change made ​​by me on the fly to solve:
init() in AppleFiscalCalendar.m

- (id)init
{
    self = [super init];
    if (self) {

        NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
        [calendar setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
        NSDateComponents *firstDateComponents = [[[NSDateComponents alloc] init] autorelease];
        // ORIGINAL: (SATURDAY) 29 September 2007
        [firstDateComponents setMonth:9];
        [firstDateComponents setDay:29];
        [firstDateComponents setYear:2007];
        NSDate *firstDate = [calendar dateFromComponents:firstDateComponents];

        NSDateComponents *components5Weeks = [[[NSDateComponents alloc] init] autorelease];
        [components5Weeks setWeek:5];
        NSDateComponents *components4Weeks = [[[NSDateComponents alloc] init] autorelease];
        [components4Weeks setWeek:4];

        NSMutableArray *dates = [NSMutableArray array];
        NSDate *currentDate = firstDate;
        int period = 0;


        NSLog(@"First Date: %@",firstDate);

        //DATE TO CHECK NOVEMBER 2011
        NSDateComponents *dateNovember2011Components = [[[NSDateComponents alloc] init] autorelease];
        [dateNovember2011Components setMonth:11];
        [dateNovember2011Components setDay:26];
        [dateNovember2011Components setYear:2011];
        NSDate *dateNovember2011 = [calendar dateFromComponents:dateNovember2011Components];

        //Covers the fiscal calendar from 2008 to 2016:
        while (period < 100) {
            //First month in a quarter covers 5 weeks, the others 4:
            NSDate *nextDate;

            if ([currentDate isEqualToDate:dateNovember2011]){ // December 2011 has 5 weeks
                nextDate = [calendar dateByAddingComponents:components5Weeks toDate:currentDate options:0];
            }else{
                nextDate = [calendar dateByAddingComponents:((period % 3 == 0) ? components5Weeks : components4Weeks) toDate:currentDate options:0];
            }
            NSLog(@"Next Date: %@",nextDate);
            [dates addObject:nextDate];
            currentDate = nextDate;
            period++;
        }

        NSMutableArray *names = [NSMutableArray array];

        NSDateFormatter *sectionTitleFormatter = [[NSDateFormatter new] autorelease];
        [sectionTitleFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
        [sectionTitleFormatter setDateFormat:@"MMMM yyyy"];

        for (NSDate *date in dates) {
            // Name of the fiscal month can be reliably found by the calendar month of a day 2 weeks after the fiscal month begins
            NSDateComponents *components = [NSDateComponents new];
            [components setDay:14];
            NSDate *result = [calendar dateByAddingComponents:components toDate:date options:0];
            [components release];
            NSString *fiscalMonthName = [sectionTitleFormatter stringFromDate:result];
            [names addObject:fiscalMonthName];
        }
        sortedFiscalMonthNames = [[NSArray alloc] initWithArray:names];
        sortedDates = [[NSArray alloc] initWithArray:dates];
    }
    return self;
}

@Astrovic
Copy link
Author

Thanks a lot, now it works fine! :)

@ddaddy
Copy link

ddaddy commented Oct 26, 2012

This is great, thanks.
You should submit a pull request for this.

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

3 participants