feat(quando-gr5): implement automatic date parsing with format detection

Implement Parse() function that automatically detects and parses common
date formats without requiring explicit layout strings. This provides an
intuitive API for parsing dates from various sources while maintaining
type safety through proper error handling.

Supported formats:
- ISO format (YYYY-MM-DD): "2026-02-09"
- ISO with slash (YYYY/MM/DD): "2026/02/09"
- EU format (DD.MM.YYYY): "09.02.2026"
- RFC2822/RFC1123: "Mon, 09 Feb 2026 00:00:00 +0000"

Key features:
- Detects and rejects ambiguous slash formats (e.g., "01/02/2026")
- Returns clear, contextual errors for invalid or ambiguous inputs
- Never panics - all errors via return values
- Zero allocations for successful parses
- Comprehensive test coverage (98%)

Performance results:
- ISO format: 105.5 ns/op (94x faster than 10µs target)
- ISO slash: 118.3 ns/op
- EU format: 117.4 ns/op
- RFC2822: 257.8 ns/op

Test coverage:
- 42 unit tests covering valid formats, error cases, edge cases
- 3 example tests demonstrating usage patterns
- Benchmarks for all format types
- Parse() function: 100% coverage

Files added:
- parse.go: Main implementation with Parse() and helper functions
- parse_test.go: Comprehensive test suite with table-driven tests

Files modified:
- example_test.go: Added ExampleParse examples
This commit is contained in:
Oliver Jakoubek 2026-02-11 18:53:16 +01:00
commit 065b767b54
4 changed files with 465 additions and 1 deletions

View file

@ -365,3 +365,48 @@ func ExampleDate_Add_chaining() {
fmt.Println(result)
// Output: 2026-02-16 10:00:00
}
// ExampleParse demonstrates basic date parsing with automatic format detection
func ExampleParse() {
date, err := quando.Parse("2026-02-09")
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println(date)
// Output: 2026-02-09 00:00:00
}
// ExampleParse_formats demonstrates parsing various supported date formats
func ExampleParse_formats() {
formats := []string{
"2026-02-09", // ISO format (YYYY-MM-DD)
"2026/02/09", // ISO with slash (YYYY/MM/DD)
"09.02.2026", // EU format (DD.MM.YYYY)
}
for _, f := range formats {
date, err := quando.Parse(f)
if err != nil {
fmt.Printf("Error parsing %s: %v\n", f, err)
continue
}
fmt.Println(date)
}
// Output:
// 2026-02-09 00:00:00
// 2026-02-09 00:00:00
// 2026-02-09 00:00:00
}
// ExampleParse_error demonstrates error handling for ambiguous formats
func ExampleParse_error() {
// Slash format without year prefix is ambiguous
// (could be US: MM/DD/YYYY or EU: DD/MM/YYYY)
_, err := quando.Parse("01/02/2026")
if errors.Is(err, quando.ErrInvalidFormat) {
fmt.Println("Ambiguous format detected")
}
// Output: Ambiguous format detected
}