TimeZone convertion in Ruby on Rails - ruby-on-rails

Im my application.rb I've set: config.time_zone = 'Brasilia'. It's ok when I save datetimes in the system. The problem is that I receive datetimes from another system that is already on the correct timezone. So, when this datetime is saved on my RoR system, it's being saved wrongly. Example: The another system sends a datetime 2015-11-10 15:07:00 (that is already on the right timezone, ready to save). But my RoR saves it like 2015-11-10 13:07:00 -0200. Is there a way to tell Rails that this datetime is already on the correct timezone?

You need to tell Rails/Ruby which timezone the incoming datetime is in, see a list of timezones using rake time:zones:all and then use the right one in the following code, eg.
Time.use_zone("Montevideo") { Time.zone.parse "2015-11-10 15:07:00" }
That will return an ActiveSupport::TimeWithZone object with the correct timezone set, and then you'll just store it in your DB and Rails will convert it to the correct UTC time.

Related

Ruby on Rails - Handling of DateTime.current and User Input Time

I'm using Rails 3.2.9 with Postgres database and JRuby 1.7.12.
Application is running Ubuntu Server with GMT+7 Asia/Bangkok Time Zone.
config.time_zone is set to Bangkok
Postgres timezone is also set to Asia/Bangkok
In my invoice model, I have 2 datetime fields called document_date and movement_date
-> movement_date is generated by application using DateTime.current
-> document_date is input by the User (i.e 2014-07-28)
When User save the Invoice, both will be converted to UTC and saved into Database.
for movement_date, it is fine (as this is the expected behaviour)
for document_date, when it is convert to UTC, it will be saved as 2014-07-27 17:00.
How can I save document_date to 2014-07-28 00:00 into database without converting to UTC.
Your Help, Advise, Suggestion would be highly appreciated and Many Thanks in advance.
If there are any additional info I should add, kindly let me know.
EDIT: the field in postgresql for both is using Timestamp Without Time Zone
If i understood it correctly, you want the document_date (which is an user input) to be in UTC, rather than in your local time. Is it correct? If so, you'll have to change it manually, as Rails makes the assumption that you want it in your local time.
What you could do is set it manually, converting the date string to datetime (String#to_datime), just before the object saving on your controller. Something like that:
def create
#invoice = Invoice.new(params[:invoice])
#invoice.document_date = params[:invoice][:document_date].to_datetime
#invoice.save
end
String#to_datetime will create a UTC DateTime as long as the string doesn't have Time and TimeZone parts on it (ie: "2014-07-28 00:00:00 +0700" will be converted to Bangkok DateTime).

How to save dates in local timezone to db with rails3?

I have Rails3 application with model user and field expires_at created like this:
t.column :expires_at, :timestamp
In my database (postgresql) it has type:
timestamp without timezone
The problem is when I call:
#user.expires_at = Time.now
#user.save
it is saved into database with UTC timezone (my local time is UTC + 1:00, Warsaw) but I don't want that. I just want to have time with my local timezone saved into the database (2011-03-30 01:29:01.766709, not 2011-03-29 23:29:01.766709)
Can I achieve this using rails3?
For saving time in local timezone to database this has to be set in application.rb
config.active_record.default_timezone = :local
If you only want to use local times on certain columns, rather than as a global setting, then the Rails documentation tells us this:
# If your attributes are time zone aware and you desire to skip time zone conversion to the current Time#zone when reading certain attributes then you can do following:
class Topic < ActiveRecord::Base
self.skip_time_zone_conversion_for_attributes = [:written_on]
end
(This also skips time zone conversion on writing, not just reading). And you can pass in an array of symbols for multiple attributes.
I am not sure which versions of Rails this was introduced in, though.

time_select with 12 hour time and Time Zone in Ruby on Rails

I have the need to capture a time and time zone from users of a rails 2.3.8 app, but have been unable to think of a clean solution to create and parse the selections.
Ideally I would have a drop-down menus for the following:
hour (1-12)
minute (0-59)
AM/PM
Time Zone
Is there a gem/plugin that accomplishes what I am looking for? Will I need to store both the time and time zone in the database? What is the best strategy for storage?
I'll eventually need to spit these values out in UTC, but a user should be able to go back and review the time in the correct time zone.
You can get time zone selects with the appropriate methods:
time_zone_options_for_select
time_zone_select
Similarly, there's date_select for dates.
Storage:
If the timezone is specific to the user and doesn't change, then store their time zone in their user record and set it when you load the current_user. Rails will convert times to/from UTC and always store UTC in the database and do the automatic convert to that default timezone for you (including daylight savings!). Easiest way to do it.
use_zone(zone) lets you override the default zone for a block, so you can accept a form value and set it with that function and set your value in that block.
UPDATE: I wrote up some Rails timezone examples as a blog entry.
I personally used jQuery to change the display only:
ampm = ["12 AM","01 AM","02 AM","03 AM","04 AM","05 AM","06 AM","07 AM","08 AM","09 AM","10 AM","11 AM","12 PM","01 PM","12 PM","01 PM","02 PM","01 PM","02 PM","03 PM","04 PM","05 PM","06 PM","07 PM","08 PM","09 PM","10 PM","11 PM"]
j("#game_start_time_4i option").each(function(index,value){
j(value).html(ampm[index]);
});
Rails:
<%= datetime_select('game', 'start_time') %>

why is the datetime in database different when I output it?

In my database on heroku, it shows contactemail.created_at = "2010-08-08 17:16:19"
However, when I use puts.contactemail.created_at I get something different. I get:
2010-08-08 10:11:13 -0700
I need to input that value through an API to another application, and I am pretty sure that the first format is what it wants. If it doesn't take that, it wants 08/08/10 17:16:19 -- in either case, I don't know how to format it properly.
This is in Ruby on Rails.
The display of date is based on your servers locale settings. If you are looking for the first format you could try
puts.contactemail.created_at.to_s(:db)
Have a look at strftime doc here http://ruby-doc.org/core/classes/Time.html#M000298 to get the second format
HTH
Ruby on rails can manage different time zones.
You can:
Make a direct sql consult (rails have some helpers)
Make that the two dates be equals, configuring config.active_record.default_timezone

How to return my current time zone in RoR?

When I use return the time that the record created, it show this :
2010-01-20 15:04:40 UTC
but I want the time in my specify time zone, for example, China. Is there any convenient method in RoR?
Configure your time zone in config/environment.rb to have Rails cast all timestamps to this time zone.
config.time_zone = 'Berlin'
As an alternative you can always use something like
Time.utc(2000).in_time_zone('Alaska')
See the documentation here.
Take a look at the TimeZone and TimeWithZone classes. They add time zone support. There's also been some additions to the Time and DateTime classes that also help deal with time zones. The documentation is given here: http://api.rubyonrails.org/classes/ActiveSupport/TimeWithZone.html.
There's also an excellent post here giving some extra details: http://ryandaigle.com/articles/2008/1/25/what-s-new-in-edge-rails-easier-timezones

Resources