Zig
Zig
How to Parse a TimeStamp (such as 2016-11-11T14:32:17.0908971Z)
Timestamps are frequently used in REST API responses. This example demonstrates how to parse a timestamp string to get at the date/time components in the local timezone or in the GMT/UTC timezone.Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main() !void {
// Let's say we have a timestamp string such as 2016-11-11T14:32:17.0908971Z
const str_timestamp = "2016-11-11T14:32:17.0908971Z";
const date_time = try chilkat.DateTime.init();
defer date_time.deinit();
date_time.setFromTimestamp(str_timestamp) catch {};
// Get a DtObj in the local timezone.
var b_local_timezone: bool = true;
const dt = try chilkat.DtObj.init();
defer dt.deinit();
date_time.toDtObj(b_local_timezone, dt);
// Get the individual date/time components
std.debug.print("-- Local Time --\n", .{});
std.debug.print("Year: {d}\n", .{dt.getYear()});
std.debug.print("Month: {d}\n", .{dt.getMonth()});
std.debug.print("Day: {d}\n", .{dt.getDay()});
std.debug.print("Hour: {d}\n", .{dt.getHour()});
std.debug.print("Minutes: {d}\n", .{dt.getMinute()});
std.debug.print("Seconds: {d}\n", .{dt.getSecond()});
// Get a DtObj in the GMT/UTC timezone.
b_local_timezone = false;
date_time.toDtObj(b_local_timezone, dt);
// Get the individual date/time components
std.debug.print("-- UTC Time --\n", .{});
std.debug.print("Year: {d}\n", .{dt.getYear()});
std.debug.print("Month: {d}\n", .{dt.getMonth()});
std.debug.print("Day: {d}\n", .{dt.getDay()});
std.debug.print("Hour: {d}\n", .{dt.getHour()});
std.debug.print("Minutes: {d}\n", .{dt.getMinute()});
std.debug.print("Seconds: {d}\n", .{dt.getSecond()});
}