authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-10-23 18:10:52 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-10-23 18:10:52 -07:00
log311bc3c3300f1cd219cc6961e948f33c090e3465
treea548f97d336463414873e4441b6f777d966dc7bf
parentf824ecbf41935e1fcbd84360cf679ddbcf4c67ca

add rest of month formatters


2 files changed, 28 insertions(+), 0 deletions(-)

main.zig+8
...@@ -20,3 +20,11 @@ test { try assertOk(0, "YYY-MM-DD HH:mm:ss", "1970-01-01 00:00:00");...@@ -20,3 +20,11 @@ test { try assertOk(0, "YYY-MM-DD HH:mm:ss", "1970-01-01 00:00:00");
20test { try assertOk(1257894000000, "YYY-MM-DD HH:mm:ss", "2009-11-10 23:00:00"); }20test { try assertOk(1257894000000, "YYY-MM-DD HH:mm:ss", "2009-11-10 23:00:00"); }
21test { try assertOk(1634858430000, "YYY-MM-DD HH:mm:ss", "2021-10-21 23:20:30"); }21test { try assertOk(1634858430000, "YYY-MM-DD HH:mm:ss", "2021-10-21 23:20:30"); }
22test { try assertOk(1634858430023, "YYY-MM-DD HH:mm:ss.SSS", "2021-10-21 23:20:30.023"); }22test { try assertOk(1634858430023, "YYY-MM-DD HH:mm:ss.SSS", "2021-10-21 23:20:30.023"); }
23test { try assertOk(1144509852789, "YYY-MM-DD HH:mm:ss.SSS", "2006-04-08 15:24:12.789"); }
24
25// test every field
26test { try assertOk(1144509852789, "M", "4"); }
27test { try assertOk(1144509852789, "Mo", "4th"); }
28test { try assertOk(1144509852789, "MM", "04"); }
29test { try assertOk(1144509852789, "MMM", "Apr"); }
30test { try assertOk(1144509852789, "MMMM", "April"); }
time.zig+20
...@@ -228,11 +228,17 @@ pub const DateTime = struct {...@@ -228,11 +228,17 @@ pub const DateTime = struct {
228 .SSS => try writer.print("{:0>3}", .{self.ms}),228 .SSS => try writer.print("{:0>3}", .{self.ms}),
229 .MM => try writer.print("{:0>2}", .{self.months + 1}),229 .MM => try writer.print("{:0>2}", .{self.months + 1}),
230 .z => try writer.writeAll(@tagName(self.timezone)),230 .z => try writer.writeAll(@tagName(self.timezone)),
231 .M => try writer.print("{}", .{self.months + 1}),
232 .Mo => try printOrdinal(writer, self.months + 1),
231233
232 .MMM => {234 .MMM => {
233 const names = [_]string{ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };235 const names = [_]string{ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
234 try writer.writeAll(names[self.months]);236 try writer.writeAll(names[self.months]);
235 },237 },
238 .MMMM => try printLongName(writer, self.months, &[_]string{
239 "January", "February", "March", "April", "May", "June",
240 "July", "August", "September", "October", "November", "December",
241 }),
236242
237 else => @compileError("'" ++ @tagName(tag) ++ "' not currently supported"),243 else => @compileError("'" ++ @tagName(tag) ++ "' not currently supported"),
238 }244 }
...@@ -361,3 +367,17 @@ pub const WeekDay = enum {...@@ -361,3 +367,17 @@ pub const WeekDay = enum {
361 };367 };
362 }368 }
363};369};
370
371fn printOrdinal(writer: anytype, num: u16) !void {
372 try writer.print("{}", .{num});
373 try writer.writeAll(switch (num) {
374 1 => "st",
375 2 => "nd",
376 3 => "rd",
377 else => "th",
378 });
379}
380
381fn printLongName(writer: anytype, index: u16, names: []const string) !void {
382 try writer.writeAll(names[index]);
383}