Testing
Supports freezing a fixed time, setting any time as the current time, so that subsequent operations run based on this simulated time rather than the real system time, which is convenient for unit testing
Set freeze time as test now
go
now := carbon.Parse("2020-08-05")
carbon.SetTestNow(now)
carbon.Now().ToDateString() // 2020-08-05
carbon.Yesterday().ToDateString() // 2020-08-04
carbon.Tomorrow().ToDateString() // 2020-08-06
carbon.Now().DiffForHumans() // just now
carbon.Yesterday().DiffForHumans() // 1 day ago
carbon.Tomorrow().DiffForHumans() // 1 day from now
carbon.Parse("2020-10-05").DiffForHumans() // 2 months from now
now.DiffForHumans(carbon.Parse("2020-10-05")) // 2 months beforeIs test time
go
carbon.IsTestNow()Clear test now
go
carbon.ClearTestNow()Sleep
When time is frozen, Sleep does not wait for real time but directly advances the frozen current time; when not frozen, it behaves the same as time.Sleep.
go
carbon.SetTestNow(carbon.Parse("2020-08-05 13:14:15"))
carbon.Sleep(2 * time.Hour)
carbon.Now().ToDateTimeString() // 2020-08-05 15:14:15