Merge branch 'odin' of git.hitchhiker-linux.org:jeang3nie/zig-chrono into odin

This commit is contained in:
Nathan Fisher 2024-01-29 23:35:52 -05:00
commit 8f4f120a3b
5 changed files with 26 additions and 40 deletions

View File

@ -1,17 +1,26 @@
const std = @import("std"); const std = @import("std");
pub fn build(b: *std.build.Builder) void { pub fn build(b: *std.Build) void {
// Standard release options allow the person running `zig build` to select // Standard release options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
const mode = b.standardReleaseOptions(); const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const lib = b.addStaticLibrary("chrono", "src/main.zig"); const lib = b.addStaticLibrary(.{
lib.setBuildMode(mode); .name = "chrono",
lib.install(); .root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
b.installArtifact(lib);
const main_tests = b.addTest("src/tests.zig"); const main_tests = b.addTest(.{
main_tests.setBuildMode(mode); .root_source_file = .{ .path = "src/tests.zig" },
.target = target,
.optimize = optimize,
});
const run_main_tests = b.addRunArtifact(main_tests);
const test_step = b.step("test", "Run library tests"); const test_step = b.step("test", "Run library tests");
test_step.dependOn(&main_tests.step); test_step.dependOn(&run_main_tests.step);
} }

View File

@ -21,7 +21,7 @@ pub const Month = enum(u4) {
const Self = @This(); const Self = @This();
pub fn days(self: Self, year: Year) u5 { pub fn days(self: Self, year: Year) u5 {
return switch (@enumToInt(self)) { return switch (@intFromEnum(self)) {
1, 3, 5, 7, 8, 10, 12 => 31, 1, 3, 5, 7, 8, 10, 12 => 31,
2 => switch (year) { 2 => switch (year) {
.normal => 28, .normal => 28,
@ -36,12 +36,12 @@ pub const Month = enum(u4) {
} }
pub fn next(self: Self) ?Self { pub fn next(self: Self) ?Self {
const num = @enumToInt(self); const num = @intFromEnum(self);
return if (num < 12) @intToEnum(Self, num + 1) else null; return if (num < 12) @enumFromInt(num + 1) else null;
} }
pub fn previous(self: Self) ?Self { pub fn previous(self: Self) ?Self {
const num = @enumToInt(self); const num = @intFromEnum(self);
return if (num > 1) @intToEnum(Self, num - 1) else null; return if (num > 1) @enumFromInt(num - 1) else null;
} }
}; };

View File

@ -12,54 +12,45 @@ const TimeZone = tz.TimeZone;
test "new year" { test "new year" {
try testing.expectEqual(Year.new(2023), Year{ .normal = 2023 }); try testing.expectEqual(Year.new(2023), Year{ .normal = 2023 });
try testing.expectEqual(Year.new(2024), Year{ .leap = 2024 }); try testing.expectEqual(Year.new(2024), Year{ .leap = 2024 });
debug.print("Passed\n", .{});
} }
test "get year" { test "get year" {
try testing.expectEqual(Year.new(2023).get(), 2023); try testing.expectEqual(Year.new(2023).get(), 2023);
try testing.expectEqual(Year.new(2024).get(), 2024); try testing.expectEqual(Year.new(2024).get(), 2024);
debug.print("Passed\n", .{});
} }
test "next year" { test "next year" {
try testing.expectEqual(Year.new(2023).next(), Year{ .leap = 2024 }); try testing.expectEqual(Year.new(2023).next(), Year{ .leap = 2024 });
debug.print("Passed\n", .{});
} }
test "last year" { test "last year" {
try testing.expectEqual(Year.new(2024).previous(), Year{ .normal = 2023 }); try testing.expectEqual(Year.new(2024).previous(), Year{ .normal = 2023 });
debug.print("Passed\n", .{});
} }
test "get days in month" { test "get days in month" {
const year = Year.new(2023); const year = Year.new(2023);
const month = Month.february; const month = Month.february;
try testing.expectEqual(month.days(year), 28); try testing.expectEqual(month.days(year), 28);
debug.print("Passed\n", .{});
} }
test "next month" { test "next month" {
try testing.expectEqual(Month.june.next(), .july); try testing.expectEqual(Month.june.next(), .july);
try testing.expectEqual(Month.december.next(), null); try testing.expectEqual(Month.december.next(), null);
debug.print("Passed\n", .{});
} }
test "last month" { test "last month" {
try testing.expectEqual(Month.june.previous(), .may); try testing.expectEqual(Month.june.previous(), .may);
try testing.expectEqual(Month.january.previous(), null); try testing.expectEqual(Month.january.previous(), null);
debug.print("Passed\n", .{});
} }
test "new offsets" { test "new offsets" {
try testing.expectEqual(Offset.new(-5, null), Offset{ .negative = .{ .hours = 5, .minutes = null } }); 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 } }); try testing.expectEqual(Offset.new(3, null), Offset{ .positive = .{ .hours = 3, .minutes = null } });
debug.print("Passed\n", .{});
} }
test "as seconds" { test "as seconds" {
try testing.expectEqual(Offset.new(-4, 30).?.asSeconds(), -16200); try testing.expectEqual(Offset.new(-4, 30).?.asSeconds(), -16200);
try testing.expectEqual(Offset.new(3, null).?.asSeconds(), 10800); try testing.expectEqual(Offset.new(3, null).?.asSeconds(), 10800);
debug.print("Passed\n", .{});
} }
test "new timezone" { test "new timezone" {
@ -69,7 +60,6 @@ test "new timezone" {
.offset => |ofs| try testing.expectEqual(ofs, Offset{ .negative = .{ .hours = 5, .minutes = null } }), .offset => |ofs| try testing.expectEqual(ofs, Offset{ .negative = .{ .hours = 5, .minutes = null } }),
else => unreachable, else => unreachable,
} }
debug.print("Passed\n", .{});
} }
test "new timezone utc" { test "new timezone utc" {
@ -77,10 +67,9 @@ test "new timezone utc" {
const tz1 = TimeZone.new(0, null).?; const tz1 = TimeZone.new(0, null).?;
try testing.expectEqual(@as(tz.TimeZoneTag, tz0), .utc); try testing.expectEqual(@as(tz.TimeZoneTag, tz0), .utc);
try testing.expectEqual(@as(tz.TimeZoneTag, tz1), .utc); try testing.expectEqual(@as(tz.TimeZoneTag, tz1), .utc);
debug.print("Passed\n", .{});
} }
test "get year" { test "get year from DateTime" {
const dt = DateTime{ const dt = DateTime{
.year = Year.new(2023), .year = Year.new(2023),
.month = .june, .month = .june,
@ -91,7 +80,6 @@ test "get year" {
.tz = TimeZone.new(-5, null).?, .tz = TimeZone.new(-5, null).?,
}; };
try testing.expectEqual(dt.getYear(), 2023); try testing.expectEqual(dt.getYear(), 2023);
debug.print("Passed\n", .{});
} }
test "get offset" { test "get offset" {
@ -105,7 +93,6 @@ test "get offset" {
.tz = TimeZone.new(-5, null).?, .tz = TimeZone.new(-5, null).?,
}; };
try testing.expectEqual(dt.getOffset().?, Offset{ .negative = .{ .hours = 5, .minutes = null } }); try testing.expectEqual(dt.getOffset().?, Offset{ .negative = .{ .hours = 5, .minutes = null } });
debug.print("Passed\n", .{});
} }
test "to timestamp utc" { test "to timestamp utc" {
@ -119,7 +106,6 @@ test "to timestamp utc" {
.tz = .utc, .tz = .utc,
}; };
try testing.expectEqual(dt.toTimestamp(), 1686633682); try testing.expectEqual(dt.toTimestamp(), 1686633682);
debug.print("Passed\n", .{});
} }
test "to timestamp negative offset" { test "to timestamp negative offset" {
@ -133,14 +119,12 @@ test "to timestamp negative offset" {
.tz = TimeZone.new(-5, null).?, .tz = TimeZone.new(-5, null).?,
}; };
try testing.expectEqual(dt.toTimestamp(), 1686633682); try testing.expectEqual(dt.toTimestamp(), 1686633682);
debug.print("Passed\n", .{});
} }
test "conversions" { test "conversions" {
const ts = std.time.timestamp(); const ts = std.time.timestamp();
const dt = DateTime.fromTimestamp(ts); const dt = DateTime.fromTimestamp(ts);
try testing.expectEqual(dt.toTimestamp(), ts); try testing.expectEqual(dt.toTimestamp(), ts);
debug.print("Passed\n", .{});
} }
test "now" { test "now" {
@ -152,7 +136,6 @@ test "now" {
const a = @divTrunc(ts, 10); const a = @divTrunc(ts, 10);
const b = @divTrunc(dt.toTimestamp(), 10); const b = @divTrunc(dt.toTimestamp(), 10);
try testing.expectEqual(a, b); try testing.expectEqual(a, b);
debug.print("Passed\n", .{});
} }
test "get weekday" { test "get weekday" {
@ -166,7 +149,6 @@ test "get weekday" {
.tz = .utc, .tz = .utc,
}; };
try testing.expectEqual(dt.weekday(), .tuesday); try testing.expectEqual(dt.weekday(), .tuesday);
debug.print("Passed\n", .{});
} }
test "get weekday 2" { test "get weekday 2" {
@ -180,7 +162,6 @@ test "get weekday 2" {
.tz = .utc, .tz = .utc,
}; };
try testing.expectEqual(dt.weekday(), .saturday); try testing.expectEqual(dt.weekday(), .saturday);
debug.print("Passed\n", .{});
} }
test "ordering lt" { test "ordering lt" {
@ -203,7 +184,6 @@ test "ordering lt" {
.tz = .utc, .tz = .utc,
}; };
try testing.expectEqual(a.compare(b), .lt); try testing.expectEqual(a.compare(b), .lt);
debug.print("Passed\n", .{});
} }
test "ordering gt" { test "ordering gt" {
@ -226,7 +206,6 @@ test "ordering gt" {
.tz = TimeZone.new(1, null).?, .tz = TimeZone.new(1, null).?,
}; };
try testing.expectEqual(a.compare(b), .gt); try testing.expectEqual(a.compare(b), .gt);
debug.print("Passed\n", .{});
} }
test "custom fmt" { test "custom fmt" {
@ -246,7 +225,6 @@ test "custom fmt" {
); );
defer testing.allocator.free(dt_string); defer testing.allocator.free(dt_string);
try testing.expect(mem.eql(u8, dt_string, "2023-06-10T06:21:22Z")); try testing.expect(mem.eql(u8, dt_string, "2023-06-10T06:21:22Z"));
debug.print("Passed\n", .{});
} }
test "fmt basic" { test "fmt basic" {
@ -271,5 +249,4 @@ test "fmt basic" {
); );
defer testing.allocator.free(dt_string); defer testing.allocator.free(dt_string);
try testing.expect(mem.eql(u8, dt_string, "2023-06-10T06:21:22-04")); try testing.expect(mem.eql(u8, dt_string, "2023-06-10T06:21:22-04"));
debug.print("Passed\n", .{});
} }

View File

@ -32,10 +32,10 @@ pub const Offset = union(Sign) {
if (hours == 0 and m == 0) return null; if (hours == 0 and m == 0) return null;
} else if (hours == 0) return null; } else if (hours == 0) return null;
if (hours < 0) { if (hours < 0) {
const h = @intCast(u4, @as(i8, hours) * -1); const h: u4 = @intCast(@as(i8, hours) * -1);
return Self{ .negative = .{ .hours = h, .minutes = minutes } }; return Self{ .negative = .{ .hours = h, .minutes = minutes } };
} else { } else {
return Self{ .positive = .{ .hours = @intCast(u4, hours), .minutes = minutes } }; return Self{ .positive = .{ .hours = @intCast(hours), .minutes = minutes } };
} }
} }

View File

@ -62,7 +62,7 @@ pub const Year = union(YearTag) {
const year = self.get(); const year = self.get();
if (year > 0) { if (year > 0) {
try writer.print("{d:0>4}", .{@intCast(u32, year)}); try writer.print("{d:0>4}", .{@as(u32, @intCast(year))});
} else { } else {
try writer.print("{d:0>4}", .{year}); try writer.print("{d:0>4}", .{year});
} }