-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsub_year_grouping.go
204 lines (171 loc) · 4.47 KB
/
sub_year_grouping.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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package level2
import (
"fmt"
"github.com/sfomuseum/go-edtf"
"github.com/sfomuseum/go-edtf/common"
"github.com/sfomuseum/go-edtf/re"
"strconv"
)
/*
Level 2 extends the season feature of Level 1 to include the following sub-year groupings.
21 Spring (independent of location)
22 Summer (independent of location)
23 Autumn (independent of location)
24 Winter (independent of location)
25 Spring - Northern Hemisphere
26 Summer - Northern Hemisphere
27 Autumn - Northern Hemisphere
28 Winter - Northern Hemisphere
29 Spring - Southern Hemisphere
30 Summer - Southern Hemisphere
31 Autumn - Southern Hemisphere
32 Winter - Southern Hemisphere
33 Quarter 1 (3 months in duration)
34 Quarter 2 (3 months in duration)
35 Quarter 3 (3 months in duration)
36 Quarter 4 (3 months in duration)
37 Quadrimester 1 (4 months in duration)
38 Quadrimester 2 (4 months in duration)
39 Quadrimester 3 (4 months in duration)
40 Semestral 1 (6 months in duration)
41 Semestral 2 (6 months in duration)
Example ‘2001-34’
second quarter of 2001
*/
func IsSubYearGrouping(edtf_str string) bool {
return re.SubYearGrouping.MatchString(edtf_str)
}
func ParseSubYearGroupings(edtf_str string) (*edtf.EDTFDate, error) {
/*
SUB 3 2001-34,2001,34
*/
m := re.SubYearGrouping.FindStringSubmatch(edtf_str)
if len(m) != 3 {
return nil, edtf.Invalid(SUB_YEAR_GROUPINGS, edtf_str)
}
year := m[1]
grouping := m[2]
start_yyyy := year
start_mm := ""
start_dd := ""
end_yyyy := year
end_mm := ""
end_dd := ""
switch grouping {
case "21", "25": // Spring (independent of location, Northern Hemisphere)
start_mm = "03"
start_dd = "01"
end_mm = "05"
end_dd = "31"
case "22", "26": // Summer (independent of location, Northern Hemisphere)
start_mm = "06"
start_dd = "01"
end_mm = "08"
end_dd = "31"
case "23", "27": // Autumn (independent of location, Northern Hemisphere)
start_mm = "09"
start_dd = "01"
end_mm = "11"
end_dd = "30"
case "24", "28": // Winter (independent of location, Northern Hemisphere)
start_mm = "12"
start_dd = "01"
end_mm = "02"
end_dd = "" // leave blank to make the code look up daysforyear(year)
y, err := strconv.Atoi(end_yyyy)
if err != nil {
return nil, err
}
end_yyyy = strconv.Itoa(y + 1)
case "29": // Spring - Southern Hemisphere
start_mm = "09"
start_dd = "01"
end_mm = "11"
end_dd = "30"
case "30": // Summer - Southern Hemisphere
start_mm = "12"
start_dd = "01"
end_mm = "02"
end_dd = "" // leave blank to make the code look up daysforyear(year)
y, err := strconv.Atoi(end_yyyy)
if err != nil {
return nil, err
}
end_yyyy = strconv.Itoa(y + 1)
case "31": // Autumn - Southern Hemisphere
start_mm = "03"
start_dd = "01"
end_mm = "05"
end_dd = "31"
case "32": // Winter - Southern Hemisphere
start_mm = "06"
start_dd = "01"
end_mm = "08"
end_dd = "31"
case "33": // Quarter 1 (3 months in duration)
start_mm = "01"
start_dd = "01"
end_mm = "03"
end_dd = "31"
case "34": // Quarter 2 (3 months in duration)
start_mm = "04"
start_dd = "01"
end_mm = "06"
end_dd = "30"
case "35": // Quarter 3 (3 months in duration)
start_mm = "07"
start_dd = "01"
end_mm = "09"
end_dd = "30"
case "36": // Quarter 4 (3 months in duration)
start_mm = "10"
start_dd = "01"
end_mm = "12"
end_dd = "31"
case "37": // Quadrimester 1 (4 months in duration)
start_mm = "01"
start_dd = "01"
end_mm = "04"
end_dd = "30"
case "38": // Quadrimester 2 (4 months in duration)
start_mm = "05"
start_dd = "01"
end_mm = "08"
end_dd = "31"
case "39": // Quadrimester 3 (4 months in duration)
start_mm = "09"
start_dd = "01"
end_mm = "12"
end_dd = "31"
case "40": // Semestral 1 (6 months in duration)
start_mm = "01"
start_dd = "01"
end_mm = "06"
end_dd = "30"
case "41": // Semestral 2 (6 months in duration)
start_mm = "07"
start_dd = "01"
end_mm = "12"
end_dd = "31"
default:
return nil, edtf.Invalid(SUB_YEAR_GROUPINGS, edtf_str)
}
start := fmt.Sprintf("%s-%s-%s", start_yyyy, start_mm, start_dd)
end := fmt.Sprintf("%s-%s", end_yyyy, end_mm)
if end_dd != "" {
end = fmt.Sprintf("%s-%s", end, end_dd)
}
_str := fmt.Sprintf("%s/%s", start, end)
sp, err := common.DateSpanFromEDTF(_str)
if err != nil {
return nil, err
}
d := &edtf.EDTFDate{
Start: sp.Start,
End: sp.End,
EDTF: edtf_str,
Level: LEVEL,
Feature: SUB_YEAR_GROUPINGS,
}
return d, nil
}