Sample code for 30+ languages & platforms
Zig Requires Chilkat v11.0.0+

Get the Last-Modified Date Before HTTP Download

See more HTTP Examples

Demonstrates how to send a HEAD request to get the last-modified date of a file on a web server (without downloading the file).

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 assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    const http = try chilkat.Http.init();
    defer http.deinit();

    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpNoBody("HEAD", "https://www.chilkatsoft.com/hamlet.xml", resp) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    // Examine the response header.
    std.debug.print("{s}\n", .{try resp.getHeader(alloc)});

    // Here is a sample response header:

    //     Content-Length: 279658
    //     Content-Type: text/xml
    //     Last-Modified: Thu, 12 May 2016 15:14:08 GMT
    //     Accept-Ranges: bytes
    //     ETag: "c1cd8bee60acd11:0"
    //     Server: Microsoft-IIS/8.5
    //     X-Powered-By: ASP.NET
    //     X-Powered-By-Plesk: PleskWin
    //     Date: Thu, 25 Jul 2019 16:40:54 GMT

    // Get the Last-Modified header.
    // If the header exists...
    if (resp.getHeaderField(alloc, "Last-Modified")) |last_mod_str| {
        // Parse the RFC822 format date/time string
        const ckdt = try chilkat.DateTime.init();
        defer ckdt.deinit();
        ckdt.setFromRfc822(last_mod_str) catch {};

        // If we want to access individual date/time parts (in the local timezone)
        const dt = try chilkat.DtObj.init();
        defer dt.deinit();
        ckdt.toDtObj(true, dt);

        std.debug.print("day/month/year = {d}/{d}/{d}\n", .{ dt.getDay(), dt.getMonth(), dt.getYear() });
    } else |_| {}
}