diff --git a/src/util/DateTime.zig b/src/util/DateTime.zig new file mode 100644 index 0000000..79520ce --- /dev/null +++ b/src/util/DateTime.zig @@ -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}); +} diff --git a/src/util/lib.zig b/src/util/lib.zig index a6a98b5..6e57425 100644 --- a/src/util/lib.zig +++ b/src/util/lib.zig @@ -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; }