Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jiaozhongy opened this issue Apr 23, 2024 · 1 comment
Open
Labels
Bug Report a reproducible bug or regression

Comments

@jiaozhongy
Copy link

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()
}

@jiaozhongy jiaozhongy added the Bug Report a reproducible bug or regression label Apr 23, 2024
@Markle-ddp
Copy link
Contributor

you should use SetDefault function set the default timezone

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Report a reproducible bug or regression
Development

No branches or pull requests

2 participants