I have an application which uses many different timezones... it sets them in a controller and they change depending on the user. All the times are stored in UTC without a timestamp etc.
My understanding is this is the normal way for Rails to handle timezones. This works fine 99% of the time until i need to do something directly with Postgres then the Timezone becomes a problem.
For example this query is completely wrong except for GMT, for example in Central Time Zone, depending on the hour set, it gets the wrong day:
Events.where("EXTRACT(dow FROM start_at)", Date.today.wday)
Where I'm trying to find all the dates that fall on a certain day.
I'm trying to do something like this. I don't need to search between timezones (they won't be mixed), but I do need to specify the timezone if it's not UTC to get correct results.
User.events.where("EXTRACT(dow FROM start_at AT TIME ZONE ?) = ?", 'UTC', Date.today.wday)
But I'm not sure how to use Time.zone to give me something that will work with TIME ZONE in Postgres.
Time.zone.tzinfo sometimes works... Postgres will work with 'Europe/Warsaw' but Rails returns 'Europe - Warsaw'
In general I'm not having much luck with timezones, any pointers would be appreciated.
Maybe someone else has a better overall solution, but what you need for the particular query is
Time.zone.tzinfo.identifier
Or, in your example:
User.events.where("EXTRACT(dow FROM start_at AT TIME ZONE ?) = ?", Time.zone.tzinfo.identifier, Date.today.wday)
Try using the Ruby TZInfo gem directly, instead of using Rails ActiveSupport::TimeZone.
Alternatively, use the MAPPING constant, as shown in the ActiveSupport::TimeZone documentation, which will take you from a Rails time zone key back to the standard IANA time zone identifier used by Postgres and others.
As Matt Johnson suggested use TZInfo gem directly. This way you can get the correctly formatted time zone identifiers you need to query with PostgreSQL.
For example if you use:
TZInfo::Timezone.all_country_zone_identifiers
This will return an array of correct IANA/Olson time zone identifiers. In other words you will get the correct 'Europe/Warsaw' NOT 'Europe - Warsaw'.
Related
I have a background job that runs every 15 minutes and generates reminder emails. I would like to create a query that returns all users who have a specific time saved and respect how their timezone setting effects that time.
So I have a User model that stores:
:time: a users reminder time, eg 17:00:00
:string: their timezone, eg EST
So if the job runs at 17:00:00 EST, it will return users whose settings are:
reminder_time: 17:00:00, time_zone: EST
reminder_time: 13:00:00, time_zone: PST
What is the best way to build that query? Can it be done in one pass, relying on Postgres to handle the work? Do I have to stagger it, group by each time zone and doing the math for each on in Ruby?
I currently have this setup as an ActiveRecord scope that doesn't consider timezones, and I am trying to add that consideration now.
scope :receives_reminder_at, -> (time) do
ready.where(reminder_time: time)
end
When dealing with users in multiple timezones, it is normally easiest to standardize on UTC.
So store the reminder_time in UTC, this way you don't have to worry about the TZ when querying, since they will all be normalized to UTC. (assuming you are running your servers UTC. it will just work as expected). Then you just use their TZ offset in order adjust the time for their viewing.
You could use a select on User model. Something like:
User.select{|user| user.time == time_the_job_runs.in_time_zone(user.string)}
You should replace the "time_the_job_runs". It didn't got clear for me how to get it. But the in_time_zone method should be the one you're looking for to convert time based on a timezone string. Hope it helps, thanks!
Just met same problem. And found article how to make it, just how to think solve it myself. But with ready code. https://robots.thoughtbot.com/a-case-study-in-multiple-time-zones
Main idea is to:
At first you find timezones which have specific time now.
module ActiveSupport
class TimeZone
def self.current_zones(hour)
all.select { |zone|
t = Time.current.in_time_zone(zone)
t.hour == hour
}.map(&:tzinfo).map(&:name)
end
end
end
You find users with this timezone.
User.where(zone: ActiveSupport::TimeZone.current_zones(hour)).where(options)
I need to use the query similar to
SELECT * FROM items WHERE to_char(created_at AT TIME ZONE 'RAILS_GIVEN_ZONE', 'DD/MM/YYYY') ILIKE '%5/02%'
where the RAILS_GIVEN_ZONE value should always use the time zone from the Rails 4 app (which could be changed by the user), not the PG's timezone option.
But the problem is that the timezones from Rails and PG do not correspond 1-to-1 exactly.
Using the offset (as in +10:00 from Time.zone.now.formatted_offset) isn't good enough since in this case PG will not be able to deal with the daylight saving time correctly.
So the question is what is the best way to automatically map Rails current time zone (Time.zone) to the PostgreSQL's named time zone?
NOTE: the create_at column is timestamptz (stored as UTC, displayed in whatever zone is necessary)
There seems to be a MAPPING constant defined in ActiveSupport::TimeZone, which contains values that, unless I am mistaking, should all be supported by Postgres:
http://api.rubyonrails.org/classes/ActiveSupport/TimeZone.html
Per the docs:
Keys are Rails TimeZone names, values are TZInfo identifiers.
If you don't want to use the MAPPING constant directly, there's a find_tzinfo method in there, which seems to return aTZInfo::TimezoneProxy:
http://rubydoc.info/gems/tzinfo/TZInfo/TimezoneProxy
The latter sports an identifier method that should contain the needed string.
I'm trying to store all timestamps in a rails application with their included timezone. I'm fine with ActiveRecord converting them to utc, but I have multiple applications hitting the same database, some of which are implemented with a timezone requirement. So what I want to do is get activerecord to convert my timestamps as usual, then write them to the database with the string 'America/Los_Angeles', or whatever appropriate timezone, appended to the timestamp. I am currently running rails 3.2.13 on jruby 1.7.8, which implements the ruby 1.9.3 api. My database is postgres 9.2.4, connected with the activerecord-jdbcpostgresql-adapter gem. The column type is timestamp with time zone.
I have already changed the natural activerecord mappings with the activerecord-native_db_types_override gem, by adding the following lines to my environment.rb:
NativeDbTypesOverride.configure({
postgres: {
datetime: { name: "timestamp with time zone" },
timestamp: { name: "timestamp with time zone" }
}
})
My application.rb currently contains
config.active_record.default_timezone = :utc
config.time_zone = "Pacific Time (US & Canada)"
I suspect I can rewrite ActiveSupport::TimeWithZone.to_s and change it's :db format to output the proper string, but I haven't been able to make that work just yet. Any help is much appreciated.
After banging my head against this same problem, I learned the sad truth of the matter:
Postgres does not support storing time zones in any of its date / time types.
So there is simply no way for you to store both a single moment in time and its time zone in one column. Before I propose an alternative solution, let me just back that up with the Postgres docs:
All timezone-aware dates and times are stored internally in UTC. They are converted to local time in the zone specified by the TimeZone configuration parameter before being displayed to the client.
So there is no good way for you to simply "append" the timezone to the timestamp. But that's not so terrible, I promise! It just means you need another column.
My (rather simple) proposed solution:
Store the timezone in a string column (gross, I know).
Instead of overwriting to_s, just write a getter.
Assuming you need this on the explodes_at column:
def local_explodes_at
explodes_at.in_time_zone(self.time_zone)
end
If you want to automatically store the time zone, overwrite your setter:
def explodes_at=(t)
self.explodes_at = t
self.time_zone = t.zone #Assumes that the time stamp has the correct offset already
end
In order to ensure that t.zone returns the right time zone, Time.zone needs to be set to the correct zone. You can easily vary Time.zone for each application, user, or object using an around filter (Railscast). There are lots of ways to do this, I just like Ryan Bates' approach, so implement it in a way that makes sense for your application.
And if you want to get fancy, and you need this getter on multiple columns, you could loop through all of your columns and define a method for each datetime:
YourModel.columns.each do |c|
if c.type == :datetime
define_method "local_#{c.name}" do
self.send(c.name).in_time_zone(self.time_zone)
end
end
end
YourModel.first.local_created_at #=> Works.
YourModel.first.local_updated_at #=> This, too.
YourModel.first.local_explodes_at #=> Ooo la la
This does not include a setter method because you really would not want every single datetime column to be able to write to self.time_zone. You'll have to decide where this gets used. And if you want to get really fancy, you could implement this across all of your models by defining it within a module and importing it into each model.
module AwesomeDateTimeReader
self.columns.each do |c|
if c.type == :datetime
define_method "local_#{c.name}" do
self.send(c.name).in_time_zone(self.time_zone)
end
end
end
end
class YourModel < ActiveRecord::Base
include AwesomeDateTimeReader
...
end
Here's a related helpful answer: Ignoring timezones altogether in Rails and PostgreSQL
Hope this helps!
May i suggest saving them in iso8601
That will allow you to:
Have the option of storing them as UTC as well
as with a timezone offset
Being international standards compliant
Use the same storage format in both cases with offset and
without.
So one of the db columns can be with a offset one in just UTC form (usual).
From the Ruby side it is as simple as
Time.now.iso8601
Time.now.utc.iso8601
ActiveRecord should work seamlessly with the conversion.
Also, most API's use this format (google) hence best for cross app compatibility.
to_char() for postgresql should give you the right format in case there is any hiccup with the default setup.
One approach, as you suggest, would be to override ActiveSupport::TimeWithZone.to_s
You might try something like this:
def to_s(format = :default)
if format == :db
time_with_timezone_format
elsif formatter = ::Time::DATE_FORMATS[format]
formatter.respond_to?(:call) ? formatter.call(self).to_s : strftime(formatter)
else
time_with_timezone_format
end
end
private
def time_with_timezone_format
"#{time.strftime("%Y-%m-%d %H:%M:%S")} #{formatted_offset(false, 'UTC')}" # mimicking Ruby 1.9 Time#to_s format
end
I haven't tested this but looking at Postgres' docs on Time Stamps, this looks valid and would give a time like: "2013-12-26 10:41:50 +0000"
The problem with this, as far as I can tell, is that you would still have trouble returning the right timezone:
For timestamp with time zone, the internally stored value is always in UTC (Universal Coordinated Time, traditionally known as Greenwich Mean Time, GMT). An input value that has an explicit time zone specified is converted to UTC using the appropriate offset for that time zone.
This is exactly what the original ActiveSupport::TimeWithZone.to_s is already doing.
So perhaps the best way to get the correct Time Zone is to set an explicit Time Zone value as a new column in the database.
This would mean that you would be able to keep the native date functionality of both Postgres and Rails while also being able to display the time in the correct timezone where necessary.
You could use this new column to then display the right zone using Ruby's Time::getlocal or Rails' ActiveSupport::TimeWithZone.in_time_zone.
I'm using Rails 3.2.8. When I generate a scaffold with a time field or datetime field. The HTML form field gets pre-populated with the current date/time in UTC. Great that it's pre-populated with the current date/time, but because it's in UTC, we have to change the time back 7 hours every time (which will sometimes require setting the day back 1 also, possibly month and year too). And then it seems the UTC time gets stored in the database, so I'll have issues displaying/editing it as well if I recorded it in our local time.
I looked at the Ruby documentation for the form helpers to deal with local time better, but I don't see anything relevant.
What's the best way to deal with editing and displaying dates and times in Rails?
UPDATE: To clarify, I like the idea that UTC time is stored in the database. I just want the time_select and datetime_select form helpers to edit the times in the user's local timezone. And, of course, I want to easily display the time in the user's local timezone.
You have to set the time zone to display on a per-user basis somewhere in a before/around_filter. This has to be done like:
Time.zone = "Kyiv"
An example can be found in its API: http://api.rubyonrails.org/classes/Time.html#method-c-zone-3D.
Run the rake time:zones:all to see all of them.
All the conversions has to be handled for you behind the scene.
This has helped me in the past. You can do things like:
Time.now.in_time_zone("Central Time (US & Canada)")
See Rails: convert UTC DateTime to another time zone
I would like my reports to display in CST while my database is in UTC. For this to work I need to be able to group by a time column and have the db do timezone conversions.
This works on my dev postgresql box:
offset = -5
result = ActiveRecord::Base.connection.execute("select date(srl.created_at AT TIME
ZONE '#{offset.to_s}') AS date, count(*) from server_request_logs srl where
srl.created_at between '#{#from.to_s(:db)}' and '#{#to.to_s(:db)}' group by
date(srl.created_at AT TIME ZONE '#{offset.to_s}')")
on heroku it errors with:
ActiveRecord::StatementInvalid: PGError: ERROR: time zone "-5" not recognized
Its also not working with TZInfo names like 'America/Winnipeg' or supported timezones like 'CST' from http://www.postgresql.org/docs/7.2/static/timezones.html
has anyone been able to get this working?
First off, make sure that you define your timestamp columns and variables as TIMESTAMP WITH TIME ZONE (or timestamptz for short). In PostgreSQL this doesn't actually cause any time stamp to be saved; but makes it a fixed point in time, stored in UTC. You can view it AT TIME ZONE of your choosing with clean semantics. TIMESTAMP WITHOUT TIME ZONE (which is what you get if you just say TIMESTAMP) is not a fixed point in time until it is resolved against a time zone, and is therefore much harder to work with.
The documentation page you cite regarding time zones is from a very old version of PostgreSQL which has gone out of support. Maybe this page will be of more help to you:
http://www.postgresql.org/docs/current/interactive/datetime-config-files.html