-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathletter_prefixed_calendar_year.go
69 lines (49 loc) · 1.66 KB
/
letter_prefixed_calendar_year.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
package level1
import (
"github.com/sfomuseum/go-edtf"
"github.com/sfomuseum/go-edtf/common"
"github.com/sfomuseum/go-edtf/re"
"strings"
)
/*
'Y' may be used at the beginning of the date string to signify that the date is a year, when (and only when) the year exceeds four digits, i.e. for years later than 9999 or earlier than -9999.
Example 1 'Y170000002' is the year 170000002
Example 2 'Y-170000002' is the year -170000002
*/
func IsLetterPrefixedCalendarYear(edtf_str string) bool {
return re.LetterPrefixedCalendarYear.MatchString(edtf_str)
}
func ParseLetterPrefixedCalendarYear(edtf_str string) (*edtf.EDTFDate, error) {
m := re.LetterPrefixedCalendarYear.FindStringSubmatch(edtf_str)
if len(m) != 2 {
return nil, edtf.Invalid(LETTER_PREFIXED_CALENDAR_YEAR, edtf_str)
}
// Years must be in the range 0000..9999.
// https://golang.org/pkg/time/#Parse
// sigh....
// fmt.Printf("DEBUG %v\n", start.Add(time.Hour * 8760 * 1000))
// ./prog.go:21:54: constant 31536000000000000000 overflows time.Duration
// common.DateSpanFromEDTF needs to be updated to simply assign a valid
// *edtf.YMD element and leave *time.Time blank when creating *edtf.Date
// instances (20210105/thisisaaronland)
yyyy := m[1]
max_length := 4
if strings.HasPrefix(yyyy, "-") {
max_length = 5
}
if len(yyyy) > max_length {
return nil, edtf.Unsupported(LETTER_PREFIXED_CALENDAR_YEAR, edtf_str)
}
sp, err := common.DateSpanFromEDTF(yyyy)
if err != nil {
return nil, err
}
d := &edtf.EDTFDate{
Start: sp.Start,
End: sp.End,
EDTF: edtf_str,
Level: LEVEL,
Feature: LETTER_PREFIXED_CALENDAR_YEAR,
}
return d, nil
}