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");
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
// 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");
lib.setBuildMode(mode);
lib.install();
const lib = b.addStaticLibrary(.{
.name = "chrono",
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
b.installArtifact(lib);
const main_tests = b.addTest("src/tests.zig");
main_tests.setBuildMode(mode);
const main_tests = b.addTest(.{
.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");
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;
minutes -= 1;
}
const second = @intCast(u6, seconds + 60);
const second: u6 = @intCast(seconds + 60);
return Self{
.year = year,
.month = month.?,
@ -145,10 +145,10 @@ pub const DateTime = struct {
return Self{
.year = year,
.month = month,
.day = @intCast(u8, day),
.hour = @intCast(u5, hour),
.minute = @intCast(u6, minute),
.second = @intCast(u6, seconds),
.day = @intCast(day),
.hour = @intCast(hour),
.minute = @intCast(minute),
.second = @intCast(seconds),
.tz = .utc,
};
} else {
@ -171,7 +171,7 @@ pub const DateTime = struct {
pub fn weekday(self: Self) WeekDay {
const ts = self.toTimestamp();
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 {
@ -190,7 +190,7 @@ pub const DateTime = struct {
_ = options;
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| {
try writer.print("T{d:0>2}", .{h});
@ -209,7 +209,7 @@ pub const DateTime = struct {
writer: anytype,
) !void {
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| {
try writer.print("T{d:0>2}", .{h});

View File

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

View File

@ -80,7 +80,7 @@ test "new timezone utc" {
debug.print("Passed\n", .{});
}
test "get year" {
test "get year from DateTime" {
const dt = DateTime{
.year = Year.new(2023),
.month = .june,

View File

@ -32,10 +32,10 @@ pub const Offset = union(Sign) {
if (hours == 0 and m == 0) return null;
} else if (hours == 0) return null;
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 } };
} 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();
if (year > 0) {
try writer.print("{d:0>4}", .{@intCast(u32, year)});
try writer.print("{d:0>4}", .{@as(u32, @intCast(year))});
} else {
try writer.print("{d:0>4}", .{year});
}