Something went wrong. Try again.
experimental verison of karlseguin's zul/datetime
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295const Date = @This();
const std = @import("std");const Writer = std.Io.Writer;const Stringify = std.json.Stringify;const Allocator = std.mem.Allocator;
const Parser = @import("Parser.zig");const datetime = @import("root.zig");const DateTime = datetime.DateTime;const Error = datetime.Error;const Month = datetime.Month;const Weekday = datetime.Weekday;const utils = @import("utils.zig");const t = utils.testing;const TestStruct = utils.TestStruct;const test_date = utils.test_date;
year: i16,month: Month,day: u8,
pub const Format = enum { iso8601, rfc3339,};
pub fn init(year: i16, month: Month, day: u8) !Date { if (!Date.valid(year, month, day)) return Error.InvalidDate;
return .{ .year = year, .month = month, .day = day, };}
pub fn isLeapYear(year: i16) bool { return (@rem(year, 4) == 0) and (@rem(year, 100) != 0 or @rem(year, 400) == 0);}
pub fn valid(year: i16, month: Month, day: u8) bool { if (day == 0) return false;
const month_days = [_]u8{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; const m: u4 = @intFromEnum(month); const max_days = if (month == .february and isLeapYear(year)) 29 else month_days[m - 1]; return day <= max_days;}
pub fn parse(fmt: Format, input: []const u8) !Date { var parser: Parser = .init(input);
const date = switch (fmt) { .rfc3339 => try parser.rfc3339Date(), .iso8601 => try parser.iso8601Date(), };
if (parser.unconsumed() != 0) return error.InvalidDate;
return date;}
pub fn order(a: Date, b: Date) std.math.Order { const year_order = std.math.order(a.year, b.year); if (year_order != .eq) return year_order;
const month_order = std.math.order(@intFromEnum(a.month), @intFromEnum(b.month)); if (month_order != .eq) return month_order;
return std.math.order(a.day, b.day);}
pub fn format(self: Date, writer: *Writer) !void { var buf: [11]u8 = undefined; const n = utils.writeDate(&buf, self); try writer.writeAll(buf[0..n]);}
pub fn jsonStringify(self: Date, out: anytype) !void { // Our goal here isn't to validate the date. It's to write what we have // in a YYYY-MM-DD format. If the data in Date isn't valid, that's not // our problem and we don't guarantee any reasonable output in such cases.
// std.fmt.formatInt is difficult to work with. The padding with signs // doesn't work and it'll always put a + sign given a signed integer with padding // So, for year, we always feed it an unsigned number (which avoids both issues) // and prepend the - if we need it.s var buf: [13]u8 = undefined; const n = utils.writeDate(buf[1..12], self); buf[0] = '"'; buf[n + 1] = '"'; try out.print("{s}", .{buf[0 .. n + 2]});}
pub fn jsonParse(allocator: Allocator, source: anytype, options: anytype) !Date { _ = options;
switch (try source.nextAlloc(allocator, .alloc_if_needed)) { inline .string, .allocated_string => |str| return Date.parse(.rfc3339, str) catch return error.InvalidCharacter, else => return error.UnexpectedToken, }}
pub fn weekday(self: Date) Weekday { var year: i16 = self.year; var day: i16 = undefined;
const m: i16 = @intFromEnum(self.month); if (m < 3) { day = @as(i16, self.day) + year; year -= 1; } else day = @as(i16, self.day) + year - 2;
const dow = std.math.cast( u3, @mod(@divTrunc(23 * m, 9) + day + 4 + @divTrunc(year, 4) - @divTrunc(year, 100) + @divTrunc(year, 400), 7), ).?; return @enumFromInt(dow);}
pub fn fromTimestamp(ns: i96) Date { _ = ns;}
test "Date: get week day" { const date1 = try parse(.rfc3339, "2025-05-25"); try t.expectEqual("Sunday", date1.weekday().name()); const date2 = try parse(.iso8601, "2023-05-25"); try t.expectEqual("Thursday", date2.weekday().name()); const date3 = try parse(.rfc3339, "1995-02-24"); try t.expectEqual("Friday", date3.weekday().name()); const date4 = try parse(.rfc3339, "1966-06-15"); try t.expectEqual("Wednesday", date4.weekday().name()); const date5 = try parse(.iso8601, "19570728"); try t.expectEqual("Sunday", date5.weekday().name());}
test "Date: json positive year" { // date, positive year const date: Date = .{ .year = 2023, .month = .september, .day = 22 }; const out = try Stringify.valueAlloc(t.allocator, date, .{}); defer t.allocator.free(out); try t.expectEqual("\"2023-09-22\"", out);}
test "Date: json negative year" { // date, negative year const date: Date = .{ .year = -4, .month = .december, .day = 3 }; const out = try Stringify.valueAlloc(t.allocator, date, .{}); defer t.allocator.free(out); try t.expectEqual("\"-0004-12-03\"", out);}
test "Date: json parseFromSlice" { // parse const ts = try std.json.parseFromSlice(TestStruct, t.allocator, "{\"date\":\"2023-09-22\"}", .{}); defer ts.deinit(); try t.expectEqual(Date{ .year = 2023, .month = .september, .day = 22 }, ts.value.date.?);}
test "Date: format positive year" { var buf: [20]u8 = undefined; const out = try std.fmt.bufPrint(&buf, "{f}", .{Date{ .year = 2023, .month = .may, .day = 22 }}); try t.expectEqual("2023-05-22", out);}
test "Date: format negative year" { var buf: [20]u8 = undefined; const out = try std.fmt.bufPrint(&buf, "{f}", .{Date{ .year = -102, .month = .december, .day = 9 }}); try t.expectEqual("-0102-12-09", out);}
test "Date: parse ISO8601" { for ([_]test_date{ //valid YYYY-MM-DD .init(.{ .year = 2023, .month = .may, .day = 22 }, try .parse(.iso8601, "2023-05-22")), .init(.{ .year = -2023, .month = .february, .day = 3 }, try .parse(.iso8601, "-2023-02-03")), .init(.{ .year = 1, .month = .february, .day = 3 }, try .parse(.iso8601, "0001-02-03")), .init(.{ .year = -1, .month = .february, .day = 3 }, try .parse(.iso8601, "-0001-02-03")),
//valid YYYYMMDD .init(.{ .year = 2023, .month = .may, .day = 22 }, try .parse(.iso8601, "20230522")), .init(.{ .year = -2023, .month = .february, .day = 3 }, try .parse(.iso8601, "-20230203")), .init(.{ .year = 1, .month = .february, .day = 3 }, try .parse(.iso8601, "00010203")), .init(.{ .year = -1, .month = .february, .day = 3 }, try .parse(.iso8601, "-00010203")), }) |td| try t.expectEqual(td.expected, td.actual);}
test "Date: parse RFC339" { for ([_]test_date{ //valid YYYY-MM-DD .init(.{ .year = 2023, .month = .may, .day = 22 }, try .parse(.rfc3339, "2023-05-22")), .init(.{ .year = -2023, .month = .february, .day = 3 }, try .parse(.rfc3339, "-2023-02-03")), .init(.{ .year = 1, .month = .february, .day = 3 }, try .parse(.rfc3339, "0001-02-03")), .init(.{ .year = -1, .month = .february, .day = 3 }, try .parse(.rfc3339, "-0001-02-03")), }) |td| try t.expectEqual(td.expected, td.actual);}
test "Date: parse RFC339 error cases" { for ([_][]const u8{ //valid YYYYMMDD "20230522", "-20230203", "00010203", "-00010203", }) |string| try t.expectError(Error.InvalidDate, parse(.rfc3339, string));}
test "Date: parse invalid common" { for (&[_]Date.Format{ .rfc3339, .iso8601 }) |fmt| { for ([_][]const u8{ // Invalid formats "", "2023/01-02", "0001-01-01 ", "2023-01/02", "2023-1-02", "2023-01-2", "9-01-2", "99-01-2", "99-01-2", "999-01-2", "-999-01-2", "-1-01-2",
// invalid month "2023-00-22", "2023-0A-22", "2023-13-22", "2023-99-22", "-2023-00-22", "-2023-13-22", "-2023-99-22",
// invalid day "2023-01-00", "2023-01-32", "2023-02-29", "2023-03-32", "2023-04-31", "2023-05-32", "2023-06-31", "2023-07-32", "2023-08-32", "2023-09-31", "2023-10-32", "2023-11-31", "2023-12-32", }) |string| try t.expectError(Error.InvalidDate, parse(fmt, string));
for ([_]test_date{ // valid (max day) .init(.{ .year = 2023, .month = .january, .day = 31 }, try .parse(fmt, "2023-01-31")), .init(.{ .year = 2023, .month = .january, .day = 31 }, try .parse(fmt, "2023-01-31")), .init(.{ .year = 2023, .month = .february, .day = 28 }, try .parse(fmt, "2023-02-28")), .init(.{ .year = 2023, .month = .march, .day = 31 }, try .parse(fmt, "2023-03-31")), .init(.{ .year = 2023, .month = .april, .day = 30 }, try .parse(fmt, "2023-04-30")), .init(.{ .year = 2023, .month = .may, .day = 31 }, try .parse(fmt, "2023-05-31")), .init(.{ .year = 2023, .month = .june, .day = 30 }, try .parse(fmt, "2023-06-30")), .init(.{ .year = 2023, .month = .july, .day = 31 }, try .parse(fmt, "2023-07-31")), .init(.{ .year = 2023, .month = .august, .day = 31 }, try .parse(fmt, "2023-08-31")), .init(.{ .year = 2023, .month = .september, .day = 30 }, try .parse(fmt, "2023-09-30")), .init(.{ .year = 2023, .month = .october, .day = 31 }, try .parse(fmt, "2023-10-31")), .init(.{ .year = 2023, .month = .november, .day = 30 }, try .parse(fmt, "2023-11-30")), .init(.{ .year = 2023, .month = .december, .day = 31 }, try .parse(fmt, "2023-12-31")),
// leap years .init(.{ .year = 2000, .month = .february, .day = 29 }, try .parse(fmt, "2000-02-29")), .init(.{ .year = 2400, .month = .february, .day = 29 }, try .parse(fmt, "2400-02-29")), .init(.{ .year = 2012, .month = .february, .day = 29 }, try .parse(fmt, "2012-02-29")), .init(.{ .year = 2024, .month = .february, .day = 29 }, try .parse(fmt, "2024-02-29")), }) |td| try t.expectEqual(td.expected, td.actual);
for ([_][]const u8{ // leap years "2000-02-30", "2400-02-30", "2012-02-30", "2024-02-30", "2100-02-29", "2200-02-29", }) |string| try t.expectError(error.InvalidDate, parse(fmt, string)); }}
test "Date: order" { { const a = Date{ .year = 2023, .month = .may, .day = 22 }; const b = Date{ .year = 2023, .month = .may, .day = 22 }; try t.expectEqual(std.math.Order.eq, a.order(b)); }
{ const a = Date{ .year = 2023, .month = .may, .day = 22 }; const b = Date{ .year = 2022, .month = .may, .day = 22 }; try t.expectEqual(std.math.Order.gt, a.order(b)); try t.expectEqual(std.math.Order.lt, b.order(a)); }
{ const a = Date{ .year = 2022, .month = .june, .day = 22 }; const b = Date{ .year = 2022, .month = .may, .day = 22 }; try t.expectEqual(std.math.Order.gt, a.order(b)); try t.expectEqual(std.math.Order.lt, b.order(a)); }
{ const a = Date{ .year = 2023, .month = .may, .day = 23 }; const b = Date{ .year = 2022, .month = .may, .day = 22 }; try t.expectEqual(std.math.Order.gt, a.order(b)); try t.expectEqual(std.math.Order.lt, b.order(a)); }}