How to Retrieve Time Zones in C/C++
If you do a lot of international business, the difference in time zones can have major implications on the way you work. Instead of manually looking up the time zone for each country you have an interaction with, the following API will allow you to automatically retrieve a country’s time zone and current time from an up-to-date database, enabling you to better accommodate and plan for your international interactions.
To begin the process, we will install libcurl in our C/C++ project:
libcurl/7.75.0
Once the installation is complete, we can input the country ISO code or name and call the function:
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(curl, CURLOPT_URL, "https://api.cloudmersive.com/validate/address/country/get-timezones");
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Content-Type: application/json");
headers = curl_slist_append(headers, "Apikey: YOUR-API-KEY-HERE");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
const char *data = "{\n \"CountryCodeOrName\": \"<string>\"\n}";
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
res = curl_easy_perform(curl);
}
curl_easy_cleanup(curl);
This will return the name of the time zone, as well as the base UTC offset and current time. If you need to obtain an API key, head over to the Cloudmersive website to register for a free account; this will provide access to 800 calls/month across our entire library of APIs.