Sample code for 30+ languages & platforms
Zig

Get Current Date/Time from NIST Time Server

See more Socket/SSL/TLS Examples

Demonstrates using Chilkat Socket to connect to an NIST time server and (using the old Time Protocol (RFC 868)), will read the current GMT time.

Note: This is not necessarily the very best means for getting the current date/time. The most commonly used time protocol is the Network Time Protocol (RFC-1305).

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    const socket = try chilkat.Socket.init();
    defer socket.deinit();

    // Connect to an NIST time server and read the current date/time
    var max_wait_ms: i32 = 4000;
    const use_tls = false;
    socket.connect("time-c.nist.gov", 37, use_tls, max_wait_ms) catch {
        std.debug.print("{s}\n", .{try socket.getLastErrorText(alloc)});
        return;
    };

    // The time server will send a big-endian 32-bit integer representing
    // the number of seconds since since 00:00 (midnight) 1 January 1900 GMT.
    // The ReceiveInt32 method will receive a 4-byte integer, but returns
    // true or false to indicate success.  If successful, the integer
    // is obtained via the ReceivedInt property.
    const big_endian = true;
    socket.receiveInt32(big_endian) catch {
        std.debug.print("{s}\n", .{try socket.getLastErrorText(alloc)});
        return;
    };

    const dt = try chilkat.DateTime.init();
    defer dt.deinit();
    dt.setFromNtpTime(socket.getReceivedInt()) catch {};

    // Show the current local date/time
    const b_local_time = true;
    std.debug.print("Current local date/time: {s}\n", .{try dt.getAsRfc822(alloc, b_local_time)});

    max_wait_ms = 10;
    socket.close(max_wait_ms) catch {};
}