I have to show how many api calls made a user during each day for the last 30 days, in order to build a nice graph. I didn’t have any problem so far with that. Something important to notice is that the database and system timezone is in UTC, but most of the users are located in Pacific Time (GMT -8).
In order to get the api usage grouped by the day of use, I can do something like
ApiCall.
where(user_id: @user.id).
where("requested_at > ?", Time.zone.now.to_date - 30.days).
group("date(requested_at)").
select("count(*) as qty, date(requested_at) as requested_at").all
Now the problem, is that the time for a user located in California, is far different from the time for a user located in England and that’s where I’m failing.
A user makes an API Call, located in California, on 2012-12-14 21:00:00 GMT -8. The API CALL in the DB is stored with the time 2012-12-14 05:00:00 (because its in GMT, so it add 8 hours).
Now for that user, if I go to see my daily usage, with the query I did, it will show that my api call was not made on 2012-12-14 at 21:00:00, because on the database its stored as 2012-12-14 05:00:00, and for the user the api usage will show that he did that api call on the next day, and he will think that the system is not working fine.
So in few words, how can I group the api calls based on the user timezone and not based on the database timezone?
Yes there is, by using mysql convert timezone function, its not a nice solution yet it works
I’ll add a bounty later to see if someone can tell me about a better solution to these kind of reports with rails and timezones