6
let timeZone = NSTimeZone.system.description
let localTimeZone = TimeZone.ReferenceType.local.description
let currentTimeZone = TimeZone.current.description
let defaultTimeZone = TimeZone.ReferenceType.default.description
let autoUpdateTimezon = TimeZone.autoupdatingCurrent.description

print ("System Timezone \(timeZone)")
print ("Local Timezone \(localTimeZone)")
print ("Current Timezone \(currentTimeZone)")
print ("Default Timezone \(defaultTimeZone)")
print ("Auto updating Timezone \(autoUpdateTimezon)")

OUTPUT

System Timezone Asia/Kolkata (current)

Local Timezone Asia/Kolkata (autoupdatingCurrent)

Current Timezone Asia/Kolkata (current)

Default Timezone Asia/Kolkata (current)

Auto updating Timezone Asia/Kolkata (autoupdatingCurrent)

So, i get all the output are same so whats the difference among these timezone and which timezone we should use in which case.

Problem

I used following to code for the date conversion

static func stringToString(strDate:String, fromFormat:String, toFormat:String)->String{
        let dateFormatter = DateFormatter()
        dateFormatter.timeZone = TimeZone.init(abbreviation: "UTC") ?? TimeZone(identifier: "UTC") ?? TimeZone.ReferenceType.default
        dateFormatter.dateFormat = fromFormat
        let currentDate = dateFormatter.date(from: strDate) ?? Date()
        dateFormatter.dateFormat =  toFormat
        dateFormatter.timeZone = TimeZone.ReferenceType.default
        let currentDates = dateFormatter.string(from: currentDate)
        return currentDates
    }

Scene : My app is crashing in qatar if user set timezone automatically and off the 24 hours, but in india there is no crash (TimeZone.ReferenceType.local)

I have given next build with TimeZone.ReferenceType.default and issue is solved

So, i cant understand what was the issue.

Crash Report

enter image description here

Old Code in which i am getting crash

enter image description here

Jaydeep Vyas
  • 4,411
  • 1
  • 18
  • 44
  • Possible duplicate of [NSTimeZone: what is the difference between localTimeZone and systemTimeZone?](https://stackoverflow.com/questions/1526990/nstimezone-what-is-the-difference-between-localtimezone-and-systemtimezone) – Prashant Tukadiya Mar 27 '18 at 05:38

2 Answers2

3

Note that TimeZone.ReferenceType is basically NSTimeZone.

If you look at the docs for TimeZone and NSTimeZone you'll find out very quickly.

From the NSTimeZone:

The system class property returns the time zone currently used by the system, if known. This value is cached once the property is accessed and doesn't reflect any system time zone changes until you call the resetSystemTimeZone() method. The local class property returns an autoupdating proxy object that always returns the current time zone used by the system.

To summarise, system is cached so won't change when the user changes their time zone. You have to call resetSystemTimeZone to update it. local on the other hand automatically updates when the user changes their time zone.

The same thing is true for TimeZone:

TimeZone provides two static functions to get time zone values: current and autoupdatingCurrent. The autoupdatingCurrent time zone automatically tracks updates made by the user.

current corresponds to system and autoupdatingCurrent corresponds to local.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • as issue is solved but i cant reproduce the issue in india, even if i have changed my device timezone to Doha-Qatar, Region - quatar, but cant get crash in india, but in qatar client get crash if he turn off the 24 hours – Jaydeep Vyas Mar 27 '18 at 06:21
  • @JaydeepVyas You replaced _what_ with `NSTimeZone.default`? – Sweeper Mar 27 '18 at 06:23
  • Intially i use TimeZone.ReferenceType.local then i replace it with TimeZone.ReferenceType.default – Jaydeep Vyas Mar 27 '18 at 06:39
  • @JaydeepVyas The `default` time zone can be set. Your app will then run on this time zone. Have you set it anywhere? If not, it should be the same as `system`. – Sweeper Mar 27 '18 at 06:41
  • yes i know that but my client in qatar is facing crash if he off the 24 hours setting in hist i phone, i test in india even if i off the 24 hrs . not getting crash – Jaydeep Vyas Mar 27 '18 at 06:42
  • but after giving him next build with default timezone isuue is solved, So i wondering where is the actual problem exsist – Jaydeep Vyas Mar 27 '18 at 06:43
  • i am converting the same with local time zone and other calculation is done based on that – Jaydeep Vyas Mar 27 '18 at 06:44
2

Local -> An object that tracks the current system time zone. Use this property when you want an object that always reflects the current system time zone. from ios 11, the local class property reflects the current system time zone, whereas previously it reflected the default time zone.

System -> The time zone currently used by the system. If you access the system class property, its value is cached by the app and doesn't update if the user subsequently changes the system time zone. In order for the system property to reflect the new time zone, you must first call the resetSystemTimeZone() method to clear the cached value.

Default -> The default time zone for the current app.If no default time zone has been set, the current system time zone is used. If the current system time zone cannot be determined, the GMT time zone is used instead.The default time zone is used by the app for date and time operations. You can set it to cause the app to run as if it were in a different time zone.

Current -> The time zone currently used by the system.

autoupdatingCurrent -> The time zone currently used by the system, automatically updating to the user’s current preference.

Source -> https://developer.apple.com/documentation/foundation/nstimezone

dahiya_boy
  • 9,298
  • 1
  • 30
  • 51