convert chatham time to gmt in swift

To convert Chatham time to GMT in Swift, you need to use the DateFormatter and TimeZone classes.

First, you need to create two TimeZone objects, one for Chatham Time and the other for GMT.

main.swift
let chathamTimeZone = TimeZone(identifier: "Pacific/Chatham")!
let gmtTimeZone = TimeZone(identifier: "GMT")!
110 chars
3 lines

Next, create an instance of DateFormatter and set the time zone to Chatham Time.

main.swift
let dateFormatter = DateFormatter()
dateFormatter.timeZone = chathamTimeZone
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
126 chars
4 lines

Now you can convert a Chatham Time String to Date:

main.swift
let chathamTimeString = "2021-06-01 10:30:00"
let chathamTime = dateFormatter.date(from: chathamTimeString)!
109 chars
3 lines

Finally, set the time zone of the Date object to GMT and format it as a String.

main.swift
dateFormatter.timeZone = gmtTimeZone
let gmtTimeString = dateFormatter.string(from: chathamTime)
print(gmtTimeString) // Output: 2021-06-01 21:30:00
149 chars
4 lines

The resulting String will be the equivalent time in GMT.

gistlibby LogSnag