/
fields.go
118 lines (100 loc) · 2.72 KB
/
fields.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
// Copyright 2018 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package pgdate
//go:generate stringer -type=field
// A field is some piece of information that we can newFieldExtract out of a
// date or time string.
type field uint
// The field values here are used in index into the fieldExtract.data
// array, otherwise we'd use 1 << iota.
const (
fieldYear field = iota
fieldMonth
fieldDay
fieldEra
fieldHour
fieldMinute
fieldSecond
fieldNanos
fieldMeridian
fieldTZHour
fieldTZMinute
fieldTZSecond
fieldMinimum = fieldYear
fieldMaximum = fieldTZSecond
)
const (
fieldValueAM = -1
fieldValuePM = 1
fieldValueBCE = -1
fieldValueCE = 1
)
// AsSet returns a singleton set containing the field.
func (f field) AsSet() fieldSet {
return 1 << f
}
// Pretty wraps the generated String() function to return only the
// name: "Year" vs "fieldYear".
func (f field) Pretty() string {
return f.String()[5:]
}
// A fieldSet is an immutable aggregate of fields.
// The zero value is an empty set.
type fieldSet int
// newFieldSet constructs a fieldSet from zero or more fields.
func newFieldSet(fields ...field) fieldSet {
var ret fieldSet
for _, f := range fields {
ret |= f.AsSet()
}
return ret
}
// Add returns a fieldSet containing the field.
func (s fieldSet) Add(field field) fieldSet {
return s.AddAll(field.AsSet())
}
// AddAll returns a fieldSet combining the other fieldSet.
func (s fieldSet) AddAll(other fieldSet) fieldSet {
return s | other
}
// Clear removes the field from the set. It is not an error to
// clear a field which is not set.
func (s fieldSet) Clear(field field) fieldSet {
return s.ClearAll(field.AsSet())
}
// ClearAll removes all fields from other in the set. It is not an
// error to clear fields which are not set.
func (s fieldSet) ClearAll(other fieldSet) fieldSet {
return s & ^other
}
// Has returns true if the given field is present in the set.
func (s fieldSet) Has(field field) bool {
return s&field.AsSet() != 0
}
// HasAll returns true if the field set contains all of the
// other fields.
func (s fieldSet) HasAll(other fieldSet) bool {
return s&other == other
}
// HasAny returns true if the field set contains any of the
// other fields.
func (s fieldSet) HasAny(other fieldSet) bool {
return s&other != 0
}
func (s *fieldSet) String() string {
ret := "[ "
for f := fieldMinimum; f <= fieldMaximum; f++ {
if s.Has(f) {
ret += f.Pretty() + " "
}
}
ret += "]"
return ret
}