Skip to content
This repository was archived by the owner on Apr 18, 2025. It is now read-only.

Commit 9e9a9b2

Browse files
committed
feat(time): parsing hour minute without seconds is now possible
1 parent 600cf4b commit 9e9a9b2

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/topology/time.zig

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ pub fn isDate(string: []const u8) bool {
341341
/// Is the string a HH:MM:SS format?
342342
pub fn isTime(string: []const u8) bool {
343343
if (formattedDigitFilter(':', string)) |count| {
344-
if (count == 2) return true;
344+
if (count >= 1) return true;
345345
}
346346
return false;
347347
}
@@ -416,10 +416,10 @@ pub fn shiftBack(t: Time, index: usize) Date {
416416

417417
/// Convert a string to an HH MM SS timestamp
418418
pub fn toTimestamp(string: []const u8) !Timestamp {
419-
if (string.len < 8) return Error.DateTooShort;
419+
if (string.len < 5) return Error.DateTooShort;
420420
const hour = try std.fmt.parseInt(u8, string[0..2], 10);
421421
const minute = try std.fmt.parseInt(u8, string[3..5], 10);
422-
const seconds = try std.fmt.parseInt(u8, string[6..8], 10);
422+
const seconds = if (string.len > 5) try std.fmt.parseInt(u8, string[6..8], 10) else 0;
423423
return .{ .hour = hour, .minute = minute, .second = seconds };
424424
}
425425

@@ -684,6 +684,12 @@ test "time parsing" {
684684
"monday 18:00:00",
685685
nowish.shiftDays(5).shiftHours(5),
686686
);
687+
// seconds should be optional
688+
try testTimeParsing(
689+
nowish,
690+
"monday 19:03",
691+
nowish.shiftDays(5).shiftHours(6).shiftMinutes(3),
692+
);
687693
try testTimeParsing(
688694
nowish,
689695
"2023-11-09 15:30:00",

0 commit comments

Comments
 (0)