I'm building an iPhone application using Xcode 3.2.3 and I have experienced very weird behavior of the application when run in Release/Distribution Configuration. Let show the code first:
- (NSTimeInterval) durationValue {
NSTimeInterval duration = 0.0;
if (!self.startDateTime) {
return 0.0;
}
NSDate *lateDate = self.endDateTime;
NSTimeInterval lateDateTimeZoneDifference = [self.endDateTimeTimeZone intValue];
if(!lateDate){
lateDate = [NSDate date];
lateDateTimeZoneDifference = [[NSTimeZone systemTimeZone] secondsFromGMT];
}
duration = [lateDate timeIntervalSinceDate:self.startDateTime]; // 1
// correct time zone differences
duration -= lateDateTimeZoneDifference;
duration += [self.startDateTimeTimeZone intValue]; // !!
return duration;
}
This is the function that is placed in the category of one of my NSManagedObject subclass. Type of startDateTime, endDateTime is NSDate. Type of endDateTimeTimeZone and startDateTimeZone are Integer32 (CoreData types). startDateTimeZone
and endDateTimeZone
store number of seconds from GMT of the date startDateTime
and endDateTime
accordingly.
The function simply counts the duration between startDateTime
and endDateTime
. Counting durationValue I also take to the consideration the time zone of the start and end date and adjust the final duration value accordingly. The code counts the duration correctly when run in Debug configuration.
When I run it in Distribution/Release the line marked with '!!' has no effect. As an example: let's say i have start date as today 1:00, end date today 2:00, time zones are the same so that we have the same value in startDateTimeTimeZone(7200 sec = +0200) and endDateTimeTimeZone(7200 sec = +0200). When duration
is counted as a timeIntervalBetween dates i get the proper duration value(line marked as '1'), in the following line the duration
value is properly substracted but in the '!!' line the duration value is not increased at all!
How to fix it? I think it might be a problem with compiler optimalization(I use -Os) or with the type differences(Integer32 and NSTimeInterval).