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

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,7 +67,6 @@ 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 from DateTime" { test "get year from DateTime" {
@ -91,7 +80,6 @@ test "get year from DateTime" {
.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", .{});
} }