54 lines
1.5 KiB
Go
54 lines
1.5 KiB
Go
// Copyright (c) 2026 Storm Dragon
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
package datetime
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestFormatsAndExamples(t *testing.T) {
|
|
now := time.Date(2026, 10, 25, 14, 5, 9, 0, time.FixedZone("fixture", -4*3600))
|
|
for _, testCase := range []struct {
|
|
id, want string
|
|
date bool
|
|
}{
|
|
{"24-hour", "14:05", false},
|
|
{"24-hour-seconds", "14:05:09", false},
|
|
{"24-hour-words", "14 hours and 05 minutes", false},
|
|
{"24-hour-seconds-words", "14 hours, 05 minutes and 09 seconds", false},
|
|
{"numbers-dm", "25/10", true},
|
|
{"numbers-md", "10/25", true},
|
|
{"numbers-dmy", "25/10/2026", true},
|
|
{"numbers-mdy", "10/25/2026", true},
|
|
{"numbers-ymd", "2026/10/25", true},
|
|
} {
|
|
format := Format_time
|
|
if testCase.date {
|
|
format = Format_date
|
|
}
|
|
if got, ok := format(testCase.id, now); !ok || got != testCase.want {
|
|
t.Fatal(testCase.id, got, ok)
|
|
}
|
|
}
|
|
for _, date := range []bool{false, true} {
|
|
options, format, valid := Time_options(now), Format_time, Valid_time
|
|
if date {
|
|
options, format, valid = Date_options(now), Format_date, Valid_date
|
|
}
|
|
for _, option := range options {
|
|
got, ok := format(option.Value, now)
|
|
if !ok || !valid(option.Value) || got == "" || got != option.Label {
|
|
t.Fatal(option, got, ok)
|
|
}
|
|
}
|
|
if got, ok := format("%arbitrary", now); ok || got != "" || valid("%arbitrary") {
|
|
t.Fatal("invalid preset accepted")
|
|
}
|
|
}
|
|
if got, _ := Format_time("24-hour-seconds", now.Add(10*time.Hour)); got != "00:05:09" {
|
|
t.Fatal(got)
|
|
}
|
|
}
|