Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is Difference among following time zones in swift

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

like image 822
Jaydeep Vyas Avatar asked Mar 27 '18 05:03

Jaydeep Vyas


People also ask

How many Times Zones are there?

The world is divided longitudinally into time zones, with each hour difference roughly 15 degrees apart. Due to some countries having half hour time zones, there are more than 24 times zones in the world.

How to determine time Zones?

There are 24 hours in a day, and 360 degrees of longitude encompassing the globe – dividing 360 by 24 gives you the 15 degrees of longitude that equates to a one-hour difference in each time zone. Based on this, you can then deduce that there are 24 time zones around the world.


2 Answers

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

like image 84
dahiya_boy Avatar answered Oct 19 '22 00:10

dahiya_boy


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.

like image 45
Sweeper Avatar answered Oct 18 '22 23:10

Sweeper