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
|
package strftime
import (
"errors"
"fmt"
"sync"
)
// because there is no such thing was a sync.RWLocker
type rwLocker interface {
RLock()
RUnlock()
sync.Locker
}
// SpecificationSet is a container for patterns that Strftime uses.
// If you want a custom strftime, you can copy the default
// SpecificationSet and tweak it
type SpecificationSet interface {
Lookup(byte) (Appender, error)
Delete(byte) error
Set(byte, Appender) error
}
type specificationSet struct {
mutable bool
lock rwLocker
store map[byte]Appender
}
// The default specification set does not need any locking as it is never
// accessed from the outside, and is never mutated.
var defaultSpecificationSet SpecificationSet
func init() {
defaultSpecificationSet = newImmutableSpecificationSet()
}
func newImmutableSpecificationSet() SpecificationSet {
// Create a mutable one so that populateDefaultSpecifications work through
// its magic, then copy the associated map
// (NOTE: this is done this way because there used to be
// two struct types for specification set, united under an interface.
// it can now be removed, but we would need to change the entire
// populateDefaultSpecifications method, and I'm currently too lazy
// PRs welcome)
tmp := NewSpecificationSet()
ss := &specificationSet{
mutable: false,
lock: nil, // never used, so intentionally not initialized
store: tmp.(*specificationSet).store,
}
return ss
}
// NewSpecificationSet creates a specification set with the default specifications.
func NewSpecificationSet() SpecificationSet {
ds := &specificationSet{
mutable: true,
lock: &sync.RWMutex{},
store: make(map[byte]Appender),
}
populateDefaultSpecifications(ds)
return ds
}
var defaultSpecifications = map[byte]Appender{
'A': fullWeekDayName,
'a': abbrvWeekDayName,
'B': fullMonthName,
'b': abbrvMonthName,
'C': centuryDecimal,
'c': timeAndDate,
'D': mdy,
'd': dayOfMonthZeroPad,
'e': dayOfMonthSpacePad,
'G': weekyear,
'g': weekyearNoCentury,
'F': ymd,
'H': twentyFourHourClockZeroPad,
'h': abbrvMonthName,
'I': twelveHourClockZeroPad,
'j': dayOfYear,
'k': twentyFourHourClockSpacePad,
'l': twelveHourClockSpacePad,
'M': minutesZeroPad,
'm': monthNumberZeroPad,
'n': newline,
'p': ampm,
'R': hm,
'r': imsp,
'S': secondsNumberZeroPad,
'T': hms,
't': tab,
'U': weekNumberSundayOrigin,
'u': weekdayMondayOrigin,
'V': weekNumberMondayOriginOneOrigin,
'v': eby,
'W': weekNumberMondayOrigin,
'w': weekdaySundayOrigin,
'X': natReprTime,
'x': natReprDate,
'Y': year,
'y': yearNoCentury,
'Z': timezone,
'z': timezoneOffset,
'%': percent,
}
func populateDefaultSpecifications(ds SpecificationSet) {
for c, handler := range defaultSpecifications {
if err := ds.Set(c, handler); err != nil {
panic(fmt.Sprintf("failed to set default specification for %c: %s", c, err))
}
}
}
func (ds *specificationSet) Lookup(b byte) (Appender, error) {
if ds.mutable {
ds.lock.RLock()
defer ds.lock.RLock()
}
v, ok := ds.store[b]
if !ok {
return nil, fmt.Errorf(`lookup failed: '%%%c' was not found in specification set`, b)
}
return v, nil
}
func (ds *specificationSet) Delete(b byte) error {
if !ds.mutable {
return errors.New(`delete failed: this specification set is marked immutable`)
}
ds.lock.Lock()
defer ds.lock.Unlock()
delete(ds.store, b)
return nil
}
func (ds *specificationSet) Set(b byte, a Appender) error {
if !ds.mutable {
return errors.New(`set failed: this specification set is marked immutable`)
}
ds.lock.Lock()
defer ds.lock.Unlock()
ds.store[b] = a
return nil
}
|