Timezone setting in Rails - ruby-on-rails

I am trying to set the ActiveRecord's time zone from UTC to current time zone. This is what I ended up
Application.rb
module application
class Application < Rails::Application
config.load_defaults 5.2
config.time_zone = 'Tokyo'.freeze
config.active_record.default_timezone = :local
end
end
I have turned off the server and restarted and made an object then when I checked the object's created_at it seems the ActiveRecords still records the data based on UTC time not Tokyo time.
Any solutions guys?

It always records the data of DateTime as UTC in Database and it will be converted according to config.time_zone if you call object.created_at
Let's say, It stored 2018-09-17 04:41:00 or Fri, 17 Sep 2018 04:41:00 UTC +00:00 in the database.
If your config the timezone as Tokyo then the result is Fri, 17 Sep 2018 13:41:00 JST +09:00
Additionally, you can use .in_time_zone method to convert the DateTime based on that zone. Here's .in_time_zone

Related

Rails Time Zone: How to pass time zone configured in application.rb file as parameter to the to_datetime method?

I configured my time zone to indian time zone in my Rails app by adding this line config.time_zone = 'Mumbai' to my application.rb file.
I am having a date time field t.datetime :check_in in my table. To this check_in column I am saving the server time like this Person.check_in = DateTime.now. When I save like this, the time is saving properly, with the time zone configured in the app. after that for some reason when I update like this Person.check_in = "24/08/2015 11:50 AM".to_datetime it is not saving the time with the time zone I configured. Below is my rails console output:
prashant#prashant-pc:~/client_proj/template$ rails c
Loading development environment (Rails 4.1.5)
2.2.2 :001 > check_in = DateTime.now
=> Mon, 24 Aug 2015 11:41:16 +0530
2.2.2 :003 > "24/08/2015 11:42 PM".to_datetime
=> Mon, 24 Aug 2015 23:42:00 +0000
2.2.2 :004 >
This is unfortunately the designed behavior of to_datetime function.
This other question is what you are after. They provide the following alternatives:
Time.zone.parse('24/08/2015 11:50 AM').to_datetime
or even:
"24/08/2015 11:50 AM".to_datetime.in_time_zone("Mumbai")
Use in_time_zone from ActiveSupport::TimeWithZone
"2015-08-14 14:38".to_datetime.in_time_zone('Mumbai')
=> Fri, 14 Aug 2015 20:08:00 IST +05:30
"2015-08-14 14:38".to_datetime.in_time_zone('Eastern Time (US & Canada)')
=> Fri, 14 Aug 2015 10:38:00 EDT -04:00
Time.now.in_time_zone("Mumbai")
=> Sat, 22 Aug 2015 12:38:32 IST +05:30
Time.now.in_time_zone("Pacific Time (US & Canada)")
=> Sat, 22 Aug 2015 00:08:21 PDT -07:00
Actually, there are several ways to do the same thing e.g. using Time.zone.local, Time.zone.parse etc. See the above link for more examples.
To, answer your exact question, to pass the time_zone configured in your application.rb file, you have to use this:
check_in = DateTime.now
check_in.in_time_zone(Rails.application.config.time_zone).to_datetime
Use local time gem. It will display the time in local time zone no matter where you are.
It is very good solution as you will don't have to call in time zone method every time you show a date in your views. The Gem has very good documentation as well. Visit https://github.com/basecamp/local_time

Passing date from angular to rails

I have a problem with passing date from Angular to Rails. In my project i use datetimepicker directive (https://github.com/dalelotts/angular-bootstrap-datetimepicker) which is using moment js. So when i pick the date it looks like "Wed Aug 05 2015 11:55:00 GMT+0300 (EEST)". Then this date is passing to rails api and rails converts it in "2015-08-05T08:55:00.000Z". So i simply need that the time received by rails was 11:55:00 not 08:55:00.
You do need to realize that Wed Aug 05 2015 11:55:00 GMT+0300 is equal to 2015-08-05T08:55:00.000Z and that the later date is a translation to UTC of the first one.
To change the timezone in which Active Record saves in the database, you can use
# application.rb
config config.time_zone = 'Eastern Time (US & Canada)'
config.active_record.default_timezone = :local
Warning! You really should think twice, even thrice, before saving times in the database in a non-UTC format.
Here's how you can find all available timezones
rake:time:all
More information: https://stackoverflow.com/a/32229086/4304188

Convert local time to UTC in Rails

In my MySQL database the updated_at field is being stored as UTC. Last week a record was entered at 7pm EDT and it's updated_at value is "2012-08-01 23:00:00". I'm trying to convert local time for a web client to UTC that can be compared against the updated_at field in the database.
As an example, I'd like to convert '08/01/2012 07:00 pm' to '2012-08-01 23:00:00' (accounting for me being in EDT) but I'm missing the time zone aspect to the conversion. '7:00 pm' is local time and could be from any time zone. My current code:
ruby-1.9.2-head :015 > from_date = DateTime.strptime('08/01/2012 07:00 pm', '%m/%d/%Y %H:%M %p')
=> Wed, 01 Aug 2012 19:00:00 +0000
ruby-1.9.2-head :016 > from_date = Time.zone.parse(from_date.to_s).utc
=> 2012-08-01 19:00:00 UTC
Any thoughts?
Edit: Also, I could use "%Z" in my strptime call but that's assuming I have the timezone in the value, which I currently do not. I guess I could use Javascript to send that along with the date/time value.
local to utc
created_at.utc
utc to local
created_at.localtime
First I would check your time zone setting.
In your environment.rb (Rails 2) or application.rb (Rails 3) file, you can set the default timezone with: config.time_zone = 'Eastern Daylight Time (EDT)'
Secondly I would look at this post for information and guidance and tips to meet your needs:
http://databasically.com/2010/10/22/what-time-is-it-or-handling-timezones-in-rails/

ActiveRecord DateTime field not matching object

i've got a Course model, which has a datetime attribute. If I look at it from the database i get one time, and if i look at it from the object i get a different date & time.
>> Course.last.attribute_for_inspect :datetime
=> "\"2012-01-04 01:00:00\""
>> Course.last.datetime
=> Tue, 03 Jan 2012 19:00:00 CST -06:00
Does anyone know why this value is different, and what I can do to fix it? The time from Course.last.datetime is correct, but my queries on the course table aren't working correctly due to the mix-up.
From the fine manual:
attribute_for_inspect(attr_name)
Returns an #inspect-like string for the value of the attribute attr_name.
[...]
Date and Time attributes are returned in the :db format.
So, when attribute_for_inspect is used for a datetime, it will return the string that the database uses for that value. Rails stores times in the database in UTC and any sensible database will use ISO 8601 for formatting timestamps on the way in and out.
2012-01-04 01:00:00 is the UTC timestamp in ISO 8601 format. When Rails returns a datetime, it converts it to an ActiveSupport::TimeWithZone instance and includes a timezone adjustment based on the timezone that you have configured in your application.rb. You're using the CST timezone and that's six hours behind UTC; subtracting six hours from 01:00 gives you 19:00 and you lose a day from crossing midnight. The human friendly format, Tue, 03 Jan 2012 19:00:00 CST -06:00, is just how ActiveSupport::TimeWithZone represents itself when inspected and the console uses x.inspect to display x.
As far as fixing your queries goes, just use t.utc before sending in a time t and you should be good.
Configuring your Rails App Timezone in application.rb
set config.active_record.default_timezone to :local as it is set to :utc by default in the application.rb
paste this code in your application.rb
config.active_record.default_timezone = :local #or :utc
config.time_zone = "Singapore" #your timezone

Is there timezone offset bug in ruby on rails?

I'm in Korea.
Rails version is 2.3.5
in my environment,
config.time_zone = 'Seoul'
config.active_record.default_timezone = 'Seoul'
created_at and updated_at value is inserted with local time in database.
but, when I access model.created_at or model.updated_at,
the value is understanding with UTC +9:00
So, I insert model to database and get the value, and check difference both values immediately,
It is different bellow:
>> Time.now.to_datetime
=> Sun, 24 Jan 2010 21:28:03 +0900
# insert new Product. And check difference Time.now and Product.created_at immediately.
>> Product.last.created_at.to_datetime
=> Mon, 25 Jan 2010 06:12:51 +0900
Is it a bug of rails?
There is a known bug in Rails:
Rails Timezone Gotcha: ActiveRecord::Base.find does not convert Time objects to UTC
http://marklunds.com/articles/one/402

Resources