Why Date.today is inserting previous date in Database? - ruby-on-rails

Date.today is showing correct date but when I used Date.today in Model.create statement it is inserting previous date in database Whereas Date.today.to_datetime is inserting correct date in database.
My question is why Date.today in Model.create method is inserting previous date in DB?

In Rails there is a setup for a timezone in /config/application.rb :
config.time_zone = 'Central Time (US & Canada)'
For consistency all the times are stored in the database of Rails in UTC . So if you try on the console:
$ rake time:zones:local
you sold see all available time zones .
You can see the difference typing :
Time.now
and
Time.zone.now
I think default columns for time of creation and update , that Rails provides , should be OK for the most of cases.

Related

use table's timestamp column(without timezone) as EST

I have a table 'A' in db which have a column 'x' of type 'timestamp without timezone' and in rails project I have set timezone to 'EST'. Everything works fine when I create/update record from my project.
The problem is that I imported data from another DB using PSQL. 'x' column have the same time as in old DB but when I use 'ActiveRecord' query, I get '+05:00' offset. I am confused how to tell 'ActiveRecord' that that time is already in 'EST'?
here is a solution by me.
=> A.first.x.to_s(:db)
# "1900-01-01 21:47:00"
=> A.first.x
#Mon, 01 Jan 1900 16:47:00 EST -05:00
but is there any other better option ?
Adding following to application.rb must work
config.time_zone = 'Eastern Time (US & Canada)'
config.active_record.default_timezone = 'Eastern Time (US & Canada)'
or Every zone you wish
You can convert your time in 'EST' time zone in your query.
You can find your answer here

Rails 4 - Want To Have Rails Use My Local Time Zone for created_at & updated_at

Is there actually a way to have Rails add/update rows with created_at and update_at using the current time zone as set on my server?
I have seen many Stack Overflow solutions where people stated how to display it on a Rails view with a selected time zone.
I have also found other Stack Overflow solutions stating that it should take the time zone from my server and update created_at and updated_at. This is not true at least for the Mac Mini Server I'm running on. I have it set to my local time zone. I also have config.time_zone = 'Central Time (US & Canada)' set in config/application.rb.
I want to be able to look at the raw database data in pgAdmin3 or some kind of database backup and know when records have been created in my time zone.
Any help would be appreciated. I will keep looking.
If you want to set the db timezone and you are using ActiveRecord, then add the following in your application config:
config.active_record.default_timezone = :local
This will use your sever's timezone on the database.
# application.rb:
class Application < Rails::Application
config.time_zone = 'Eastern Time (US & Canada)'
end
Time.zone # => #<TimeZone:0x514834...>
Time.zone.name # => "Eastern Time (US & Canada)"
Time.zone.now # => Sun, 18 May 2008 14:30:44 EDT -04:00
Please refer following link >>http://api.rubyonrails.org/classes/ActiveSupport/TimeZone.html#method-c-5B-5D
You can also run rake time:zones:all in order to list all time zones Rails knows. You can then update your setting in application.rb accordingly.

Different date saved to database - wrong time zone

When I fetch a date from FullCalendar with Javascript I have the following values:
Fri Sep 13 2013 08:30:33 GMT-0400 (EDT)
After I save that date to database I see that in database there are different values:
2013-09-13 12:00:00.000000
So, date is automatically converted to UTC and saved like that into database. How to save the correct date into database? I don't want to hardcode it :)
Thank you.
For that you need to define your time zone in your application.rb
config.time_zone = 'YOUR-TIME-ZONE'
config.active_record.default_timezone = :local
config.active_record.time_zone_aware_attributes = false
By default, the rails app timezone is UTC. Whenever the record is saved the date, datetime, time fields are saved with DB's timezone but when they are fetched back in models it is converted back to UTC.
So, in your case set your application timezone in application.rb like this
config.time_zone = 'Central Time (US & Canada)'
Now, whenever the records are fetched from database it will always be converted to CST timezone irrespective of DBs timezone.
Hope this will help.

Rails and dates, are they stored in UTC by default?

What is the best way for me to handle dates and timezones in rails?
Scenerio: I have customers who purchase products on a website from all over the world, and when they log in they will be able to choose which timezone they are from.
So I believe I should be storing everything in the database at UTC, and then on the front-end I should be converting the dates to the users set timezone preference.
Are their any gotchas with Ruby and Rails and datetimes etc?
I'm new to rails, so I am looking for guidance on how to handle this properly.
Fortunately Rails will pretty much handle things for you. As others pointed out, dates are stored by AR in UTC format. If you have a time_zone field for your users table you can do something like this:
# application.rb
config.time_zone = "Mountain Time (US & Canada)" # Default time zone
-
# application_controller.rb
before_filter :set_time_zone, :if => :logged_in?
protected
def set_time_zone
Time.zone = current_user.time_zone if current_user.time_zone
end
All the datetimes should be shown in the proper time zone in your views.
I have had one production app that didn't like using the default time zone, but I can't remember which version of Rails/Ruby it was running.
Ok so take a look at your config/application.rb file.
You should find commented lines:
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'
So default is UTC but you can set whatever ou need there.
Yes, they are. In app, whenever you display date or time for user, everything you need is just adding timezone offset (for example: date + 1.hour for GMT+1). Remember that you need to take care of daylight saving, too. For efficency, consider adding 2 columns in your user table: timezone and time_offset. Then you would on each case do something like
= #order.created_at + session[:user].time_offset
Instead of always checking offset for each timezone set in profile.
I found
rake time:zones:all
to be really useful. It shows a list of offsets and then zone name strings under that. eg:
* UTC +12:00 *
Auckland
Fiji
Kamchatka
Magadan
Marshall Is.
Solomon Is.
Wellington
I needed below in application.rb (not intuitive given default time zone string of "Mountain Time (US & Canada)"):
config.time_zone = 'Wellington'

Rails 3, updated_at is 1 day ahead?

For some reason when updating a record, updated_at is getting updated as 2011-01-23, even though Date.today is returning 2011-01-22. Why are these different? Is there a way to set them to be the same?
Thanks!
By default, Rails uses UTC as the timezone for all created_at and updated_at fields. You can override this by setting a custom timezone in config/application.rb:
config.time_zone = 'Central Time (US & Canada)'
You can run rake time:zones:all from your application directory to get a list of all valid time zones.
Did you set your time zone in the config/environment.rb file?

Resources