-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonth.go
More file actions
30 lines (26 loc) · 850 Bytes
/
Copy pathmonth.go
File metadata and controls
30 lines (26 loc) · 850 Bytes
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
package command
import "bytes"
// months maps the GNU -M three-letter month abbreviations to calendar order.
// An unknown month sorts as 0, before January, matching GNU sort.
var months = map[string]int{
"JAN": 1, "FEB": 2, "MAR": 3, "APR": 4, "MAY": 5, "JUN": 6,
"JUL": 7, "AUG": 8, "SEP": 9, "OCT": 10, "NOV": 11, "DEC": 12,
}
// monthLess compares keys by calendar month (-M), e.g. Jan < Feb. Keys that are
// not month names share rank 0 and fall back to lexical order.
func monthLess(a, b []byte) bool {
ma := monthRank(a)
mb := monthRank(b)
if ma != mb {
return ma < mb
}
return lexicalLess(a, b)
}
// monthRank returns the calendar rank of a month abbreviation, or 0 if unknown.
func monthRank(key []byte) int {
abbr := bytes.ToUpper(bytes.TrimSpace(key))
if len(abbr) > 3 {
abbr = abbr[:3]
}
return months[string(abbr)]
}