diff --git a/calendars.go b/calendars.go index 9600be2..70890eb 100644 --- a/calendars.go +++ b/calendars.go @@ -4,6 +4,7 @@ package sitemaps import ( + "fmt" "time" ) @@ -13,6 +14,10 @@ type CalendarDay struct { Day int } +func (cd CalendarDay) ISO() string { + return fmt.Sprintf("%d-%02d-%02d", cd.Year, cd.Month, cd.Day) +} + func DayFrom(t time.Time) CalendarDay { return CalendarDay{ Year: t.Year(), diff --git a/calendars_test.go b/calendars_test.go index 59ff2e0..75ef772 100644 --- a/calendars_test.go +++ b/calendars_test.go @@ -10,7 +10,7 @@ import ( "github.com/shoenig/test/must" ) -func Test_DayFrom(t *testing.T) { +func TestCalendarDay_DayFrom(t *testing.T) { t.Parallel() now := time.Date(2025, 11, 1, 6, 15, 0, 0, time.UTC) @@ -19,3 +19,16 @@ func Test_DayFrom(t *testing.T) { must.Eq(t, 11, cd.Month) must.Eq(t, 1, cd.Day) } + +func TestCalendarDay_ISO(t *testing.T) { + t.Parallel() + + cd := CalendarDay{ + Year: 2025, + Month: 11, + Day: 1, + } + + iso := cd.ISO() + must.Eq(t, "2025-11-01", iso) +} diff --git a/encode_test.go b/encode_test.go new file mode 100644 index 0000000..5663cbd --- /dev/null +++ b/encode_test.go @@ -0,0 +1,58 @@ +// Copyright (c) CattleCloud LLC +// SPDX-License-Identifier: BSD-3-Clause + +package sitemaps + +import ( + "bytes" + "strings" + "testing" + "time" + + "github.com/shoenig/test/must" +) + +func TestSite_Write(t *testing.T) { + t.Parallel() + + unix := time.Date(2025, 10, 31, 9, 15, 0, 0, time.UTC).Unix() + unix2 := time.Date(2025, 2, 21, 16, 30, 0, 0, time.UTC).Unix() + + site := make(Site, 0, 2) + site = append(site, &URL{ + Location: "/foo/bar", + Modified: EncodeUnix(unix), + Frequency: Daily, + Priority: Bump, + }) + site = append(site, &URL{ + Location: "/other", + Modified: EncodeUnix(unix2), + Frequency: Weekly, + Priority: None, + }) + + buf := new(bytes.Buffer) + err := site.Write(buf) + must.NoError(t, err) + + exp := strings.TrimSpace(` + + + + /foo/bar + 2025-10-31 + daily + 0.6 + + + /other + 2025-02-21 + weekly + 0.1 + +`) + + result := strings.TrimSpace(buf.String()) + must.Eq(t, exp, result) +} diff --git a/layout.xml b/layout.xml index 767f27d..4830c66 100644 --- a/layout.xml +++ b/layout.xml @@ -3,7 +3,7 @@ {{- range .}} {{.Location}} - {{.Modified.DayISO}} + {{.Modified.ISO}} {{.Frequency}} {{.Priority}}