-
Notifications
You must be signed in to change notification settings - Fork 18
/
sitemap_types.go
73 lines (59 loc) · 1.67 KB
/
sitemap_types.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package sitemap
import "time"
type sitemapEntry struct {
Location string `xml:"loc"`
LastModified string `xml:"lastmod,omitempy"`
ParsedLastModified *time.Time
ChangeFrequency Frequency `xml:"changefreq,omitempty"`
Priority float32 `xml:"priority,omitempty"`
}
func newSitemapEntry() *sitemapEntry {
return &sitemapEntry{ChangeFrequency: Always, Priority: 0.5}
}
func (e *sitemapEntry) GetLocation() string {
return e.Location
}
func (e *sitemapEntry) GetLastModified() *time.Time {
if e.ParsedLastModified == nil && e.LastModified != "" {
e.ParsedLastModified = parseDateTime(e.LastModified)
}
return e.ParsedLastModified
}
func (e *sitemapEntry) GetChangeFrequency() Frequency {
return e.ChangeFrequency
}
func (e *sitemapEntry) GetPriority() float32 {
return e.Priority
}
type sitemapIndexEntry struct {
Location string `xml:"loc"`
LastModified string `xml:"lastmod,omitempty"`
ParsedLastModified *time.Time
}
func newSitemapIndexEntry() *sitemapIndexEntry {
return &sitemapIndexEntry{}
}
func (e *sitemapIndexEntry) GetLocation() string {
return e.Location
}
func (e *sitemapIndexEntry) GetLastModified() *time.Time {
if e.ParsedLastModified == nil && e.LastModified != "" {
e.ParsedLastModified = parseDateTime(e.LastModified)
}
return e.ParsedLastModified
}
func parseDateTime(value string) *time.Time {
if value == "" {
return nil
}
t, err := time.Parse(time.RFC3339, value)
if err != nil {
// second chance
// try parse as short format
t, err = time.Parse("2006-01-02", value)
if err != nil {
return nil
}
}
return &t
}