Skip to content

Commit 42a0dfa

Browse files
committed
refactor(timezone): Optimize time zone parsing logic to support variable parameters
1 parent f92ee8d commit 42a0dfa

File tree

5 files changed

+87
-128
lines changed

5 files changed

+87
-128
lines changed

creator.go

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func CreateFromStdTime(stdTime StdTime, timezone ...string) *Carbon {
1313
loc *Location
1414
err error
1515
)
16-
if loc, err = parseTimezone(timezone[0]); err != nil {
16+
if loc, err = parseTimezone(timezone...); err != nil {
1717
return &Carbon{Error: err}
1818
}
1919
return NewCarbon(stdTime.In(loc))
@@ -22,16 +22,10 @@ func CreateFromStdTime(stdTime StdTime, timezone ...string) *Carbon {
2222
// CreateFromTimestamp creates a Carbon instance from a given timestamp with second precision.
2323
func CreateFromTimestamp(timestamp int64, timezone ...string) *Carbon {
2424
var (
25-
tz string
2625
loc *Location
2726
err error
2827
)
29-
if len(timezone) > 0 {
30-
tz = timezone[0]
31-
} else {
32-
tz = DefaultTimezone
33-
}
34-
if loc, err = parseTimezone(tz); err != nil {
28+
if loc, err = parseTimezone(timezone...); err != nil {
3529
return &Carbon{Error: err}
3630
}
3731
return NewCarbon(time.Unix(timestamp, MinNanosecond).In(loc))
@@ -40,16 +34,10 @@ func CreateFromTimestamp(timestamp int64, timezone ...string) *Carbon {
4034
// CreateFromTimestampMilli creates a Carbon instance from a given timestamp with millisecond precision.
4135
func CreateFromTimestampMilli(timestampMilli int64, timezone ...string) *Carbon {
4236
var (
43-
tz string
4437
loc *Location
4538
err error
4639
)
47-
if len(timezone) > 0 {
48-
tz = timezone[0]
49-
} else {
50-
tz = DefaultTimezone
51-
}
52-
if loc, err = parseTimezone(tz); err != nil {
40+
if loc, err = parseTimezone(timezone...); err != nil {
5341
return &Carbon{Error: err}
5442
}
5543
return NewCarbon(time.Unix(timestampMilli/1e3, (timestampMilli%1e3)*1e6).In(loc))
@@ -58,16 +46,10 @@ func CreateFromTimestampMilli(timestampMilli int64, timezone ...string) *Carbon
5846
// CreateFromTimestampMicro creates a Carbon instance from a given timestamp with microsecond precision.
5947
func CreateFromTimestampMicro(timestampMicro int64, timezone ...string) *Carbon {
6048
var (
61-
tz string
6249
loc *Location
6350
err error
6451
)
65-
if len(timezone) > 0 {
66-
tz = timezone[0]
67-
} else {
68-
tz = DefaultTimezone
69-
}
70-
if loc, err = parseTimezone(tz); err != nil {
52+
if loc, err = parseTimezone(timezone...); err != nil {
7153
return &Carbon{Error: err}
7254
}
7355
return NewCarbon(time.Unix(timestampMicro/1e6, (timestampMicro%1e6)*1e3).In(loc))
@@ -76,16 +58,10 @@ func CreateFromTimestampMicro(timestampMicro int64, timezone ...string) *Carbon
7658
// CreateFromTimestampNano creates a Carbon instance from a given timestamp with nanosecond precision.
7759
func CreateFromTimestampNano(timestampNano int64, timezone ...string) *Carbon {
7860
var (
79-
tz string
8061
loc *Location
8162
err error
8263
)
83-
if len(timezone) > 0 {
84-
tz = timezone[0]
85-
} else {
86-
tz = DefaultTimezone
87-
}
88-
if loc, err = parseTimezone(tz); err != nil {
64+
if loc, err = parseTimezone(timezone...); err != nil {
8965
return &Carbon{Error: err}
9066
}
9167
return NewCarbon(time.Unix(timestampNano/1e9, timestampNano%1e9).In(loc))
@@ -158,16 +134,10 @@ func CreateFromTimeNano(hour, minute, second, nanosecond int, timezone ...string
158134
// creates a new Carbon instance from a given date, time and nanosecond.
159135
func create(year, month, day, hour, minute, second, nanosecond int, timezone ...string) *Carbon {
160136
var (
161-
tz string
162137
loc *Location
163138
err error
164139
)
165-
if len(timezone) > 0 {
166-
tz = timezone[0]
167-
} else {
168-
tz = DefaultTimezone
169-
}
170-
if loc, err = parseTimezone(tz); err != nil {
140+
if loc, err = parseTimezone(timezone...); err != nil {
171141
return &Carbon{Error: err}
172142
}
173143
return NewCarbon(time.Date(year, time.Month(month), day, hour, minute, second, nanosecond, loc))

helper.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,24 +131,30 @@ func format2layout(format string) string {
131131
// timezoneCache caches parsed timezone locations to avoid repeated parsing
132132
var timezoneCache sync.Map
133133

134-
// parses a timezone string as a time.Location instance.
135-
func parseTimezone(timezone string) (loc *Location, err error) {
136-
if timezone == "" {
134+
// parses timezone strings as a time.Location instance.
135+
func parseTimezone(timezone ...string) (loc *Location, err error) {
136+
var tz string
137+
if len(timezone) > 0 {
138+
tz = timezone[0]
139+
} else {
140+
tz = DefaultTimezone
141+
}
142+
if tz == "" {
137143
return nil, ErrEmptyTimezone()
138144
}
139145

140146
// Check cache first
141-
if cached, exists := timezoneCache.Load(timezone); exists {
147+
if cached, exists := timezoneCache.Load(tz); exists {
142148
return cached.(*Location), nil
143149
}
144150

145-
if loc, err = time.LoadLocation(timezone); err != nil {
146-
err = fmt.Errorf("%w: %w", ErrInvalidTimezone(timezone), err)
151+
if loc, err = time.LoadLocation(tz); err != nil {
152+
err = fmt.Errorf("%w: %w", ErrInvalidTimezone(tz), err)
147153
return
148154
}
149155

150156
// Cache the successful result
151-
timezoneCache.Store(timezone, loc)
157+
timezoneCache.Store(tz, loc)
152158
return
153159
}
154160

0 commit comments

Comments
 (0)