0

This strange behavior has recently came to my attention, while I was testing my Rails app on local environment in which I use around_filter to set the timezone to registered user (the default timezone is UTC).

What I did was that I registered a new user in my app. My current time was 10pm GMT-5 (March 3), and this user's created_at time was saved to database to 4am UTC (March 4). Now, I know that this time is saved in database with the timezone settings, but here comes the problem:

I use a graph for visual representation of daily registered users, and when I called the following function to tell me number of users registered in the last few days:

from ||= Date.today - 1.month
to ||= Date.today
where(created_at: from..to).group('DATE(created_at)').count

It would say that this user was registered in March 4, while it was in fact registered on March 3 from my perspective.

My question is: How should I call where function and group by a created_at column, so that the dates with be affected correctly (according to my timezone) ?

Or is there something else that I should be doing differently?

2 Answers 2

1

I'm not a rubyist, so I'll let someone else give the specific code, but I can answer from a general algorithmic perspective.

If you're storing UTC in the database, then you need to query by UTC as well.

In determining the range of the query (the from and to), you'll need to know the start and stop times for "today" in your local time zone, and convert those each to UTC.

For example, I'm in the US Pacific time zone, and today is March 7th, 2015.

from: 2015-03-07T00:00:00-08:00  =  2015-03-07T08:00:00Z
  to: 2015-03-08T00:00:00-08:00  =  2015-03-08T08:00:00Z

If you want to subtract a month like you showed in the example, do it before you convert to UTC. And watch out for daylight saving time. There's no guarantee the offsets will be the same.

Also, you'll want to use a half-open interval range that excludes the upper bound. I believe in Ruby that this is done with three dots (...) instead of two (at least according to this).

Grouping is usually a bit more difficult. I assume this is a query against a database, right? Well, if the db you're querying has time zone support, then you could use it convert the date to your time zone before grouping. Something like this (pseudocode):

groupby(DATE(CONVERT_TZ(created_at,'UTC','America/Los_Angeles')))

Since you didn't state what DB you're using, I can't be more specific. CONVERT_TZ is available on MySQL, and I believe Oracle and Postgres both have time zone support as well.

1

Date.today will default to your system's set timezone (which by the way should always be UTC, here's why) so if you want to use UTC, simply do Time.zone.now.to_date if rails is set to UTC

Otherwise you should do

Time.use_zone('UTC') do
  Time.zone.now.to_date
end

After this you should display the created_at dates by doing object.created_at.in_time_zone('EST')
to show it in your current timezone

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.