forked from maddyblue/sqlfmt
/
zone_cache.go
executable file
·92 lines (79 loc) · 2.38 KB
/
zone_cache.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
// 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
import (
"fmt"
"time"
"github.com/labulakalia/sqlfmt/cockroach/pkg/util/syncutil"
"github.com/labulakalia/sqlfmt/cockroach/pkg/util/timeutil"
)
// zoneCache stores the results of resolving time.Location instances.
//
// timeutil.LoadLocation does not perform caching internally and requires
// many disk accesses to locate the named zoneinfo file.
//
// We also save time.FixedZone calls to avoid needing to regenerate
// the string representation.
type zoneCache struct {
mu struct {
syncutil.Mutex
named map[string]*zoneCacheEntry
fixed map[int]*time.Location
}
}
var zoneCacheInstance = zoneCache{}
func init() {
zoneCacheInstance.mu.named = make(map[string]*zoneCacheEntry)
zoneCacheInstance.mu.fixed = make(map[int]*time.Location)
}
type zoneCacheEntry struct {
loc *time.Location
// We don't expect the tzinfo database to change frequently, relative
// to restarts of the server. Errors are unlikely to ever resolve.
err error
}
// LoadLocation wraps timeutil.LoadLocation which does not perform
// caching internally and which requires many disk accesses to
// locate the named zoneinfo file.
func (z *zoneCache) LoadLocation(zone string) (*time.Location, error) {
z.mu.Lock()
entry, ok := z.mu.named[zone]
z.mu.Unlock()
if !ok {
loc, err := timeutil.LoadLocation(zone)
entry = &zoneCacheEntry{loc: loc, err: err}
z.mu.Lock()
z.mu.named[zone] = entry
z.mu.Unlock()
}
return entry.loc, entry.err
}
// FixedZone wraps time.FixedZone.
func (z *zoneCache) FixedZone(hours, minutes, seconds int) *time.Location {
offset := (hours*60+minutes)*60 + seconds
z.mu.Lock()
ret, ok := z.mu.fixed[offset]
z.mu.Unlock()
if !ok {
if minutes < 0 {
minutes *= -1
}
if seconds < 0 {
seconds *= -1
}
// Need a field width of 3 for the leading digit because we're
// forcing the sign to be printed and it counts as part of the field.
ret = time.FixedZone(fmt.Sprintf("%+03d%02d%02d", hours, minutes, seconds), offset)
z.mu.Lock()
z.mu.fixed[offset] = ret
z.mu.Unlock()
}
return ret
}