Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Fiscal Months Calendar #225

Open
Astrovic opened this Issue · 3 comments

3 participants

@Astrovic

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

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

Thanks a lot, now it works fine! :)

@ddaddy

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

@ddaddy ddaddy referenced this issue from a commit in ddaddy/AppSales-Mobile
Darren Jones Corrected Fiscal Calendar thanks to AlessandroPappalardo. Issue #225 fb14f9b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.