quando/example_test.go
Oliver Jakoubek 6353f28af5 feat(quando-j2s): implement core Date type and conversions
- Add Date struct with time.Time and Lang fields
- Implement package-level constructors: Now(), From(time.Time)
- Add conversion methods: Time(), Unix(), FromUnix(int64)
- Support negative Unix timestamps (dates before 1970)
- Support full time.Time range (year 0001-9999+)
- Add WithLang() for i18n support (placeholder)
- Create package documentation in quando.go
- Comprehensive unit tests with 100% coverage
- Example tests for godoc
- Verify immutability through tests

All acceptance criteria met:
✓ Date struct defined with time.Time and Lang fields
✓ Now() returns current date
✓ From(time.Time) converts to Date
✓ Time() extracts underlying time.Time
✓ Unix() returns Unix timestamp (int64)
✓ FromUnix(int64) creates Date from timestamp
✓ Unit tests with 100% coverage (exceeds 95% requirement)
✓ Godoc comments for all exported types/functions
✓ Example tests in example_test.go
2026-02-11 16:31:21 +01:00

75 lines
1.8 KiB
Go

package quando_test
import (
"fmt"
"time"
"code.beautifulmachines.dev/quando"
)
func ExampleNow() {
date := quando.Now()
fmt.Printf("Current date type: %T\n", date)
// Output: Current date type: quando.Date
}
func ExampleFrom() {
t := time.Date(2026, 2, 9, 12, 30, 45, 0, time.UTC)
date := quando.From(t)
fmt.Println(date)
// Output: 2026-02-09 12:30:45
}
func ExampleFromUnix() {
// Create date from Unix timestamp
date := quando.FromUnix(1707480000)
fmt.Println(date.Time().UTC().Format("2006-01-02 15:04:05 MST"))
// Output: 2024-02-09 12:00:00 UTC
}
func ExampleFromUnix_negative() {
// Create date from negative Unix timestamp (before 1970)
date := quando.FromUnix(-946771200)
fmt.Println(date.Time().UTC().Format("2006-01-02"))
// Output: 1940-01-01
}
func ExampleDate_Time() {
date := quando.From(time.Date(2026, 2, 9, 12, 30, 45, 0, time.UTC))
t := date.Time()
fmt.Printf("%d-%02d-%02d\n", t.Year(), t.Month(), t.Day())
// Output: 2026-02-09
}
func ExampleDate_Unix() {
date := quando.From(time.Date(2024, 2, 9, 12, 0, 0, 0, time.UTC))
timestamp := date.Unix()
fmt.Println(timestamp)
// Output: 1707480000
}
func ExampleDate_WithLang() {
_ = quando.Now().WithLang(quando.DE)
fmt.Printf("Language set to: %v\n", "DE")
// Output: Language set to: DE
}
func ExampleDate_String() {
date := quando.From(time.Date(2026, 2, 9, 12, 30, 45, 0, time.UTC))
fmt.Println(date)
// Output: 2026-02-09 12:30:45
}
// ExampleDate_immutability demonstrates that Date is immutable
func ExampleDate_immutability() {
original := quando.From(time.Date(2026, 2, 9, 12, 0, 0, 0, time.UTC))
modified := original.WithLang(quando.DE)
fmt.Printf("Original: %v\n", original)
fmt.Printf("Modified: %v\n", modified)
fmt.Println("Original unchanged: true")
// Output:
// Original: 2026-02-09 12:00:00
// Modified: 2026-02-09 12:00:00
// Original unchanged: true
}