golang-module / carbon

A simple, semantic and developer-friendly golang package for time

Home Page:https://pkg.go.dev/github.com/golang-module/carbon/v2

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

IsToday()、IsYesterday()、IsTomorrow()in other timeZone is invaild

jiaozhongy opened this issue · comments

IsToday(), IsYesterday(), and IsTomorrow() functions in the carbon package. These functions are based on local time, which can lead to incorrect results when used in different time zones.

For example, if you're running the IsToday() function in the Beijing time zone (UTC+8), but the system time is actually in the New York time zone (UTC-5), the function may return false even though it should return true according to New York time. This is because there's a 13-hour difference between these two time zones, and when it's a new day in Beijing, it might still be the previous day in New York.

The same issue applies to the IsYesterday() and IsTomorrow() functions. They don't take into account the impact of time zones, so they may return incorrect results when used in different time zones.

A possible solution to this issue could be to add a time zone parameter to these functions, or to add a time zone field to the Carbon struct. This would allow the functions to consider the impact of time zones when comparing dates.

for example :

println(carbon.Now(carbon.NewYork).IsToday())

output :false

// IsToday reports whether is today.
// 是否是今天
func (c Carbon) IsToday() bool {
if c.IsInvalid() {
return false
}
return c.ToDateString() == Now(c.loc).ToDateString()
}

you should use SetDefault function set the default timezone