Move Month
into module
This commit is contained in:
parent
61757c9336
commit
2578b0f4fa
125
src/main.zig
125
src/main.zig
@ -2,94 +2,12 @@ const std = @import("std");
|
||||
const debug = std.debug;
|
||||
const testing = std.testing;
|
||||
pub const Year = @import("year.zig").Year;
|
||||
pub const Month = @import("month.zig").Month;
|
||||
|
||||
pub const SECONDS_PER_MINUTE = 60;
|
||||
pub const SECONDS_PER_HOUR = 60 * 60;
|
||||
pub const SECONDS_PER_DAY = SECONDS_PER_HOUR * 24;
|
||||
|
||||
pub const Month = enum(u4) {
|
||||
january = 1,
|
||||
february = 2,
|
||||
march = 3,
|
||||
april = 4,
|
||||
may = 5,
|
||||
june = 6,
|
||||
july = 7,
|
||||
august = 8,
|
||||
september = 9,
|
||||
october = 10,
|
||||
november = 11,
|
||||
december = 12,
|
||||
|
||||
const Self = @This();
|
||||
|
||||
pub fn days(self: Self, year: Year) u5 {
|
||||
return switch (@enumToInt(self)) {
|
||||
1, 3, 5, 7, 8, 10, 12 => 31,
|
||||
2 => switch (year) {
|
||||
.normal => 28,
|
||||
.leap => 29,
|
||||
},
|
||||
else => 30,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn seconds(self: Self, year: Year) u32 {
|
||||
return @as(u32, self.days(year)) * SECONDS_PER_DAY;
|
||||
}
|
||||
|
||||
pub fn next(self: Self) ?Self {
|
||||
const num = @enumToInt(self);
|
||||
return if (num < 12) @intToEnum(Self, num + 1) else null;
|
||||
}
|
||||
|
||||
pub fn previous(self: Self) ?Self {
|
||||
const num = @enumToInt(self);
|
||||
return if (num > 1) @intToEnum(Self, num - 1) else null;
|
||||
}
|
||||
};
|
||||
|
||||
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;
|
||||
try testing.expectEqual(month.days(year), 28);
|
||||
debug.print("Passed\n", .{});
|
||||
}
|
||||
|
||||
test "next month" {
|
||||
try testing.expectEqual(Month.june.next(), .july);
|
||||
try testing.expectEqual(Month.december.next(), null);
|
||||
debug.print("Passed\n", .{});
|
||||
}
|
||||
|
||||
test "last month" {
|
||||
try testing.expectEqual(Month.june.previous(), .may);
|
||||
try testing.expectEqual(Month.january.previous(), null);
|
||||
debug.print("Passed\n", .{});
|
||||
}
|
||||
|
||||
pub const TimeZoneTag = enum(u1) {
|
||||
utc,
|
||||
offset,
|
||||
@ -143,6 +61,47 @@ pub const Offset = union(Sign) {
|
||||
}
|
||||
};
|
||||
|
||||
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;
|
||||
try testing.expectEqual(month.days(year), 28);
|
||||
debug.print("Passed\n", .{});
|
||||
}
|
||||
|
||||
test "next month" {
|
||||
try testing.expectEqual(Month.june.next(), .july);
|
||||
try testing.expectEqual(Month.december.next(), null);
|
||||
debug.print("Passed\n", .{});
|
||||
}
|
||||
|
||||
test "last month" {
|
||||
try testing.expectEqual(Month.june.previous(), .may);
|
||||
try testing.expectEqual(Month.january.previous(), null);
|
||||
debug.print("Passed\n", .{});
|
||||
}
|
||||
|
||||
test "new offsets" {
|
||||
try testing.expectEqual(Offset.new(-5, null), Offset{ .negative = .{ .hours = 5, .minutes = null } });
|
||||
try testing.expectEqual(Offset.new(3, null), Offset{ .positive = .{ .hours = 3, .minutes = null } });
|
||||
|
47
src/month.zig
Normal file
47
src/month.zig
Normal file
@ -0,0 +1,47 @@
|
||||
const std = @import("std");
|
||||
const debug = std.debug;
|
||||
const testing = std.testing;
|
||||
const Year = @import("year.zig").Year;
|
||||
const SECONDS_PER_DAY = @import("main.zig").SECONDS_PER_DAY;
|
||||
|
||||
pub const Month = enum(u4) {
|
||||
january = 1,
|
||||
february = 2,
|
||||
march = 3,
|
||||
april = 4,
|
||||
may = 5,
|
||||
june = 6,
|
||||
july = 7,
|
||||
august = 8,
|
||||
september = 9,
|
||||
october = 10,
|
||||
november = 11,
|
||||
december = 12,
|
||||
|
||||
const Self = @This();
|
||||
|
||||
pub fn days(self: Self, year: Year) u5 {
|
||||
return switch (@enumToInt(self)) {
|
||||
1, 3, 5, 7, 8, 10, 12 => 31,
|
||||
2 => switch (year) {
|
||||
.normal => 28,
|
||||
.leap => 29,
|
||||
},
|
||||
else => 30,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn seconds(self: Self, year: Year) u32 {
|
||||
return @as(u32, self.days(year)) * SECONDS_PER_DAY;
|
||||
}
|
||||
|
||||
pub fn next(self: Self) ?Self {
|
||||
const num = @enumToInt(self);
|
||||
return if (num < 12) @intToEnum(Self, num + 1) else null;
|
||||
}
|
||||
|
||||
pub fn previous(self: Self) ?Self {
|
||||
const num = @enumToInt(self);
|
||||
return if (num > 1) @intToEnum(Self, num - 1) else null;
|
||||
}
|
||||
};
|
Loading…
Reference in New Issue
Block a user