Bring up to zig 0.11

This commit is contained in:
Nathan Fisher 2023-09-10 18:30:09 -04:00
parent 9547839328
commit 2d2b99c954
6 changed files with 34 additions and 25 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

@ -114,7 +114,7 @@ pub const DateTime = struct {
seconds -= 60; seconds -= 60;
minutes -= 1; minutes -= 1;
} }
const second = @intCast(u6, seconds + 60); const second: u6 = @intCast(seconds + 60);
return Self{ return Self{
.year = year, .year = year,
.month = month.?, .month = month.?,
@ -145,10 +145,10 @@ pub const DateTime = struct {
return Self{ return Self{
.year = year, .year = year,
.month = month, .month = month,
.day = @intCast(u8, day), .day = @intCast(day),
.hour = @intCast(u5, hour), .hour = @intCast(hour),
.minute = @intCast(u6, minute), .minute = @intCast(minute),
.second = @intCast(u6, seconds), .second = @intCast(seconds),
.tz = .utc, .tz = .utc,
}; };
} else { } else {
@ -171,7 +171,7 @@ pub const DateTime = struct {
pub fn weekday(self: Self) WeekDay { pub fn weekday(self: Self) WeekDay {
const ts = self.toTimestamp(); const ts = self.toTimestamp();
const days = @divTrunc(ts, SECONDS_PER_DAY); const days = @divTrunc(ts, SECONDS_PER_DAY);
return @intToEnum(WeekDay, @rem(days, 7)); return @enumFromInt(@rem(days, 7));
} }
pub fn compare(self: Self, other: Self) Comparison { pub fn compare(self: Self, other: Self) Comparison {
@ -190,7 +190,7 @@ pub const DateTime = struct {
_ = options; _ = options;
try writer.print("{s}-{d:0>2}-{d:0>2}", .{ try writer.print("{s}-{d:0>2}-{d:0>2}", .{
self.year, @enumToInt(self.month), self.day, self.year, @intFromEnum(self.month), self.day,
}); });
if (self.hour) |h| { if (self.hour) |h| {
try writer.print("T{d:0>2}", .{h}); try writer.print("T{d:0>2}", .{h});
@ -209,7 +209,7 @@ pub const DateTime = struct {
writer: anytype, writer: anytype,
) !void { ) !void {
try writer.print("{s}{d:0>2}{d:0>2}", .{ try writer.print("{s}{d:0>2}{d:0>2}", .{
self.year, @enumToInt(self.month), self.day, self.year, @intFromEnum(self.month), self.day,
}); });
if (self.hour) |h| { if (self.hour) |h| {
try writer.print("T{d:0>2}", .{h}); try writer.print("T{d:0>2}", .{h});

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

@ -80,7 +80,7 @@ test "new timezone utc" {
debug.print("Passed\n", .{}); 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,

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