Got conversion to timestamp working and passing tests
This commit is contained in:
parent
7bfdd02688
commit
f4c138b2a4
58
src/main.zig
58
src/main.zig
@ -35,7 +35,7 @@ pub const Year = union(YearTag) {
|
||||
}
|
||||
|
||||
pub fn seconds(self: Self) i64 {
|
||||
return self.days * SECONDS_PER_DAY;
|
||||
return @as(i64, self.days()) * SECONDS_PER_DAY;
|
||||
}
|
||||
|
||||
pub fn get(self: Self) u31 {
|
||||
@ -99,8 +99,8 @@ pub const Month = enum(u4) {
|
||||
};
|
||||
}
|
||||
|
||||
pub fn seconds(self: Self) u32 {
|
||||
return self.days() * SECONDS_PER_DAY;
|
||||
pub fn seconds(self: Self, year: Year) u32 {
|
||||
return @as(u32, self.days(year)) * SECONDS_PER_DAY;
|
||||
}
|
||||
|
||||
pub fn next(self: Self) ?Self {
|
||||
@ -252,35 +252,39 @@ pub const DateTime = struct {
|
||||
}
|
||||
|
||||
pub fn toTimestamp(self: Self) i64 {
|
||||
var seconds = 0;
|
||||
var seconds: i64 = 0;
|
||||
if (self.year.get() < 1970) {
|
||||
var year = Year.new(1970);
|
||||
while (year != self.year) {
|
||||
while (year.get() != self.year.get()) {
|
||||
year = year.previous();
|
||||
seconds -= year.seconds();
|
||||
}
|
||||
} else if (self.year.get() > 1970) {
|
||||
var year = Year.new(1970);
|
||||
while (year != self.year) {
|
||||
while (year.get() != self.year.get()) {
|
||||
seconds += year.seconds();
|
||||
year = year.next();
|
||||
}
|
||||
}
|
||||
var month = Month.january;
|
||||
while (month != self.month) {
|
||||
seconds += month.seconds();
|
||||
seconds += month.seconds(self.year);
|
||||
month = month.next().?;
|
||||
}
|
||||
seconds += (self.day - 1) * SECONDS_PER_DAY;
|
||||
if (self.hours) |h| {
|
||||
seconds += h * 3600;
|
||||
seconds += @as(i64, self.day - 1) * SECONDS_PER_DAY;
|
||||
if (self.hour) |h| {
|
||||
seconds += @as(i64, h - 1) * 3600;
|
||||
}
|
||||
if (self.minutes) |m| {
|
||||
seconds += m * 60;
|
||||
if (self.minute) |m| {
|
||||
seconds += @as(i64, m) * 60;
|
||||
}
|
||||
if (self.seconds) |s| {
|
||||
if (self.second) |s| {
|
||||
seconds += s;
|
||||
}
|
||||
switch (self.tz) {
|
||||
.utc => {},
|
||||
.offset => |ofs| seconds -= ofs.asSeconds(),
|
||||
}
|
||||
return seconds;
|
||||
}
|
||||
};
|
||||
@ -312,3 +316,31 @@ test "get offset" {
|
||||
};
|
||||
try testing.expectEqual(dt.getOffset().?, Offset{ .negative = .{ .hours = 5, .minutes = null } });
|
||||
}
|
||||
|
||||
test "to timestamp utc" {
|
||||
const dt = DateTime{
|
||||
.year = Year.new(2023),
|
||||
.month = .june,
|
||||
.day = 13,
|
||||
.hour = 6,
|
||||
.minute = 21,
|
||||
.second = 22,
|
||||
.nanos = null,
|
||||
.tz = .utc,
|
||||
};
|
||||
try testing.expectEqual(dt.toTimestamp(), 1686633682);
|
||||
}
|
||||
|
||||
test "to timestamp negative offset" {
|
||||
const dt = DateTime{
|
||||
.year = Year.new(2023),
|
||||
.month = .june,
|
||||
.day = 13,
|
||||
.hour = 1,
|
||||
.minute = 21,
|
||||
.second = 22,
|
||||
.nanos = null,
|
||||
.tz = TimeZone.new(-5, null).?,
|
||||
};
|
||||
try testing.expectEqual(dt.toTimestamp(), 1686633682);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user