lychee
lychee

Reputation: 1851

Swift Convert unix timezone to local

Im making a request to a weather API to get some weather data. The issue is how to make sense of the timezone they are providing. Here is a snippet from JSON

"timezone": -14400 // How do you interpret this?,
"id": 5128581,
"name": "New York",
"cod": 200

Is there a way in Swift to convert this timezone so it could be used to display the current time in New York relative to your local timezone?

Upvotes: 0

Views: 54

Answers (1)

Leo Dabus
Leo Dabus

Reputation: 236350

You just need to parse your json string and then you can use DateFormatter initializing its timeZone property with that value using the secondsFromGMT initializer:

let timezone = -14400

let dateFormatter = DateFormatter()
dateFormatter.timeStyle = .short
dateFormatter.timeZone = TimeZone(secondsFromGMT: timezone)
dateFormatter.string(from: Date()) // "6:39 PM"

Upvotes: 2

Related Questions