diff --git a/src/main.zig b/src/main.zig index 28326c2..0ac24de 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,82 +1,12 @@ const std = @import("std"); const debug = std.debug; const testing = std.testing; +pub const Year = @import("year.zig").Year; pub const SECONDS_PER_MINUTE = 60; pub const SECONDS_PER_HOUR = 60 * 60; pub const SECONDS_PER_DAY = SECONDS_PER_HOUR * 24; -pub const YearTag = enum(u1) { - normal, - leap, - - fn new(year: i32) YearTag { - return if (@rem(year, 4) == 0 and (@rem(year, 100) != 0 or @rem(year, 400) == 0)) .leap else .normal; - } -}; - -pub const Year = union(YearTag) { - normal: i32, - leap: i32, - - const Self = @This(); - - pub fn new(year: i32) Self { - return switch (YearTag.new(year)) { - .normal => Self{ .normal = year }, - .leap => Self{ .leap = year }, - }; - } - - pub fn days(self: Self) u16 { - return switch (self) { - .normal => 365, - .leap => 366, - }; - } - - pub fn seconds(self: Self) i64 { - return @as(i64, self.days()) * SECONDS_PER_DAY; - } - - pub fn get(self: Self) i32 { - return switch (self) { - .normal => |year| year, - .leap => |year| year, - }; - } - - pub fn next(self: Self) Self { - return Self.new(self.get() + 1); - } - - pub fn previous(self: Self) Self { - return Self.new(self.get() - 1); - } -}; - -test "new year" { - try testing.expectEqual(Year.new(2023), Year{ .normal = 2023 }); - try testing.expectEqual(Year.new(2024), Year{ .leap = 2024 }); - debug.print("Passed\n", .{}); -} - -test "get year" { - try testing.expectEqual(Year.new(2023).get(), 2023); - try testing.expectEqual(Year.new(2024).get(), 2024); - debug.print("Passed\n", .{}); -} - -test "next year" { - try testing.expectEqual(Year.new(2023).next(), Year{ .leap = 2024 }); - debug.print("Passed\n", .{}); -} - -test "last year" { - try testing.expectEqual(Year.new(2024).previous(), Year{ .normal = 2023 }); - debug.print("Passed\n", .{}); -} - pub const Month = enum(u4) { january = 1, february = 2, @@ -119,6 +49,28 @@ pub const Month = enum(u4) { } }; +test "new year" { + try testing.expectEqual(Year.new(2023), Year{ .normal = 2023 }); + try testing.expectEqual(Year.new(2024), Year{ .leap = 2024 }); + debug.print("Passed\n", .{}); +} + +test "get year" { + try testing.expectEqual(Year.new(2023).get(), 2023); + try testing.expectEqual(Year.new(2024).get(), 2024); + debug.print("Passed\n", .{}); +} + +test "next year" { + try testing.expectEqual(Year.new(2023).next(), Year{ .leap = 2024 }); + debug.print("Passed\n", .{}); +} + +test "last year" { + try testing.expectEqual(Year.new(2024).previous(), Year{ .normal = 2023 }); + debug.print("Passed\n", .{}); +} + test "get days in month" { const year = Year.new(2023); const month = Month.february; diff --git a/src/year.zig b/src/year.zig new file mode 100644 index 0000000..1faa7d5 --- /dev/null +++ b/src/year.zig @@ -0,0 +1,53 @@ +const std = @import("std"); +const debug = std.debug; +const testing = std.testing; +const SECONDS_PER_DAY = @import("main.zig").SECONDS_PER_DAY; + +pub const YearTag = enum(u1) { + normal, + leap, + + fn new(year: i32) YearTag { + return if (@rem(year, 4) == 0 and (@rem(year, 100) != 0 or @rem(year, 400) == 0)) .leap else .normal; + } +}; + +pub const Year = union(YearTag) { + normal: i32, + leap: i32, + + const Self = @This(); + + pub fn new(year: i32) Self { + return switch (YearTag.new(year)) { + .normal => Self{ .normal = year }, + .leap => Self{ .leap = year }, + }; + } + + pub fn days(self: Self) u16 { + return switch (self) { + .normal => 365, + .leap => 366, + }; + } + + pub fn seconds(self: Self) i64 { + return @as(i64, self.days()) * SECONDS_PER_DAY; + } + + pub fn get(self: Self) i32 { + return switch (self) { + .normal => |year| year, + .leap => |year| year, + }; + } + + pub fn next(self: Self) Self { + return Self.new(self.get() + 1); + } + + pub fn previous(self: Self) Self { + return Self.new(self.get() - 1); + } +};