Compare commits

..

No commits in common. "8f4f120a3b40a202eade75b743245e51406c0c8a" and "2d2b99c954d21edd5a75f597014ec15ce6042065" have entirely different histories.

2 changed files with 30 additions and 7 deletions

View File

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

View File

@ -12,45 +12,54 @@ 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" {
@ -60,6 +69,7 @@ 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" {
@ -67,6 +77,7 @@ 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" {
@ -80,6 +91,7 @@ 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" {
@ -93,6 +105,7 @@ 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" {
@ -106,6 +119,7 @@ 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" {
@ -119,12 +133,14 @@ 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" {
@ -136,6 +152,7 @@ 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" {
@ -149,6 +166,7 @@ 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" {
@ -162,6 +180,7 @@ 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" {
@ -184,6 +203,7 @@ 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" {
@ -206,6 +226,7 @@ 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" {
@ -225,6 +246,7 @@ 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" {
@ -249,4 +271,5 @@ 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", .{});
} }