Compare commits

...

2 Commits

2 changed files with 7 additions and 30 deletions

View File

@ -114,7 +114,7 @@ pub const DateTime = struct {
seconds -= 60;
minutes -= 1;
}
const second: u6 = @intCast(seconds + 60);
const second = @as(u6, @intCast(seconds + 60));
return Self{
.year = year,
.month = month.?,
@ -145,15 +145,15 @@ pub const DateTime = struct {
return Self{
.year = year,
.month = month,
.day = @intCast(day),
.hour = @intCast(hour),
.minute = @intCast(minute),
.second = @intCast(seconds),
.day = @as(u8, @intCast(day)),
.hour = @as(u5, @intCast(hour)),
.minute = @as(u6, @intCast(minute)),
.second = @as(u6, @intCast(seconds)),
.tz = .utc,
};
} else {
return Self{
.year = Year.new(0),
.year = Year.new(1970),
.month = .january,
.day = 1,
.hour = 0,
@ -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 @enumFromInt(@rem(days, 7));
return @as(WeekDay, @enumFromInt(@rem(days, 7)));
}
pub fn compare(self: Self, other: Self) Comparison {

View File

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