I am trying to combine a NSDate (date) with a NSDate (time) into a new NSDate object, for whatever reason I seem to be losing 1 hour.
I have an array of formatted time objects that are 15 minutes between each one. I have a selectable date item, which referes to a NSDate object.
A user selects a formatted time object (I have a reference to the NSDate in a dictionary) and then when it comes to save; I do this;
if (self.selectedDate && self.selectedTimeObject)
{
// Combine selected date and selected time into a new NSDate object
NSCalendar *cal = [NSCalendar currentCalendar];
unsigned unitFlagsDate = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
unsigned unitFlagsTime = NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *dateComponents = [cal components:unitFlagsDate fromDate:self.selectedDate];
NSDateComponents *timeComponents = [cal components:unitFlagsTime fromDate:self.selectedTimeObject];
[dateComponents setSecond:[timeComponents second]];
[dateComponents setHour:[timeComponents hour]];
[dateComponents setMinute:[timeComponents minute]];
NSDate *combinedDate = [cal dateFromComponents:dateComponents];
NSLog(@"targetDate = %@", combinedDate);
}
In my example I choose 01:00; but for some reason I lose 1 hour depending on the date.
Logs;
targetDate = 2015-03-31 00:00:00 +0000
targetDate = 2015-03-30 00:00:00 +0000
targetDate = 2015-03-29 01:00:00 +0000
targetDate = 2015-03-28 01:00:00 +0000
targetDate = 2015-03-27 01:00:00 +0000
targetDate = 2015-03-31 00:00:00 +0000
targetDate = 2015-04-01 00:00:00 +0000
targetDate = 2015-04-02 00:00:00 +0000
Now in my logs, I noticed that anything before 28 is fine, but anything after that date loses 1 hour without explanation.
I have checked my array, I have checked my indexes, I have checked that the objects are valid time objects; however I am stumped -- why does it lose 1 hour?