Converting Timezone of object in database - ruby-on-rails

I am trying to change a UTC time that is in my Postgresql database to an Eastern Time to send in a mailer. I have been going crazy trying to convert these and just can't seem to get it right.
Time is was actually created (EST): 1:33pm
Calls in console:
item.created_at => "2015-09-07 17:33:16"
item.to_time("Eastern Time (US & Canada)") => 2015-09-07 17:33:16 +0000
item.in_time_zone("Eastern Time (US & Canada)").to_time.strftime("%l:%M%p") => 9:33PM
It seems like there is some type of conversion going on (going from 17:33 to 21:33) but it is 'backwards'? It should go down 4 hours not up 4 hours.
Any help is greatly appreciated!
Thank you!

You only need this:
item.created_at.in_time_zone("Eastern Time (US & Canada)")
Here's a contrived example:
[2] pry(main)> time = Time.now.utc
=> 2015-09-07 17:51:17 UTC
[3] pry(main)> time.in_time_zone("Eastern Time (US & Canada)")
=> Mon, 07 Sep 2015 13:51:17 EDT -04:00

Not sure if this is what you are looking for but there's an option in application.rb to set your timezone. Kindly check
config.time_zone = 'Pacific Time (US & Canada)'

I apologize, this was my mistake.
Since the information I was getting was from a query, I for some reason had to parse the created_at to a DateTime object before I was able to use the in_time_zone method.
So, in the end,
DateTime.parse(time).in_time_zone("Eastern Time (US & Canada)")
worked just as it should have.

Related

Rails: Converting created_at to PST

I want to convert the created_at to this format January 12, 2:00 PM PST.
The first step I tried was to convert the created_at field to Pacific Standard Time (PST).
However, I'm stuck - I can't even get past this step.
I've tried these, but neither worked:
Time.parse(self.created_at).in_time_zone('Pacific Time (US & Canada)')
time = Time.parse(self.created_at)
time.in_time_zone('Pacific Time (US & Canada)')
I receive no implicit conversion of ActiveSupport::TimeWithZone into String when I do this.
I based it on these questions:
How do you convert the following time from UTC to EST in Ruby (without Rails)?
How to convert time from UTC to PST in rails
Since it is already a datetime, you should be able to just do this to convert it:
self.created_at.in_time_zone('Pacific Time (US & Canada)')
Don't forget to save the new time zone to the record in the database.
Time.parse() is meant to convert strings into datetime(https://apidock.com/ruby/v2_5_5/Time/parse/class).

How to set the timezone to EST throughout the rails app?

I want to use EST throughout the app. I have set the
config.time_zone = 'Eastern Time (US & Canada)'
config.active_record.default_timezone = 'Eastern Time (US & Canada)'
what I want is, I have to get all the time I have used like "DateTime.now" and it now returns time in my local timezone but I want it to return date time in EST. Also I have date time range picker and I get the start date and end date and parse the string to time using
(params[:start_date]).to_time
which also returns the date time in local zone and instead i want all those to return in EST. How can I do this? Any help is appreciated.
Thanks in advance.
DateTime.now and Time.now doesn't respect Time.zone and will always use the server time. If the machine you're using is set to a different timezone, these 2 will use that timezone.
to_time on the other hand defaults to local timezone as mentioned in apidock (although just tried this out and I get utc by default). To get these times in the timezone set, append in_time_zone
DateTime.now.in_time_zone
Time.now.in_time_zone
string.to_time.in_time_zone
There are some shortcuts for these though. The following code will return the timestamps in the current timezone
Time.zone.now
Time.zone.parse(string)
Normally, in database date-time would be saved in UTC format unless you have selected a specific TimeZone for Active Record and it would be easier if you have to meddle with multiple TimeZones. But, while you access the values you will get the values in you selected TimeZone.
config.time_zone = 'Eastern Time (US & Canada)
Now, suppose you have a datetime value in UTC from table as
datetime1 = "2014-08-25 10:25:57 +0000"
You can convert it to the selected timezone 'Eastern Time (US & Canada)' as below,
Time.zone.parse(datetime1) => Mon, 25 Aug 2014 06:25:57 EDT -04:00
Then, if you need it in a proper format, use method strftime.
Time.zone.parse("2014-08-25 10:25:57 +0000").strftime("%d-%m-%Y %H:%M:%S") => "25-08-2014 06:25:57"
Hoep it helps :)

How to preserve timezone when using Time.to_s(:db)?

I'm trying to convert Time object with PST timezone.
zone = ActiveSupport::TimeZone.new("Pacific Time (US & Canada)")
now = Time.now.in_time_zone(zone)
when I output now, it's correct. when I do Time.now.in_time_zone(zone).to_s(:db), it outputs GMT (original date).
How do I fix it?
Update: Looks like..the following code works
zone = ActiveSupport::TimeZone.new("Pacific Time (US & Canada)")
now = Time.now.in_time_zone(zone).strftime("%Y-%m-%d %H:%M:%S")
Database dates are usually stored in utc time format..
ActiveSupport is returning UTC, not GMT on to_s(:db),
see http://apidock.com/rails/ActiveSupport/TimeWithZone/to_s
EDIT: Maybe this helps you?
http://chris.finne.us/2011/09/23/rails-3-tosdb-when-database-time-is-not-utc/

How to get correct time zone offset in Rails 2.3?

Using Rails 2.3, when I run ActiveSupport::TimeZone.us_zones, I get the following:
....
* UTC -08:00 *
Pacific Time (US & Canada)
....
Now, since DST is in effect right now, the UTC offset should have been -07:00 !
Anyone know how to get the correct Time zone offsets in Rails 2.3 ?
You can use the UTC offset of the current time in that time zone:
zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
zone.now.utc_offset
=> -14400
zone.utc_offset
=> -18000

Why doesn't `config.time_zone` seem to do anything?

In application.rb, it says:
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.
But setting config.time_zone = 'Central Time (US & Canada)' or config.time_zone = 'Eastern Time (US & Canada)' has no effect - the created_at field in a model is stil being saved in UTC.
According to this railsforum answer:
config.time_zone just lets rails know
that your server is set to this
timezone so when it writes dates to
the database it can properly convert
it to UTC.
If that is true, then why is it that when my system time is Pacific Time (US & Canada) and config.time_zone = 'Central Time (US & Canada)' or config.time_zone = 'Eastern Time (US & Canada)', that the created_at time is the correct UTC? Should it not be incorrect?!
Because, if the PST time is 8 PM, then EST is 11 PM and UTC is 4 AM.
Presuming that Rails does Time.now, that would be 8 PM. And we told Rails that the server is in EST. So, 8 PM would be EST time as far as Rails is concerned and the UTC would then be 5 AM UTC, which would be incorrect (because the actual time is 8 PM PST/11 PM EST, which is 4 AM UTC)
What's going on here?
Here's a list of concepts and things that may help you:
config.time_zone doesn't set "Server Time", that is controlled by your operating system usually.
Rails always stores your dates in UTC in the database (unless you change a different setting).
Time.now returns the localtime for your computer in your timezone and it also includes the local timezone offset from your operating system, which means Ruby and therefore Rails knows how to convert localtime into UTC. You can try this by using irb directly, so no Rails libraries are loaded:
ctcherry$ irb
>> Time.now
=> Mon Feb 21 20:53:14 -0800 2011
>>
If config.time_zone, or Time.zone is set, to lets say EST, Rails expects that if you set a datetime attribute that you mean for that time and date to be in specified timezone, in this case EST. This is why you set Time.zone equal to your end users timezone, so they can use their local time and dates, and you can pass them directly into your ActiveRecord models and Rails can convert it to UTC for storage in the database.
Does that help at all?
You need to use in_time_zone (i.e. Time.now.in_time_zone) to get some something other than UTC.

Resources