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

View File

@ -12,45 +12,54 @@ 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" {
@ -60,6 +69,7 @@ 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" {
@ -67,6 +77,7 @@ 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" {
@ -80,6 +91,7 @@ test "get year from DateTime" {
.tz = TimeZone.new(-5, null).?,
};
try testing.expectEqual(dt.getYear(), 2023);
debug.print("Passed\n", .{});
}
test "get offset" {
@ -93,6 +105,7 @@ 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" {
@ -106,6 +119,7 @@ test "to timestamp utc" {
.tz = .utc,
};
try testing.expectEqual(dt.toTimestamp(), 1686633682);
debug.print("Passed\n", .{});
}
test "to timestamp negative offset" {
@ -119,12 +133,14 @@ 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" {
@ -136,6 +152,7 @@ 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" {
@ -149,6 +166,7 @@ test "get weekday" {
.tz = .utc,
};
try testing.expectEqual(dt.weekday(), .tuesday);
debug.print("Passed\n", .{});
}
test "get weekday 2" {
@ -162,6 +180,7 @@ test "get weekday 2" {
.tz = .utc,
};
try testing.expectEqual(dt.weekday(), .saturday);
debug.print("Passed\n", .{});
}
test "ordering lt" {
@ -184,6 +203,7 @@ test "ordering lt" {
.tz = .utc,
};
try testing.expectEqual(a.compare(b), .lt);
debug.print("Passed\n", .{});
}
test "ordering gt" {
@ -206,6 +226,7 @@ test "ordering gt" {
.tz = TimeZone.new(1, null).?,
};
try testing.expectEqual(a.compare(b), .gt);
debug.print("Passed\n", .{});
}
test "custom fmt" {
@ -225,6 +246,7 @@ 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" {
@ -249,4 +271,5 @@ 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", .{});
}