Sample code for 30+ languages & platforms
Lua Requires Chilkat v9.5.0.96+

Query NTP Server for Current Date/Time

Demonstrates how to query an NTP server to get the current date/time.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- Specify the NTP server domain.
-- Information is passed in a JSON object to allow for any unanticipated future optional information that might need to be provided.
local json = chilkat.JsonObject.new()
json:update_string("ntp_server", "pool.ntp.org")

-- Query the NTP server for the current date/time.
local dt = chilkat.DateTime.new()
local success = dt:set_from_ntp_server(json:emit())
if not success then
    print(dt.last_error_text)
    return
end

-- Show the date/time in various formats..
local bLocalTime = true
print(dt:get_as_timestamp(bLocalTime))
print(dt:get_as_rfc822(bLocalTime))
print(dt:get_as_unix_time(bLocalTime))

-- Sample output:

-- 2023-10-31T09:43:27-05:00
-- Tue, 31 Oct 2023 09:43:27 -0500
-- 1698745407