Add DateTime

This commit is contained in:
jaina heartles 2022-07-18 00:03:29 -07:00
parent f28f71c978
commit b1190ddea8
2 changed files with 51 additions and 0 deletions

49
src/util/DateTime.zig Normal file
View file

@ -0,0 +1,49 @@
const DateTime = @This();
const std = @import("std");
seconds_since_epoch: i64,
pub fn now() DateTime {
return .{ .seconds_since_epoch = std.time.timestamp() };
}
pub fn epochSeconds(value: DateTime) std.time.epoch.EpochSeconds {
return .{ .secs = @intCast(u64, value.seconds_since_epoch) };
}
pub fn year(value: DateTime) std.time.epoch.Year {
return value.epochSeconds().getEpochDay().calculateYearDay().year;
}
pub fn month(value: DateTime) std.time.epoch.Month {
return value.epochSeconds().getEpochDay().calculateYearDay().calculateMonthDay().month;
}
pub fn day(value: DateTime) u9 {
return value.epochSeconds().getEpochDay().calculateYearDay().calculateMonthDay().day;
}
pub fn hour(value: DateTime) u5 {
return value.epochSeconds().getDaySeconds().getHoursIntoDay();
}
pub fn minute(value: DateTime) u6 {
return value.epochSeconds().getDaySeconds().getMinutesIntoHour();
}
pub fn second(value: DateTime) u6 {
return value.epochSeconds().getDaySeconds().getSecondsIntoMinute();
}
pub fn format(value: DateTime, comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) !void {
return std.fmt.format(
writer,
"{}-{}-{}T{}{}{}",
.{ value.year(), value.month().numeric(), value.day(), value.hour(), value.minute(), value.second() },
);
}
pub fn jsonStringify(value: DateTime, _: std.json.StringifyOptions, writer: anytype) !void {
try std.fmt.format(writer, "\"{}\"", .{value});
}

View file

@ -1,9 +1,11 @@
pub const ciutf8 = @import("./ciutf8.zig");
pub const Uuid = @import("./Uuid.zig");
pub const DateTime = @import("./DateTime.zig");
pub const PathIter = @import("./PathIter.zig");
test {
_ = ciutf8;
_ = Uuid;
_ = PathIter;
_ = DateTime;
}