How to Get Time Zones for a Country in Swift
For the past 30 years, online capabilities have steadily grown in quality and number. As a direct result of this growth, international business has also seen a tremendous increase. To ensure your out-of-country customers and partners are receiving correct information pertaining to their location, it can be helpful to incorporate a time zone conversion tool into your website or application. The following API can be used in Swift to automatically get the Olson time zones for a specific country, allowing you to save the time it would take to build it out manually.
Now to perform the operation, all you’ll need are the country name or ISO code, as well as your API key to plug into the below code:
import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endifvar semaphore = DispatchSemaphore (value: 0)let parameters = "{\n \"CountryCodeOrName\": \"<string>\"\n}"
let postData = parameters.data(using: .utf8)var request = URLRequest(url: URL(string: "https://api.cloudmersive.com/validate/address/country/get-timezones")!,timeoutInterval: Double.infinity)
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("YOUR-API-KEY-HERE", forHTTPHeaderField: "Apikey")request.httpMethod = "POST"
request.httpBody = postDatalet task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data else {
print(String(describing: error))
semaphore.signal()
return
}
print(String(data: data, encoding: .utf8)!)
semaphore.signal()
}task.resume()
semaphore.wait()
Your prompt response will indicate the country name, time zones, and current time along with the two-letter ISO code, two-letter FIPS code, and three-letter code.