I have a strange problem with EKEventEditViewController when using it with custom timezones. It behaves differently in two situations:
Situation 1 - works fine:
- Launch app
- Create EKEventEditViewController to add new event with startDate = [NSDate date]
- New event start is displayed correctly (current time)
- Change default timezone with [NSTimeZone setDefaultTimeZone:otherTimeZone]
- Create EKEventEditViewController to add new event with startDate = [NSDate date]
- New event start is displayed correctly (current time adjusted to time zone)
Situation 2 - unexpected behavior:
- Launch app
- Change default timezone with [NSTimeZone setDefaultTimeZone:otherTimeZone]
- Create EKEventEditViewController to add new event with startDate = [NSDate date]
- New event start is displayed incorrectly (system timezone offset + default timezone offset)
- Change default timezone back to system timezone [NSTimeZone setDefaultTimeZone:[NSTimeZone systemTimeZone]]
- Creat开发者_如何转开发e EKEventEditViewController to add new event with startDate = [NSDate date]
- New event start is still displayed incorrectly (system timezone offset + default timezone offset)
My guess that on first display of EKEventEditViewController it somehow caches default timezone and then uses it as an offset.
Has anyone faced similar problem? Is this a bug or am I missing something?
I had exact same problem. I was storing all the dates in the database in GMT timeZone with offsets (separately). My app uses custom timeZone from the beginning it's being run (GMT). When I wanted to use those dates while exporting an events to the calendar, I was seeing wrong start and end dates. What helped me solving the problem was firstly convert the dates I had stored in the database to the system Time Zone using the following converter method (see below). Such converted date passed to the EKEventEditViewController was then showing the dates correctly. Hope it will solve your problem too.
+ (NSDate *) convertToSystemTimeZone:(NSDate*)sourceDate {
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
NSDate* destinationDate = [[[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate] autorelease];
return destinationDate; }
精彩评论