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

Get Email Date/Time

Demonstrates getting the email "Date" header field in a CkDateTime object.

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();

    const email = try chilkat.Email.init();
    defer email.deinit();

    // Load a .eml file into the email object.
    try email.loadEml("/home/users/chilkat/eml/myEml.eml");

    const dt_time = try chilkat.DateTime.init();
    defer dt_time.deinit();
    dt_time.setFromRfc822(try email.getEmailDateStr(alloc)) catch {};

    // Once we have the CkDateTime object, we can get the date/time in many different formats:

    // Get as a RFC822 GMT string:
    var b_local_time: bool = false;
    std.debug.print("{s}\n", .{try dt_time.getAsRfc822(alloc, b_local_time)});

    // Get as an RFC822 string in the local timezone.
    // (remember, the daylight savings that existed at the given time in the past is applied)
    b_local_time = true;
    std.debug.print("{s}\n", .{try dt_time.getAsRfc822(alloc, b_local_time)});

    // Get as a 32-bit UNIX time (local or GMT..)
    // The Unix time is number of seconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC).
    const unix_time = @as(i32, @intCast(dt_time.getAsUnixTime(b_local_time)));
    std.debug.print("Unix time: {d}\n", .{unix_time});

    // One can also get the as a "DtObj" object for accessing the individual
    // parts of the date/time, such as month, day, year, hour, minute, etc.
    // The DtObj can be obtained in the GMT or local timezone:
    const dt_obj = try chilkat.DtObj.init();
    defer dt_obj.deinit();
    dt_time.toDtObj(b_local_time, dt_obj);

    if (!dt_time.getLastMethodSuccess()) {
        std.debug.print("This should never really happen!\n", .{});
        return;
    }

    std.debug.print("{d}-{d}-{d} {d}:{d}:{d}\n", .{ dt_obj.getDay(), dt_obj.getMonth(), dt_obj.getYear(), dt_obj.getHour(), dt_obj.getMinute(), dt_obj.getSecond() });
}