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

Fix timezone bug when clicking heatmap #15141

Merged
merged 3 commits into from
Apr 1, 2021
Merged

Fix timezone bug when clicking heatmap #15141

merged 3 commits into from
Apr 1, 2021

Conversation

mayswind
Copy link
Contributor

@mayswind mayswind commented Mar 24, 2021

I click date block in heatmap, but gitea shows the actions in other date. There are two actions in 2021-03-23 00:10 (GMT+8) and 2021-03-23 00:21 (GMT+8), but only if I click 2021-03-22, the actions in 2021-03-23 would be displayed (see the below screenshot).
1

I enabled sql log, then and I find the query date range is 1616457600 (2021-03-23 08:00:00 in GMT+8) to 1616543999 (2021-03-24 07:59:59 in GMT+8) when I click "2021-03-23" in heatmap (see the below log). The time zone of the container which runs gitea is GMT+8. I runns the official docker image gitea/gitea:1.14.0-rc2.

2021/03/24 14:47:11 ...m.io/xorm/core/db.go:286:afterProcess() [I] [SQL] SELECT `id`, `user_id`, `op_type`, `act_user_id`, `repo_id`, `comment_id`, `is_deleted`, `ref_name`, `is_private`, `content`, `created_unix` FROM `action` WHERE user_id=? AND is_deleted=? AND created_unix>=? AND created_unix<=? ORDER BY `id` DESC LIMIT 20 [1 false 1616457600 1616543999] - 1.064742ms

You can reproduce this bug when the time zone of your server/container is not UTC.

@lunny lunny added type/bug skip-changelog This PR is irrelevant for the (next) changelog, for example bug fixes for unreleased features. labels Mar 24, 2021
models/action.go Outdated Show resolved Hide resolved
@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Mar 24, 2021
@noerw
Copy link
Member

noerw commented Mar 24, 2021

I don't think this is the right fix. This problem has several layers (I don't fully understand it, I hope I summarize correctly):

  1. all activities assume the same server timezone, meaning they potentially store the incorrect time for users in other timezones
  2. the heatmap assumes timestamps in user's / browser timezone, but we pass in timestamp that is the start of day in server timezone, meaning the activities show up on the wrong day in the heatmap (or maybe the heatmap assumes UTC? need to doublecheck)
  3. the click event uses the timestamp from the heatmap, which is incorrect due to (2)

If I understand correctly, your changes would adress problem (3).. but it doesn't consider the browser timezone.

Imho the correct fix would be to actually only store UTC timestamps in the DB (and convert already stored timestamps on activities to UTC via a migration).
To address problem (1), a user setting indicating their local timezone could be added (defaulting to server timezone)

This is a longstanding issue with several proposed patches in the comments, did you take a look at this? #6087

@mayswind
Copy link
Contributor Author

I don't think this is the right fix. This problem has several layers (I don't fully understand it, I hope I summarize correctly):

  1. all activities assume the same server timezone, meaning they potentially store the incorrect time for users in other timezones
  2. the heatmap assumes timestamps in user's / browser timezone, but we pass in timestamp that is the start of day in server timezone, meaning the activities show up on the wrong day in the heatmap (or maybe the heatmap assumes UTC? need to doublecheck)
  3. the click event uses the timestamp from the heatmap, which is incorrect due to (2)

If I understand correctly, your changes would adress problem (3).. but it doesn't consider the browser timezone.

Imho the correct fix would be to actually only store UTC timestamps in the DB (and convert already stored timestamps on activities to UTC via a migration).
To address problem (1), a user setting indicating their local timezone could be added (defaulting to server timezone)

This is a longstanding issue with several proposed patches in the comments, did you take a look at this? #6087

I think it is really worth to do the work in #6087, but this pr and #13935 has nothing to do with #6087. #13935 just makes heatmap clickable, and passes "Date" parameter (converted to UTC time zone in frontend) to backend.
I think what the server should do in this pr and #13935 is just converting the "Date" parameter to unix time correctly, and this pr makes this job correct.

@silverwind
Copy link
Member

silverwind commented Mar 25, 2021

To really fix all time zone issues, we need to:

  • remove the server time zone setting
  • make all data sent and received by the server UTC

Essentially the server must not be aware of a time zone and if client wants to send/display another time zone, they need to convert from/to UTC when talking with the server.

The server can of course accept received ISO format with time zone info but needs to ensure it's converted to UTC before hitting the database.

Copy link
Member

@noerw noerw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I may have misunderstood the scope of this PR ;)
This fix seems to be a good stopgap for the issue, but to fix related issues like #6087 we should really only store UTC.

@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Mar 26, 2021
@mayswind mayswind requested a review from lunny March 29, 2021 05:38
@GiteaBot GiteaBot added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Mar 31, 2021
@6543 6543 added this to the 1.15.0 milestone Mar 31, 2021
@6543 6543 added backport/v1.14 and removed skip-changelog This PR is irrelevant for the (next) changelog, for example bug fixes for unreleased features. labels Mar 31, 2021
@lunny lunny added the backport/done All backports for this PR have been created label Apr 1, 2021
@6543
Copy link
Member

6543 commented Apr 1, 2021

🚀

@6543 6543 merged commit 9b316a3 into go-gitea:master Apr 1, 2021
@go-gitea go-gitea locked and limited conversation to collaborators May 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
backport/done All backports for this PR have been created lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. type/bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants