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

a problem about where condition of time.Time with timezone #189

Open
wlg1024 opened this issue May 19, 2024 · 0 comments
Open

a problem about where condition of time.Time with timezone #189

wlg1024 opened this issue May 19, 2024 · 0 comments
Assignees

Comments

@wlg1024
Copy link

wlg1024 commented May 19, 2024

Description

I noticed that gorm sqlite will reserve timezone when it saves the field whose type is time.Time ,
but it ignores the timezone in where condition.

for example:

db.Create(&Record{ Time : time.Now() })
// my local timezone is "+8 Hours" ,assume that the value of  Time is set to "2024-05-01 09:00:00+08:00"
var records []Record
db.Where("time >= ? , time.Date(2024,5,1,8,0,0,0,time.UTC) ).Find(&records)

In fact, "2024-05-01 09:00:00+08:00" is before than "2024-05-01 08:00:00 UTC" which is equal with "2024-05-01 16:00:00+08:00",
so records should not have the record that we created just now.
but gorm may ignore the timezone when compare the time field, so that records hold the record incorrectly.
This result is very counterintuitive.

I print the sql statement.

SELECT * FROM `records` WHERE  time >= "2024-05-01 08:00:00"

gorm converted the time in condition to string "2024-05-01 08:00:00" without timezone, so sqlite just compared the string and got a wrong result.

I suggest that gorm should reserve timezone when it save and compare similarly ,or convert the field to UTC uniformly when it save and compare.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants