const std = @import("std"); const Writer = std.Io.Writer; const datetime = @import("root.zig"); const Date = datetime.Date; const DateTime = datetime.DateTime; const Time = datetime.Time; pub const test_time = struct { expected: Time, actual: Time, pub fn init(e: Time, a: Time) test_time { return .{ .expected = e, .actual = a, }; } }; pub const test_date = struct { expected: Date, actual: Date, pub fn init(e: Date, a: Date) test_date { return .{ .expected = e, .actual = a, }; } }; pub const TestStruct = struct { date: ?Date = null, time: ?Time = null, datetime: ?DateTime = null, }; pub const testing = @import("testing.zig"); /// Title cases enums pub fn titleCase(comptime T: type, e: T) []const u8 { comptime std.debug.assert(@typeInfo(T) == .@"enum"); inline for (std.meta.fields(T)) |field| { if (e == @field(T, field.name)) { const title: *const [field.name.len]u8 = comptime blk: { break :blk &([1]u8{std.ascii.toUpper(field.name[0])} ++ field.name[1..field.name.len].*); }; return title; } } unreachable; } pub fn writeDate(into: []u8, date: Date) u8 { var buf: []u8 = undefined; // cast year to a u16 so it doesn't insert a sign // we don't want the + sign, ever // and we don't even want it to insert the - sign, because it screws up // the padding (we need to do it ourselfs) const year = date.year; var writer: Writer = .fixed(into); var y: u16 = undefined; if (year < 0) { writer.writeByte('-') catch unreachable; y = @intCast(year * -1); } else y = @intCast(year); writer.printInt(y, 10, .lower, .{ .width = 4, .fill = '0' }) catch unreachable; buf = if (year < 0) into[5..] else into[4..]; buf[0] = '-'; buf[1..3].* = paddingTwoDigits(@intFromEnum(date.month)); buf[3] = '-'; buf[4..6].* = paddingTwoDigits(date.day); // return the length of the string. 10 for positive year, 11 for negative return if (year < 0) 11 else 10; } pub fn writeTime(into: []u8, time: Time) u8 { into[0..2].* = paddingTwoDigits(time.hour); into[2] = ':'; into[3..5].* = paddingTwoDigits(time.min); into[5] = ':'; into[6..8].* = paddingTwoDigits(time.sec); const micros = time.micros; if (micros == 0) { return 8; } if (@rem(micros, 1000) == 0) { into[8] = '.'; var writer = std.Io.Writer.fixed(into[9..12]); writer.printInt(micros / 1000, 10, .lower, .{ .width = 3, .fill = '0' }) catch unreachable; return 12; } into[8] = '.'; var writer = std.Io.Writer.fixed(into[9..15]); writer.printInt(micros, 10, .lower, .{ .width = 6, .fill = '0' }) catch unreachable; return 15; } pub fn paddingTwoDigits(value: usize) [2]u8 { std.debug.assert(value < 61); const digits = "0001020304050607080910111213141516171819" ++ "2021222324252627282930313233343536373839" ++ "4041424344454647484950515253545556575859" ++ "60"; return digits[value * 2 ..][0..2].*; }