Sample code for 30+ languages & platforms
Lua Requires Chilkat v11.6.0+

Azure Maps: Find a Time Zone and UTC Offsets

See more Azure Maps Examples

Finds the IANA time zone for coordinates and reads the standard offset, daylight-saving adjustment, and reference local time. Timezone 1.0 takes latitude before longitude. Omit timeStamp for current information, or supply an ISO 8601 timestamp when the date matters.

The effective UTC offset combines the standard offset and daylight-saving adjustment. Store the zone ID, not just today's offset, when scheduling future events. Not all locations observe daylight saving.

Uses direct Chilkat.Http calls to the Azure public-cloud endpoint. Supply a nonempty subscription_key string in private qa_data/azure/maps.json. Shared-key authentication must be enabled on the Maps account. Keep this file outside source control and public web directories, and do not embed account keys in publicly distributed applications. Microsoft Entra ID is an alternative.

Query parameters are supplied as raw text for Chilkat to encode. The code checks transport status, HTTP status, and JSON parsing separately. For throttling or temporary service failures, use bounded retries and honor Retry-After. HTTP diagnostics can contain credentials and location data; inspect them privately. Calls use verified HTTPS and do not follow redirects.

Microsoft API reference

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- This example requires the Chilkat API to have been previously unlocked.
-- See Global.UnlockBundle for sample code.

local config = chilkat.JsonObject.new()
config.strict = true
-- Private file: {"subscription_key":"YOUR_AZURE_MAPS_KEY"}.
local success = config:load_file("qa_data/azure/maps.json")
if not success then
    print("Could not load private Azure Maps configuration.")
    return
end

if config:json_type_of("subscription_key") ~= 1 then
    print("Missing or invalid subscription_key.")
    return
end

if config:string_of_equals("subscription_key", "", true) then
    print("A nonempty subscription_key is required.")
    return
end

local http = chilkat.Http.new()
http.require_ssl_cert_verify = true
http.follow_redirects = false
http.connect_timeout = 30
http.read_timeout = 90
http.accept = "application/json"
local response = chilkat.HttpResponse.new()
local result = chilkat.JsonObject.new()
result.strict = true
local query = chilkat.JsonObject.new()
-- Current Azure Maps operations accept the key as a header.
http:set_request_header("subscription-key", config:string_of("subscription_key"))
-- Timezone 1.0 uses LATITUDE,LONGITUDE (unlike Search 2026 GeoJSON).
-- Omit timeStamp to ask for the current time at the service.
query:update_string("api-version", "1.0")
query:update_string("query", "47.642282,-122.136833")
query:update_string("options", "all")
success = http:http_params("GET", "https://atlas.microsoft.com/timezone/byCoordinates/json", query, response)
-- Http methods return false for HTTP errors as well as transport failures.
if response.status_code == 0 then
    print("No HTTP response. Inspect diagnostics privately; they can contain credentials.")
    return
end

if response.status_code ~= 200 then
    print("HTTP status: " .. response.status_code)
    print("Error code: " .. response:get_header_field("x-ms-error-code"))
    print("Retry-After: " .. response:get_header_field("Retry-After"))
    -- Correct 400/401/403 errors before retrying. Back off for 429 and transient 5xx.
    -- Keep response bodies and detailed diagnostics out of public logs.
    return
end

if not success then
    print("The HTTP response was incomplete.")
    return
end

success = result:load(response.body_str)
if not success then
    print("Expected a well-formed JSON response.")
    return
end

local zones = chilkat.JsonArray.new()
local zone = chilkat.JsonObject.new()
success = result:array_of2("timeZones", zones)
if not success then
    print("Expected timeZones array.")
    return
end

if zones.size == 0 then
    print("No time zone was returned.")
    return
end

local i = 0
while i < zones.size do
    success = zones:object_at2(i, zone)
    if not success then
        print("Invalid time zone.")
        return
    end

    if zone:json_type_of("id") ~= 1 then
        print("Missing or invalid id.")
        return
    end

    if zone:json_type_of("referenceTime.standardOffset") ~= 1 then
        print("Missing or invalid referenceTime.standardOffset.")
        return
    end

    if zone:json_type_of("referenceTime.daylightSavings") ~= 1 then
        print("Missing or invalid referenceTime.daylightSavings.")
        return
    end

    if zone:json_type_of("referenceTime.wallTime") ~= 1 then
        print("Missing or invalid referenceTime.wallTime.")
        return
    end

    print("IANA time zone: " .. zone:string_of("id"))
    print("Standard UTC offset: " .. zone:string_of("referenceTime.standardOffset"))
    print("Daylight-saving adjustment: " .. zone:string_of("referenceTime.daylightSavings"))
    print("Local time: " .. zone:string_of("referenceTime.wallTime"))
    -- Effective offset is standardOffset plus daylightSavings, not either alone.
    -- Preserve the IANA ID: offsets can change with date and local rules.
    i = i + 1
end