Move `Year` into module

This commit is contained in:
Nathan Fisher 2023-06-13 15:50:45 -04:00
parent 536228c9ee
commit 61757c9336
2 changed files with 76 additions and 71 deletions

View File

@ -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;

53
src/year.zig Normal file
View File

@ -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);
}
};