Skip to content

Commit bece749

Browse files
committed
support empty string
1 parent 779b66b commit bece749

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

parsetime.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,19 @@ func NewParseTime(location ...interface{}) (ParseTime, error) {
5353
case *time.Location:
5454
loc = val
5555
case string:
56-
loc, err = time.LoadLocation(val)
57-
if err != nil {
58-
var offset int
59-
offset, err = timezone.GetOffset(val)
56+
if val == "" {
57+
zone, offset := time.Now().In(time.Local).Zone()
58+
loc = time.FixedZone(zone, offset)
59+
} else {
60+
loc, err = time.LoadLocation(val)
6061
if err != nil {
61-
return ParseTime{}, errInvalidTimezone
62+
var offset int
63+
offset, err = timezone.GetOffset(val)
64+
if err != nil {
65+
return ParseTime{}, errInvalidTimezone
66+
}
67+
loc = time.FixedZone(val, offset)
6268
}
63-
loc = time.FixedZone(val, offset)
6469
}
6570
default:
6671
return ParseTime{}, fmt.Errorf("Invalid type: %T", val)

parsetime_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,18 @@ func TestNewParseTime(test *testing.T) {
935935
assert.Equal(loc.String(), loc2.String(), "Incorrect location")
936936
}
937937

938+
func TestNewParseTimeEmptyString(test *testing.T) {
939+
assert := assert.New(test)
940+
941+
p, _ := NewParseTime("")
942+
943+
loc := p.GetLocation()
944+
zone, offset := time.Now().In(time.Local).Zone()
945+
loc2 := time.FixedZone(zone, offset)
946+
947+
assert.Equal(loc.String(), loc2.String(), "Incorrect location")
948+
}
949+
938950
func TestNewParseTimeLocation(test *testing.T) {
939951
assert := assert.New(test)
940952

0 commit comments

Comments
 (0)