iPhone NSCalendar - Incorrect Calendar Drawn for users outside US
After trying to work on someone else's code, and so many different iterations, I am unable to understand what am I doing wrong with dates on a calendar month view. I am trying to present a calendar in month view fashion like calendar.app but unfortunately some users in countries like Australia, UK are reporting incorrect alignment of Days to Dates. For instance in Australia it displays 17th March as Friday. Any help is highly appreciated!
Here is the code sample I am using:
-(void)setCalendar
{
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comps = [gregorian components:units fromDate:[NSDate date]];
self.currentDate = [gregorian dateFromComponents:comps];
NSInteger weekdayInteger=[comps weekday];
currentMonth=[comps month];
currentYear=[comps year];
NSString *dateString=[[NSString stringWithFormat:@"%@",currentDate] substringToIndex:10];
NSArray *array= [dateString componentsSeparatedByString:@"-"];
NSInteger currentDay=[[array objectAtIndex:2] intValue];
currentMonthConstant=currentMonth;
currentYearConstant=currentYear;
currentDayConstant=currentDay;
currentConstantDate=[NSString stringWithFormat:@"%d/%d/%d",currentDayConstant,currentMonthConstant,currentYearConstant];
currentFirstDay=((8+weekdayInteger-(currentDay%7))%7);
if(currentFirstDay==0)
currentFirstDay=7;
[gregorian release];
}
And this is how im populating the calendar:
-(void)fillCalender:(NSInteger)month weekStarts:(NSInteger)weekday year:(NSInteger)year
{
currentMonth=month;
currentYear=year;
currentFirstDay=weekday;
UIButton *temp;
NSInteger numberOfDays=[self getNumberofDays:month YearVlue:year];
开发者_开发问答 currentLastDay=(currentFirstDay+numberOfDays-1)%7;
if(currentLastDay==0)
currentLastDay=7;
NSString *monthName=[self getMonth:month];
NSInteger grid=(weekday-1);
monthLabel.text=[NSString stringWithFormat:@"%@ %d",monthName,year];
for(int i=1;i<=numberOfDays;i++)
{
if([frontView isEqualToString:@"view1"])
temp=[arr_View2Buttons objectAtIndex:grid];
else
temp= [arr_View1Buttons objectAtIndex:grid];
[temp setBackgroundImage:nil forState:UIControlStateNormal];
[temp setUserInteractionEnabled:TRUE];
UILabel *aLbl = (UILabel *)[temp viewWithTag:123];
[aLbl setText:[NSString stringWithFormat:@"%d",i]];
// check for today
if(currentDayConstant == i && currentMonthConstant == currentMonth && currentYearConstant == currentYear)
{
[aLbl setTextColor:[UIColor blueColor]];
}
else
{
[aLbl setTextColor:[UIColor colorWithRed:69/255.0 green:2/255.0 blue:71/255.0 alpha:1.0]];
}
[temp setTag:i];
grid++;
if(grid==35)
grid=0;
}
}
And finally viewdidload:
- (void)viewDidLoad
{
[super viewDidLoad];
[self setCalendar];
// set views
[self.view1 setFrame:CGRectMake(0,45,320,232)];
[self.view2 setFrame:CGRectMake(0,277,320,232)];
[self.view addSubview:self.view1];
[self.view addSubview:self.view2];
frontView = @"view2";
[self fillCalender:currentMonth weekStarts:currentFirstDay year:currentYear];
frontView = @"view1";
}
You use NSDateComponents to get the weekday, the month and the year. Which is fine.
But then you use string manipulation to get the day.
And you manipulate the string you get from the description method of NSDate. And from my experience this method returns whatever it wants. I could never figure out which timezone offset the description method uses, on my machine it returns the NSDate in UTC timezone, other users have asked questions on SO that imply that this method returns the NSDate in the local timezone, or in a timezone that is next to the local timezone. Or in a arbitrary timezone. Usually this is no problem because you can use the timezone information, but you simply ignore it.
To make a long story short: Do not use string processing if you want date processing
IMHO this is a timezone issue. Because you mix two different ways of date processing, and you ignore the timezone.
Get rid of the string manipulation part and use NSDateComponents for everything.
You need to change the code
replace NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
with
NSCalendar *currentCalendar = [NSCalendar currentCalendar];
this will identify the current calendar from the device and will perform accordingly
Hope this will solve your problem...
精彩评论