Skip to content

Commit 7c96261

Browse files
authored
Merge pull request #5 from jf-tech/loccache
move loadLoc to caches
2 parents a6c6e06 + 39f8419 commit 7c96261

File tree

5 files changed

+54
-47
lines changed

5 files changed

+54
-47
lines changed

caches/timeLocCache.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package caches
2+
3+
import (
4+
"time"
5+
)
6+
7+
// TimeLocationCache is the default loading cache used for caching *time.Location
8+
// object If the default size is too big/small and/or a cache limit isn't desired
9+
// at all, caller can simply replace the cache during global initialization. But
10+
// be aware it's global so any packages uses this package inside your process will
11+
// be affected.
12+
var TimeLocationCache = NewLoadingCache()
13+
14+
func GetTimeLocation(tz string) (*time.Location, error) {
15+
loc, err := TimeLocationCache.Get(tz, func(key interface{}) (interface{}, error) {
16+
return time.LoadLocation(key.(string))
17+
})
18+
if err != nil {
19+
return nil, err
20+
}
21+
return loc.(*time.Location), nil
22+
}

caches/timeLocCache_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package caches
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestGetTimeLocation(t *testing.T) {
10+
TimeLocationCache = NewLoadingCache()
11+
assert.Equal(t, 0, len(TimeLocationCache.DumpForTest()))
12+
// failure case
13+
expr, err := GetTimeLocation("unknown")
14+
assert.Error(t, err)
15+
assert.Equal(t, "unknown time zone unknown", err.Error())
16+
assert.Nil(t, expr)
17+
assert.Equal(t, 0, len(TimeLocationCache.DumpForTest()))
18+
// success case
19+
expr, err = GetTimeLocation("America/New_York")
20+
assert.NoError(t, err)
21+
assert.NotNil(t, expr)
22+
assert.Equal(t, 1, len(TimeLocationCache.DumpForTest()))
23+
// repeat success case shouldn't case any cache growth
24+
expr, err = GetTimeLocation("America/New_York")
25+
assert.NoError(t, err)
26+
assert.NotNil(t, expr)
27+
assert.Equal(t, 1, len(TimeLocationCache.DumpForTest()))
28+
}

times/parse.go

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,6 @@ import (
88
"github.com/jf-tech/go-corelib/caches"
99
)
1010

11-
var locCache = caches.NewLoadingCache()
12-
13-
func loadLoc(tz string) (*time.Location, error) {
14-
loc, err := locCache.Get(tz, func(key interface{}) (interface{}, error) {
15-
return time.LoadLocation(key.(string))
16-
})
17-
if err != nil {
18-
return nil, err
19-
}
20-
return loc.(*time.Location), nil
21-
}
22-
2311
// SmartParse parses a date time string and returns a time.Time and a tz flag indicates whether the
2412
// date time string contains tz info.
2513
// The date time string can be either date only, or date + time, or date + time + tz.
@@ -57,7 +45,7 @@ func SmartParse(s string) (t time.Time, tz bool, err error) {
5745

5846
if tzStr, tzFound := probeTimezoneSuffix(s); tzFound {
5947
// no err checking because tz from probeTimezoneSuffix guaranteed to be valid.
60-
loc, _ = loadLoc(tzStr)
48+
loc, _ = caches.GetTimeLocation(tzStr)
6149
// -1 for the '-' that is not included in tz.
6250
s = strings.TrimSpace(s[:len(s)-len(tzStr)-1])
6351
}

times/parse_test.go

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,6 @@ import (
77
"github.com/stretchr/testify/assert"
88
)
99

10-
func TestLoadLoc(t *testing.T) {
11-
for _, test := range []struct {
12-
tz string
13-
expectedErr string
14-
}{
15-
{
16-
tz: "America/Los_Angeles",
17-
expectedErr: "",
18-
},
19-
{
20-
tz: "America/Indiana/Indianapolis",
21-
expectedErr: "",
22-
},
23-
{
24-
tz: "Unknown",
25-
expectedErr: "unknown time zone Unknown",
26-
},
27-
} {
28-
t.Run(test.tz, func(t *testing.T) {
29-
loc, err := loadLoc(test.tz)
30-
if test.expectedErr != "" {
31-
assert.Error(t, err)
32-
assert.Equal(t, test.expectedErr, err.Error())
33-
assert.Nil(t, loc)
34-
} else {
35-
assert.NoError(t, err)
36-
assert.Equal(t, test.tz, loc.String())
37-
}
38-
})
39-
}
40-
}
41-
4210
func TestSmartParse_Success(t *testing.T) {
4311
for _, test := range []struct {
4412
name string

times/util.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package times
33
import (
44
"time"
55

6+
"github.com/jf-tech/go-corelib/caches"
67
"github.com/jf-tech/go-corelib/strs"
78
)
89

@@ -13,7 +14,7 @@ func OverwriteTZ(t time.Time, tz string) (time.Time, error) {
1314
if !strs.IsStrNonBlank(tz) {
1415
return t, nil
1516
}
16-
loc, err := loadLoc(tz)
17+
loc, err := caches.GetTimeLocation(tz)
1718
if err != nil {
1819
return time.Time{}, err
1920
}
@@ -28,7 +29,7 @@ func ConvertTZ(t time.Time, tz string) (time.Time, error) {
2829
if !strs.IsStrNonBlank(tz) {
2930
return t, nil
3031
}
31-
loc, err := loadLoc(tz)
32+
loc, err := caches.GetTimeLocation(tz)
3233
if err != nil {
3334
return time.Time{}, err
3435
}

0 commit comments

Comments
 (0)