Force time to be stored without localization? - ruby-on-rails

We are building a Rails application that needs to read data from a database that was created, and is used, by another application that I have no control over.
The other application stores all of its time information in a UTC time field but forces local time to be stored there.
Example:
current time: 2014-04-15 11:36:54 -0600
current time in UTC: 2014-04-15 17:36:54 UTC
time stored in db: 2014-04-15 11:36:54 UTC
My problem is that Rails wants to convert the local time to UTC when it stores the time.
Is there any way I can tell Rails to not change my times to UTC when it stores them?
update:
So what a guess my real question is how can I store local time in the database but still have UTC in my models?

If you set
ActiveRecord::Base.default_timezone = :local
Then it should use the local timezone (as set by the TZ environment variable etc.). It's up to you to manage the local timezone and to take care of any oddities due to the timezone changing (eg if you observe daylight savings time)

Related

Rails + PostgreSQL: How to query timezone info along with time

I'm using Rails4 and PostgreSQL v9.6.2, my problem is that I want to process datetimes in Rails in the timezone that it was entered, it gets stored properly in PostreSQL but when I retrieve it with Rails the timezone info gets lost.
I'm using a timestamp with time zone in PostgreSQL and datetime in Rails. For example, while on PostgreSQL I see 2012-10-26 19:26:00+02, on Rails I'm getting Fri, 26 Oct 2012 17:26:00 UTC +00:00, which is technically the same date, but on UTC, or without the original timezone. I'd want to get, from Rails, the timezone info stored on PostgreSQL.
From Postgres manual:
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. If
no time zone is stated in the input string, then it is assumed to be
in the time zone indicated by the system's TimeZone parameter, and is
converted to UTC using the offset for the timezone zone.
When a timestamp with time zone value is output, it is always
converted from UTC to the current timezone zone, and displayed as
local time in that zone. To see the time in another time zone, either
change timezone or use the AT TIME ZONE construct
You can override current time zone on a session level using:
SET TIME ZONE timezone;
where timezone your desired time zone.

Time zone for Firebase.ServerValue.TIMESTAMP

It appears that when I use Firebase.ServerValue.TIMESTAMP currently, it is using PDT (I assume that will be changing to PST in the fall).
Is there a way to get this in UTC instead of PDT?
Firebase timestamps are always stored as milliseconds since the epoch (midnight of 1/1/1970 in UTC). This is the same way that dates work in Javascript and many other languages. This is a timezone-agnostic way of representing time.
Generally speaking, timezone only plays a role in how a time is displayed to a user, not in how it's represented under-the-hood. Firebase timestamps are no different.
So, if you construct a JS date object using a timestamp created by Firebase.ServerValue.TIMESTAMP, it will automatically have the same timezone as the machine on which it is being displayed.

Ruby on Rails timezone

My server is in US, rails default timezone is set to 'UTC', a user from India submits the form,Will rails convert that user's time zone from India to UTC and save it in created_at column or it will save the UTC current time?
It will save the current time in UTC. Created_at and updated_at are Active Records internal timestamp columns, and as such they are never user input.
Also, basic security protocol dictates that you should never use the client's time settings, always the servers.
Rails will do one thing though - it will use the webserver's current time in UTC and store it in the database instead of the database servers current time.
By default, times are stored in UTC + offset format. When you specify the default timezone as lets say "Mumbai" it will return the datetime objects converted for that timezone.
You can also do:
Time.zone.parse(#post.created_at.zone)

Problem with Time Comparison in Rails AR Query

I'm running Rails 3.0.4 with a PostgreSQL Database and i created a DateTime field called ordered_at.
I set it by creation with Time.now and i want to query the ordered_at field with something like where("ordered_at > ?", params[:later_than], but if I put later than with something like 1.minute.from_now, I'm getting results where no results should be.
In Detail: ordered_at is 2011-03-08 11:11:49 and params[:later_than] is 2011-03-08 11:09:58 UTC.
I think that is because of the time zone in the Time object, which doesn't exists in the database (The ordered_At field is a timestamp without time zone Datatype).
Does anyone had the same problem and can help me?
Fixed it.
The root of evil was in my fixtures.
You have to set times in your fixtures like that:
ordered_at: <%= (30.minutes.ago).iso8601 %>
otherwise you will get the timezone of the database/server
The thing you have to remember is Rails store the time in your database in UTC. Rails convert the time into UTC and sets it as the standard to look at in the database. So if you have params[:later_than] as 2011-03-08 11:11:49 UTC or any time for that matter with any time zone, Rails will convert it into UTC time zone before inserting into the SQL query. If you are saving Time.now into the database, Time.now will be the server time or local machine time with the time zone in which the machine is set. Before saving to database, Rails will convert it to UTC and save it. So, at database level everything will be converted to UTC and then will be compared so avoiding confusion.

When is it appropriate to use Time#utc in Rails 2.1?

I am working on a Rails application that needs to handle dates and times in users' time zones. We have recently migrated it to Rails 2.1 and added time zone support, but there are numerous situations in which we use Time#utc and then compare against that time. Wouldn't that be the same as comparing against the original Time object?
When is it appropriate to use Time#utc in Rails 2.1? When is it inappropriate?
If you've set:
config.time_zone = 'UTC'
In your environment.rb (it's there by default), then times will automagically get converted into UTC when ActiveRecord stores them.
Then if you set Time.zone (in a before_filter on application.rb is the usual place) to the user's Time Zone, all the times will be automagically converted into the user's timezone from the utc storage.
Just be careful with Time.now.
Also see:
http://mad.ly/2008/04/09/rails-21-time-zone-support-an-overview/
http://errtheblog.com/posts/49-a-zoned-defense - you can use the JS here to detect zones
Hope that helps.
If your application has users in multiple time zones, you should always store your times in UTC (any timezone would work, but you should stick with the most common convention).
Let the user input in local time, but convert to UTC to store (and compare, and manipulate). Then, convert back from UTC to display in each users' local time zone.

Resources