Skip to content

Commit 5397b8b

Browse files
committed
fix: Fix bug of missing timezone in TestNow settings
1 parent bc12b62 commit 5397b8b

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

traveler.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ import (
77
// Now returns a Carbon instance for now.
88
// 当前
99
func Now(timezone ...string) *Carbon {
10-
if IsTestNow() {
11-
return frozenNow.testNow.Copy()
12-
}
1310
var (
1411
tz string
1512
loc *Location
@@ -20,6 +17,9 @@ func Now(timezone ...string) *Carbon {
2017
} else {
2118
tz = DefaultTimezone
2219
}
20+
if IsTestNow() {
21+
return frozenNow.testNow.Copy().SetTimezone(tz)
22+
}
2323
if loc, err = parseTimezone(tz); err != nil {
2424
return &Carbon{Error: err}
2525
}

traveler_example_test.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,38 @@ func ExampleNow() {
1111

1212
carbon.SetTestNow(carbon.Parse("2020-08-05"))
1313

14-
fmt.Println(carbon.Now(carbon.UTC).ToString())
14+
fmt.Println(carbon.Now().ToString())
15+
fmt.Println(carbon.Now(carbon.PRC).ToString())
1516

1617
// Output:
1718
// 2020-08-05 00:00:00 +0000 UTC
19+
// 2020-08-05 08:00:00 +0800 CST
1820
}
1921

2022
func ExampleTomorrow() {
2123
defer carbon.CleanTestNow()
2224

2325
carbon.SetTestNow(carbon.Parse("2020-08-05"))
2426

25-
fmt.Println(carbon.Tomorrow(carbon.UTC).ToString())
27+
fmt.Println(carbon.Tomorrow().ToString())
28+
fmt.Println(carbon.Tomorrow(carbon.PRC).ToString())
2629

2730
// Output:
2831
// 2020-08-06 00:00:00 +0000 UTC
32+
// 2020-08-06 08:00:00 +0800 CST
2933
}
3034

3135
func ExampleYesterday() {
3236
defer carbon.CleanTestNow()
3337

3438
carbon.SetTestNow(carbon.Parse("2020-08-05"))
3539

36-
fmt.Println(carbon.Yesterday(carbon.UTC).ToString())
40+
fmt.Println(carbon.Yesterday().ToString())
41+
fmt.Println(carbon.Yesterday(carbon.PRC).ToString())
3742

3843
// Output:
3944
// 2020-08-04 00:00:00 +0000 UTC
45+
// 2020-08-04 08:00:00 +0800 CST
4046
}
4147

4248
func ExampleCarbon_AddDuration() {

0 commit comments

Comments
 (0)