-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathtime.go
More file actions
142 lines (125 loc) · 3.62 KB
/
time.go
File metadata and controls
142 lines (125 loc) · 3.62 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package rss
import (
"slices"
"strings"
"time"
)
// nextUpdate converts RSS-style update data to the
// timestamp of the next acceptable update.
func (r *Reader) nextUpdate(minsToLive int, skipHours []int, skipDays []string) time.Time {
return nextUpdate(r.now(), r.defaultUpdateInterval, minsToLive, skipHours, skipDays)
}
func nextUpdate(now time.Time, defaultInterval time.Duration, minsToLive int, skipHours []int, skipDays []string) time.Time {
if minsToLive == 0 {
return now.Add(defaultInterval)
}
skipHours = slices.Clone(skipHours)
slices.Sort(skipHours)
update := now.Add(time.Duration(minsToLive) * time.Minute)
for _, hour := range skipHours {
if hour == update.Hour() {
update = update.Add(time.Hour)
}
}
again:
for _, day := range skipDays {
if strings.Title(day) == update.Weekday().String() {
update = update.Add(time.Duration(24-update.Hour()) * time.Hour)
goto again
}
}
return update
}
func (r *Reader) parseTime(s string) time.Time {
return parseTime(s, r.timeLayouts, r.timeLayoutsWithNamedLocation)
}
func parseTime(s string, layouts, layoutsWithNamedLocation []string) time.Time {
s = strings.TrimSpace(s)
for _, layout := range layouts {
tv, err := time.Parse(layout, s)
if err == nil {
return tv.UTC()
}
}
for _, layout := range layoutsWithNamedLocation {
tv, err := time.Parse(layout, s)
if err != nil {
continue
}
loc, err := time.LoadLocation(tv.Location().String())
if err != nil {
return time.Time{}
}
tv, err = time.ParseInLocation(layout, s, loc)
if err == nil {
return tv.UTC()
}
}
return time.Time{}
}
// defaultTimeLayouts contains a list of [time.Parse]
// layouts that are used in attempts to convert
// item.Date and item.PubDate strings to time.Time
// values.
//
// The layouts are attempted sequentially until
// either [time.Parse] does not return an error or
// all layouts have been attempted.
var defaultTimeLayouts = []string{
"Mon, 2 Jan 2006 15:04:05 Z",
"Mon, 2 Jan 2006 15:04:05",
"Mon, 2 Jan 2006 15:04:05 -0700",
"Mon, 2 Jan 06 15:04:05 -0700",
"Mon, 2 Jan 06 15:04:05",
"2 Jan 2006 15:04:05 -0700",
"2 Jan 2006 15:04:05",
"2 Jan 06 15:04:05 -0700",
"2006-01-02 15:04:05 -0700",
"2006-01-02 15:04:05",
time.ANSIC,
time.UnixDate,
time.RubyDate,
time.RFC822Z,
time.RFC1123Z,
time.RFC3339,
time.RFC3339Nano,
// Not some common time zones. While they are odd,
// they can occur but we should only check
// them last (to slightly improve runtime).
// We also always use the offset, because the
// texual representation can be ambiguous.
// For example, PST can have different rules
// in different locals:
// https://news.ycombinator.com/item?id=10199812
"2 Jan 2006 15:04:05 -0700 MST",
"2 Jan 2006 15:04:05 MST -0700",
"Mon, 2 Jan 2006 15:04:05 MST -0700",
"Mon, 2 Jan 2006 15:04:05 -0700 MST",
"2 Jan 06 15:04:05 -0700 MST",
"2 Jan 06 15:04:05 MST -0700",
"Jan 2, 2006 15:04 PM -0700 MST",
"Jan 2, 2006 15:04 PM MST -0700",
"Jan 2, 06 15:04 PM MST -0700",
"Jan 2, 06 15:04 PM -0700 MST",
}
// defaultTimeLayoutsWithNamedLocation are time
// layouts that do not contain the location as
// a literal constant. For example, instead of
// -0700, they use MST.
//
// Go does not load the timezone by default,
// which means [parseTime] calls
// `time.LoadLocation(t.Location().String())`
// and then applies the offset returned by
// LoadLocation to the result.
var defaultTimeLayoutsWithNamedLocation = []string{
"Mon, 2 Jan 2006 15:04:05 MST",
"Mon, 2 Jan 06 15:04:05 MST",
"2 Jan 2006 15:04:05 MST",
"2 Jan 06 15:04:05 MST",
"Jan 2, 2006 15:04 PM MST",
"Jan 2, 06 15:04 PM MST",
time.RFC1123,
time.RFC850,
time.RFC822,
}