-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathdate_node.go
189 lines (147 loc) · 3.72 KB
/
date_node.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
package gedcom
import (
"fmt"
)
// DateNode represents a DATE node.
//
// See the full specification for dates in the documentation for Date.
type DateNode struct {
*SimpleNode
// Dates are expensive to parse so we should not attempt to parse the value
// until it is needed. Also, if we have already parsed the value once it
// should not be parsed again.
alreadyParsed bool
parsedDateRange DateRange
}
// NewDateNode creates a new DATE node.
func NewDateNode(value string, children ...Node) *DateNode {
return &DateNode{
newSimpleNode(TagDate, value, "", children...),
false, DateRange{},
}
}
// If the node is nil both results will be zero dates.
func (node *DateNode) DateRange() (dateRange DateRange) {
if node == nil {
return NewZeroDateRange()
}
// Parsing dates is very expensive. Cache them.
if node.alreadyParsed {
return node.parsedDateRange
}
defer func(node *DateNode) {
node.parsedDateRange = dateRange
node.alreadyParsed = true
}(node)
return NewDateRangeWithString(node.Value())
}
// String returns the date range as defined in the specification of DateNode.
//
// There are too many combinations to document here, but the two main formats
// you will receive will look like:
//
// Bet. Feb 1956 and Mar 1956
// Abt. 13 Nov 1983
//
func (node *DateNode) String() string {
startDate, endDate := node.StartAndEndDates()
if startDate.Is(endDate) {
return startDate.String()
}
return fmt.Sprintf("Bet. %s and %s", startDate, endDate)
}
// Years fulfils the Yearer interface and is a convenience for:
//
// node.DateRange().Years()
//
func (node *DateNode) Years() float64 {
return node.DateRange().Years()
}
func (node *DateNode) Similarity(node2 *DateNode, maxYears float64) float64 {
if node == nil || node2 == nil {
return 0.5
}
return node.DateRange().Similarity(node2.DateRange(), maxYears)
}
func (node *DateNode) Equals(node2 Node) bool {
leftIsNil := IsNil(node)
rightIsNil := IsNil(node2)
if leftIsNil || rightIsNil {
return false
}
if date2, ok := node2.(*DateNode); ok {
return node.DateRange().Equals(date2.DateRange())
}
return false
}
func (node *DateNode) IsValid() bool {
if node == nil {
return false
}
return node.DateRange().IsValid()
}
func (node *DateNode) StartDate() Date {
if node == nil {
return Date{}
}
dateRange := node.DateRange()
return dateRange.StartDate()
}
func (node *DateNode) EndDate() Date {
if node == nil {
return Date{}
}
dateRange := node.DateRange()
return dateRange.EndDate()
}
func (node *DateNode) StartAndEndDates() (Date, Date) {
if node == nil {
return NewZeroDateRange().StartAndEndDates()
}
return node.DateRange().StartAndEndDates()
}
func (node *DateNode) IsExact() bool {
if node == nil {
return false
}
return node.DateRange().IsExact()
}
func (node *DateNode) IsPhrase() bool {
if node == nil {
return false
}
return node.DateRange().IsPhrase()
}
func (node *DateNode) Sub(node2 *DateNode) (min Duration, max Duration, errs error) {
nodeStart, nodeEnd := node.DateRange().StartAndEndDates()
node2Start, node2End := node2.DateRange().StartAndEndDates()
errs = NewErrors(
nodeStart.ParseError,
nodeEnd.ParseError,
node2Start.ParseError,
node2End.ParseError,
).Err()
min = node.StartDate().Sub(node2.StartDate())
max = node.EndDate().Sub(node2.EndDate())
return
}
func (node *DateNode) Warnings() Warnings {
if !node.IsValid() {
return Warnings{
NewUnparsableDateWarning(node),
}
}
return nil
}
func (node *DateNode) IsBefore(node2 *DateNode) bool {
if node == nil {
return false
}
return node.DateRange().IsBefore(node2.DateRange())
}
func (node *DateNode) IsAfter(node2 *DateNode) bool {
if node == nil {
return false
}
return node.DateRange().IsAfter(node2.DateRange())
}