1

I have this method that converts date to string:

- (NSString*)getDateTimeBasedOnDeviceTimeFormat:(NSDate *)theDate{

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    NSDate *dateSource;
    NSString *dateStr;

    [dateFormatter setLocale:[NSLocale currentLocale]];
    if (theDate) {
        dateSource = theDate;
    } else {
        dateSource = [NSDate date];
    }

    [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]]; //tried local timezone
    [dateFormatter setDateStyle:NSDateFormatterLongStyle];
    [dateFormatter setTimeStyle:NSDateFormatterShortStyle];

    dateStr = [dateFormatter stringFromDate:dateSource];

    return dateStr;
}

For iOS 10.3.1:

the returned value is October 31, 2017 at 2:26 PM (this is wrong)

For iOS 11:

the returned value is October 31, 2017 at 10:26 AM (this is correct)

Both the simulators have the same language and region set. English (US).

enter image description here

enter image description here

mac date time settings:

enter image description here I have no idea why this conversion shows up wrong in iOS 10.3.1

Can someone point me in a right direction ?

Teja Nandamuri
  • 11,045
  • 6
  • 57
  • 109
  • `[dateFormatter setLocale:[NSLocale currentLocale]];` is redundant; this is what the formatter defaults to. – jscs Oct 31 '17 at 14:47
  • Why would you need to change the timezone anyways? Have you tried without the line where you do `setTimeZone:`? – TawaNicolas Oct 31 '17 at 14:51
  • I thought by setting timezone to systemTimeZOne, the app will adjust to the device timezone so date conversions for appointments won't messed up when users change their timezones. YEs, I tried without that line, the result is same. @TawaNicolas – Teja Nandamuri Oct 31 '17 at 14:53
  • Added screenshots of the timezone settings @rmaddy – Teja Nandamuri Oct 31 '17 at 15:38
  • I believe both the simulators share the same timezone setting from the mac. – Teja Nandamuri Oct 31 '17 at 15:39
  • I tried your code with the 10.3 simulator and got the correct result-- the local time at my location. Maybe try resetting the simulator? – Tom Harrington Oct 31 '17 at 15:54
  • it looks like the simulator issue, as the simulator time itself is showing wrong. The simulator shows the same wrong time even after resetting its contents. @TomHarrington – Teja Nandamuri Oct 31 '17 at 16:00
  • That's the real problem then-- the code can only show what the simulator claims the local time is. – Tom Harrington Oct 31 '17 at 16:06

2 Answers2

2

EDIT Here is a radar that summarises the issue: Versions of the iOS simulator for anything other than 11.0 and 11.1 use GMT instead of the macOS time zone.

I have noticed this also, but on all non-11 iOS simulators in Mac OS 10.13.1 only.

Here's another example:

import Foundation
let formatterUTC = DateFormatter()
formatterUTC.locale = Locale(identifier: "en_GB")
formatterUTC.dateFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'"
formatterUTC.timeZone = TimeZone(abbreviation: "UTC")
let d = formatterUTC.date(from:"2017-07-01T12:00:00Z")

let formatter24 = DateFormatter()
formatter24.locale = Locale(identifier: "en_GB")
formatter24.dateFormat = "HH:mm"
let s = formatter24.string(from:d!) // ⚠️ "12:00" on iOS <= 10.3.1, Mac OS 10.13.1,
                                    //    "13:00" elsewhere! ⚠️
funkybro
  • 8,432
  • 6
  • 39
  • 52
1

You have a pretty similar issue as to what I was experiencing when testing 10.3.1. While debugging my dateFormatter, I noticed that the time zone on the simulator was always coming back as "GMT". It didn't appear to be properly using my computer settings.

let timeZone = TimeZone.current.abbreviation() // produces "GMT" on iOS 10 and "PDT" on iOS 11

So I tested on physical device, everything was being displayed properly. I'm chalking this one up to an Apple Simulator bug on 10.3.1 sims at least. Hope this helps and I hope you have extra devices to test on!

Craig Fisher
  • 371
  • 2
  • 15