Sample code for 30+ languages & platforms
Zig

JSON Date Parsing

See more JSON Examples

Demonstrates how to parse date/time strings from JSON.

Note: This example uses the DtOf and DateOf methods introduced in Chilkat v9.5.0.73

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 json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.setEmitCompact(false);

    // First, let's create JSON containing some date/time strings.
    json.updateString("test.timestamp", "2018-01-30T20:35:00Z") catch {};
    json.updateString("test.rfc822", "Tue, 24 Apr 2018 08:47:03 -0500") catch {};
    json.updateString("test.dateStrings[0]", "2018-01-30T20:35:00Z") catch {};
    json.updateString("test.dateStrings[1]", "Tue, 24 Apr 2018 08:47:03 -0500") catch {};
    json.updateNumber("test.StartLoggingTime", "1446834998.695") catch {};
    json.updateNumber("test.Expiration", "1442877512.0") catch {};
    json.updateInt("test.StartTime", 1518867432) catch {};

    std.debug.print("{s}\n", .{try json.emit(alloc)});

    // We've built the following JSON:

    // {
    //   "test": {
    //     "timestamp": "2018-01-30T20:35:00Z",
    //     "rfc822": "Tue, 24 Apr 2018 08:47:03 -0500",
    //     "dateStrings": [
    //       "2018-01-30T20:35:00Z",
    //       "Tue, 24 Apr 2018 08:47:03 -0500"
    //     ],
    //     "StartLoggingTime": 1446834998.695,
    //     "Expiration": 1442877512.0,
    //     "StartTime": 1518867432
    //   }
    // }

    // Use the DateOf and DtOf methods to load Chilkat date/time objects with the date/time values.
    // The CkDateTime object is primarily for loading a date/time from numerous formats, and then getting
    // the date/time in various formats.  Thus, it's primarly for date/time format conversion.
    // The DtObj object holds a date/time where the individual components (day, month, year, hour, minutes, etc.) are
    // immediately accessible as integers.
    const date_time = try chilkat.DateTime.init();
    defer date_time.deinit();
    const dt = try chilkat.DtObj.init();
    defer dt.deinit();
    const get_as_local = false;

    // Load the date/time at test.timestamp into the dateTime object.
    try json.dateOf("test.timestamp", date_time);
    std.debug.print("{s}\n", .{try date_time.getAsTimestamp(alloc, get_as_local)});
    std.debug.print("{d}\n", .{date_time.getAsUnixTime(false)});
    std.debug.print("{s}\n", .{try date_time.getAsRfc822(alloc, get_as_local)});

    try json.dateOf("test.rfc822", date_time);
    std.debug.print("{s}\n", .{try date_time.getAsTimestamp(alloc, get_as_local)});

    json.setI(0);
    try json.dateOf("test.dateStrings[i]", date_time);
    std.debug.print("{s}\n", .{try date_time.getAsTimestamp(alloc, get_as_local)});

    json.setI(1);
    try json.dateOf("test.dateStrings[i]", date_time);
    std.debug.print("{s}\n", .{try date_time.getAsTimestamp(alloc, get_as_local)});

    try json.dateOf("test.StartLoggingTime", date_time);
    std.debug.print("{s}\n", .{try date_time.getAsTimestamp(alloc, get_as_local)});

    try json.dateOf("test.Expiration", date_time);
    std.debug.print("{s}\n", .{try date_time.getAsTimestamp(alloc, get_as_local)});

    try json.dateOf("test.StartTime", date_time);
    std.debug.print("{s}\n", .{try date_time.getAsTimestamp(alloc, get_as_local)});

    // Output so far:

    //     2018-01-30T20:35:00Z
    //     1517344500
    //     Tue, 30 Jan 2018 20:35:00 GMT
    //     2018-04-24T13:47:03Z
    //     2018-01-30T20:35:00Z
    //     2018-04-24T13:47:03Z
    //     2015-11-07T00:36:38Z
    //     2015-09-22T04:18:32Z
    //     2018-02-17T17:37:12Z

    // Now load the date/time strings into the dt object:
    try json.dtOf("test.timestamp", get_as_local, dt);
    std.debug.print("month={d}, day={d}, year={d}, hour={d}, minute={d}\n", .{ dt.getMonth(), dt.getDay(), dt.getYear(), dt.getHour(), dt.getMinute() });

    try json.dtOf("test.rfc822", get_as_local, dt);
    std.debug.print("month={d}, day={d}, year={d}, hour={d}, minute={d}\n", .{ dt.getMonth(), dt.getDay(), dt.getYear(), dt.getHour(), dt.getMinute() });

    json.setI(0);
    try json.dtOf("test.dateStrings[i]", get_as_local, dt);
    std.debug.print("month={d}, day={d}, year={d}, hour={d}, minute={d}\n", .{ dt.getMonth(), dt.getDay(), dt.getYear(), dt.getHour(), dt.getMinute() });

    json.setI(1);
    try json.dtOf("test.dateStrings[i]", get_as_local, dt);
    std.debug.print("month={d}, day={d}, year={d}, hour={d}, minute={d}\n", .{ dt.getMonth(), dt.getDay(), dt.getYear(), dt.getHour(), dt.getMinute() });

    try json.dtOf("test.StartLoggingTime", get_as_local, dt);
    std.debug.print("month={d}, day={d}, year={d}, hour={d}, minute={d}\n", .{ dt.getMonth(), dt.getDay(), dt.getYear(), dt.getHour(), dt.getMinute() });

    try json.dtOf("test.Expiration", get_as_local, dt);
    std.debug.print("month={d}, day={d}, year={d}, hour={d}, minute={d}\n", .{ dt.getMonth(), dt.getDay(), dt.getYear(), dt.getHour(), dt.getMinute() });

    try json.dtOf("test.StartTime", get_as_local, dt);
    std.debug.print("month={d}, day={d}, year={d}, hour={d}, minute={d}\n", .{ dt.getMonth(), dt.getDay(), dt.getYear(), dt.getHour(), dt.getMinute() });

    // Output:

    // month=1, day=30, year=2018, hour=20, minute=35
    // month=4, day=24, year=2018, hour=13, minute=47
    // month=1, day=30, year=2018, hour=20, minute=35
    // month=4, day=24, year=2018, hour=13, minute=47
    // month=11, day=6, year=2015, hour=18, minute=36
    // month=9, day=21, year=2015, hour=23, minute=18
    // month=2, day=17, year=2018, hour=11, minute=37
}