0

I'm trying to set time on a NSDatePicker according to a certain offset that I have in milliseconds. When I set the time, the value that I see is off and I can't figure out why. Here is the code I use to set the picker:

- (void)setDatePicker{

    int targetmillisondsFromMidnight = [self.schedule.targetHour intValue]; //Value is: 61680000 milliseconds whis is equal to 17:08 UTC (or 19:08 in my local time);
    NSDate* todayMidnight = [NSCalendar.currentCalendar startOfDayForDate:[NSDate new]];
    NSTimeZone* timezone = [NSTimeZone localTimeZone]; //Value is: Local Time Zone (Asia/Jerusalem (GMT‎+2‎) offset 7200)
    NSInteger seconds = [timezone secondsFromGMT]; //Value is: 7200
    todayMidnight = [todayMidnight dateByAddingTimeInterval:seconds]; // Value is: 2019-12-25 00:00:00 UTC
    NSDate* scheduleDate = [NSDate dateWithTimeInterval:targetmillisondsFromMidnight/1000 sinceDate:todayMidnight]; //Value is: 2019-12-25 17:08:00 UTC

    NSCalendar *calendar = [NSCalendar currentCalendar];
    [calendar setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
    NSDateComponents *components = [calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:scheduleDate];

    [self.datePicker setDate:[calendar dateFromComponents:components] animated:YES];
} 

I stop with a breakpoint at the last command in the function and I print some values, the outputs that I get are: po [components hour] = 17 po [components minute] = 8 po [calendar dateFromComponents:components] = 001-01-01 17:08:00 +0000

So from my understanding the date is set with 17:08. What I expect to see on the time picker is 19:08, however what I see is 19:28. I can't figure out where those 20 minutes are coming from.

Keselme
  • 3,779
  • 7
  • 36
  • 68

1 Answers1

1

Try this code. Date picker uses while setting time current timezone and uses UTC offset based on passed date (001-01-01 17:08:00 +0000) and looks for offset in timezone database at this timepoint. Because at that time (year zero) there were no timezones, it could not be found in tz database so time zone offset was calculated based on mean solar time, so you got offset 2:20 (approx.) for your area.

- (void)setDatePicker {
    int targetmillisondsFromMidnight = 61680000; //Value is: 61680000 milliseconds whis is equal to 17:08 UTC (or 19:08 in my local time);
    NSCalendar *calendar = NSCalendar.currentCalendar;
    NSTimeZone* timezone = [NSTimeZone timeZoneWithName:@"Asia/Jerusalem"]; //Value is: Local Time Zone (Asia/Jerusalem (GMT‎+2‎) offset 7200)
    calendar.timeZone = timezone;
    NSDate* todayMidnight = [calendar startOfDayForDate:[NSDate new]];

    NSInteger seconds = [timezone secondsFromGMT]; //Value is: 7200
    todayMidnight = [todayMidnight dateByAddingTimeInterval:seconds]; // Value is: 2019-12-25 00:00:00 UTC
    NSDate* scheduleDate = [NSDate dateWithTimeInterval:targetmillisondsFromMidnight/1000 sinceDate:todayMidnight]; //Value is: 2019-12-25 17:08:00 UTC

    NSDate *date = [scheduleDate dateByAddingTimeInterval:seconds];

    self.datePicker.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
    [self.datePicker setDate:date animated:YES];
}
schmidt9
  • 4,436
  • 1
  • 26
  • 32