0

How can I get the current timezone in MacOS using swift 3.0 in the format “(UTC +5:30) Bombay, Calcutta, Madras, New Delhi”. As of now, I am getting it "IST": "Asia/Calcutta”.

The problem is, from our windows system we are getting the timezone as “(UTC +5:30) Bombay, Calcutta, Madras, New Delhi”, I want the same timezone format in MacOS as well.

I could come up with following with the solution provided in link How to get a user's time zone?

let secondsFromGMT = TimeZone.current.secondsFromGMT()
let hrsfromGMT = secondsFromGMT/3600
NSLog("hrsfromGMT \(hrsfromGMT)")
let remainingminutes = (Double(secondsFromGMT)
        .truncatingRemainder(dividingBy: 3600))/60
NSLog("remainingminutes \(remainingminutes)")
let localTimeZoneIdentifier = 
TimeZone.current.identifier.replacingOccurrences(of: "/", with: ",", options: .literal, range: nil)
let utcoffset = String(format: "%02d:%02d", hrsfromGMT,Int(remainingminutes))
let utcoffset = String(format: "%02d:%02d", hrsfromGMT,Int(remainingminutes))
messageBuilderObj.timezone = "(UTC+"+utcoffset+") "+localTimeZoneIdentifier

with this I am getting output as //"(UTC+05:30) Asia,Kolkata" instead of //"(UTC+05:30) Chennai, Kolkata, Mumbai, New Delhi". Please feel free to modify the code and suggest any solutions

Tulsidas
  • 21
  • 3
  • Does this answer your question? [How to get a user's time zone?](https://stackoverflow.com/questions/27053135/how-to-get-a-users-time-zone) – Asif Mujtaba Jul 13 '20 at 18:01
  • Zulqarnayn - I have already seen the answer suggested by you. I need the timezones we see on windows machines like(GMT +5:30) Bombay,Calcutta,Madras,New Delhi”. Do we have any API for it in swift. – Tulsidas Jul 13 '20 at 19:25
  • *Do we have any API for it in swift*. No we don't have. But the city part is not relevant for time zone calculations. Only `UTC+0530` is relevant. – vadian Jul 13 '20 at 19:39
  • This is such a specific request so why don't you download a list of windows time zone names and store them in a file in your app bundle and create your own api? – Joakim Danielson Jul 13 '20 at 19:43
  • Thanks for your suggestion Joakim Danielson. Lets see how it goes, I was thinking that I am missing on something. Since this is available in windows I thought it should be readily available in MAC as well. – Tulsidas Jul 13 '20 at 19:48

0 Answers0