-
Notifications
You must be signed in to change notification settings - Fork 407
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
Comments
The problem is December 2011 for the Apple has 5 weeks instead of 4 as expected. - (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;
} |
Thanks a lot, now it works fine! :) |
This is great, thanks. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
The text was updated successfully, but these errors were encountered: