From a06a638736a0894984fdd16d79bd5994c33ad619 Mon Sep 17 00:00:00 2001 From: zzstoatzz Date: Thu, 13 Aug 2026 20:56:48 -0500 Subject: [PATCH] example: zat.Datetime replaces the hand-rolled 45-line datetime parser Co-Authored-By: Claude Fable 5 --- build.zig.zon | 2 +- examples/streamplace_chat.zig | 45 ++--------------------------------- 2 files changed, 3 insertions(+), 44 deletions(-) diff --git a/build.zig.zon b/build.zig.zon index fe620bc..fcd434d 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -10,7 +10,7 @@ }, .zat = .{ .url = "https://tangled.org/zat.dev/zat/archive/main", - .hash = "zat-0.3.30-5PuC7rdBDACJTxCH1TwCh-kSPEUZxbIWDpF9D_k_k0kh", + .hash = "zat-0.3.30-5PuC7iRkDAAusYf4oaOtbmZkGf-ZZ9gEU60ySf_xtuu6", }, }, .paths = .{ "build.zig", "build.zig.zon", "src", "examples" }, diff --git a/examples/streamplace_chat.zig b/examples/streamplace_chat.zig index 5cc3691..9fbcae0 100644 --- a/examples/streamplace_chat.zig +++ b/examples/streamplace_chat.zig @@ -351,49 +351,8 @@ fn copySegment(allocator: std.mem.Allocator, uri: []const u8, value: std.json.Va } fn parseUtcMicros(value: []const u8) !i64 { - if (value.len < 20 or value[4] != '-' or value[7] != '-' or value[10] != 'T' or - value[13] != ':' or value[16] != ':' or value[value.len - 1] != 'Z') - return error.InvalidDatetime; - - const year = try std.fmt.parseInt(u16, value[0..4], 10); - const month_int = try std.fmt.parseInt(u4, value[5..7], 10); - const day = try std.fmt.parseInt(u5, value[8..10], 10); - const hour = try std.fmt.parseInt(u5, value[11..13], 10); - const minute = try std.fmt.parseInt(u6, value[14..16], 10); - const second = try std.fmt.parseInt(u6, value[17..19], 10); - if (year < std.time.epoch.epoch_year or month_int < 1 or month_int > 12 or - hour > 23 or minute > 59 or second > 59) - return error.InvalidDatetime; - - const month: std.time.epoch.Month = @enumFromInt(month_int); - if (day < 1 or day > std.time.epoch.getDaysInMonth(year, month)) return error.InvalidDatetime; - - var days: i64 = 0; - var y: u16 = std.time.epoch.epoch_year; - while (y < year) : (y += 1) days += std.time.epoch.getDaysInYear(y); - var m: u4 = 1; - while (m < month_int) : (m += 1) { - days += std.time.epoch.getDaysInMonth(year, @enumFromInt(m)); - } - days += @as(i64, day) - 1; - - var micros = (days * std.time.s_per_day + @as(i64, hour) * std.time.s_per_hour + - @as(i64, minute) * std.time.s_per_min + @as(i64, second)) * std.time.us_per_s; - if (value.len > 20) { - if (value[19] != '.') return error.InvalidDatetime; - const fraction = value[20 .. value.len - 1]; - if (fraction.len == 0) return error.InvalidDatetime; - var fractional_us: i64 = 0; - var digits: usize = 0; - while (digits < fraction.len and digits < 6) : (digits += 1) { - const c = fraction[digits]; - if (c < '0' or c > '9') return error.InvalidDatetime; - fractional_us = fractional_us * 10 + (c - '0'); - } - while (digits < 6) : (digits += 1) fractional_us *= 10; - micros += fractional_us; - } - return micros; + const dt = zat.Datetime.parse(value) orelse return error.InvalidDatetime; + return dt.micros; } fn latestLivestreamUri( -- 2.51.2