Zig
Zig
Compute Difference Between Two DateTime Strings
Demonstrates how to compute the difference between two datetime strings.Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// Imagine we have these strings:
const t1 = "2022-11-14 15:45:38";
const t2 = "2022-11-16 17:23:41";
// How many minutes are between the two?
// First, we'd want load each date/time into a Chilkat CkDateTime object.
// In this case, there are no CkDateTime methods that accept the above format.
// However.. the CkDateTime's SetFromTimestamp will accept a date in the format "YYYY-MM-DDThh:mm:ssZ"
// First modify the above dates to the timestamp format.
const sb1 = try chilkat.StringBuilder.init();
defer sb1.deinit();
sb1.append(t1) catch {};
var count: i32 = sb1.replace(" ", "T");
sb1.append("Z") catch {};
const sb2 = try chilkat.StringBuilder.init();
defer sb2.deinit();
sb2.append(t2) catch {};
count = sb2.replace(" ", "T");
sb2.append("Z") catch {};
// Load each into a CkDateTime
const dt1 = try chilkat.DateTime.init();
defer dt1.deinit();
dt1.setFromTimestamp(try sb1.getAsString(alloc)) catch {};
// verify...
std.debug.print("{s}\n", .{try dt1.getAsTimestamp(alloc, false)});
const dt2 = try chilkat.DateTime.init();
defer dt2.deinit();
dt2.setFromTimestamp(try sb2.getAsString(alloc)) catch {};
// verify...
std.debug.print("{s}\n", .{try dt2.getAsTimestamp(alloc, false)});
// Get the difference in seconds
const diff_seconds = dt2.diffSeconds(dt1);
std.debug.print("Difference in seconds: {d}\n", .{diff_seconds});
std.debug.print("Difference in minutes: {d}\n", .{@divTrunc(diff_seconds, 60)});
}