Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions calendars.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package sitemaps

import (
"fmt"
"time"
)

Expand All @@ -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(),
Expand Down
15 changes: 14 additions & 1 deletion calendars_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
58 changes: 58 additions & 0 deletions encode_test.go
Original file line number Diff line number Diff line change
@@ -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(`
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>/foo/bar</loc>
<lastmod>2025-10-31</lastmod>
<changefreq>daily</changefreq>
<priority>0.6</priority>
</url>
<url>
<loc>/other</loc>
<lastmod>2025-02-21</lastmod>
<changefreq>weekly</changefreq>
<priority>0.1</priority>
</url>
</urlset>`)

result := strings.TrimSpace(buf.String())
must.Eq(t, exp, result)
}
2 changes: 1 addition & 1 deletion layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{{- range .}}
<url>
<loc>{{.Location}}</loc>
<lastmod>{{.Modified.DayISO}}</lastmod>
<lastmod>{{.Modified.ISO}}</lastmod>
<changefreq>{{.Frequency}}</changefreq>
<priority>{{.Priority}}</priority>
</url>
Expand Down