How do I change the zone offset for a time in Ruby on Rails?
I'm using Rails 2.0 before they added the code that makes weppos solution work. Here's what I did
# Silly hack, because sometimes the input_date is in the wrong timezone
temp = input_date.to_time.to_a
temp[8] = true
temp[9] = "Eastern Daylight Time"
input_date = Time.local(*temp)
I break the time down into a 10 element array, change the timezone and then convert the array back into a time.
Set time zone offset in Ruby
Change the time zone on your OS; Ruby will pick up the change.
Ruby / Rails - Change the timezone of a Time, without changing the value
Sounds like you want something along the lines of
ActiveSupport::TimeZone.new('America/New_York').local_to_utc(t)
This says convert this local time (using the zone) to utc. If you have Time.zone
set then you can of course to
Time.zone.local_to_utc(t)
This won't use the timezone attached to t - it assumes that it's local to the time zone you are converting from.
One edge case to guard against here is DST transitions: the local time you specify may not exist or may be ambiguous.
How do you get the local timezone offset in +hh:mm format?
strftime
with %z
(:
means hour and minute offset from UTC with a colon):
Time.current.strftime("%:z")
Change Time zone in pure ruby (not rails)
You can use the Time
extensions from Active Support outside of Rails:
require 'active_support/core_ext/time'
t = Time.now
#=> 2014-08-15 15:38:56 +0200
t.in_time_zone('Pacific Time (US & Canada)')
#=> Fri, 15 Aug 2014 06:38:56 PDT -07:00
Now you can do
class Time
def to_pst
in_time_zone('Pacific Time (US & Canada)')
end
end
t = Time.now
#=> 2014-08-15 15:42:39 +0200
t.to_i
#=> 1408110159
t.to_pst
#=> Fri, 15 Aug 2014 06:42:39 PDT -07:00
t.to_pst.to_i
#=> 1408110159
# timestamp does not change!
Additionally you might also want the time extensions on Numeric
and Date
:
require 'active_support/core_ext/date'
require 'active_support/core_ext/numeric/time'
2.days.from_now
#=> 2014-08-17 15:42:39 +0200
Time zone offset select
There are no such built-in Rails helpers, but it's easy to implement with options_for_select
options_for_select(ActiveSupport::TimeZone.us_zones.map {|zone| zone.utc_offset / 3600}.uniq)
Related Topics
Routing Error - Uninitialized Constant
Retrieve Contents of Url as String
Ruby: No Such File or Directory @ Rb_Sysopen - Testfile (Errno::Enoent)
Don't Have Jekyll-Paginate or One of Its Dependencies Installed
How to Force One Field in Ruby's CSV Output to Be Wrapped with Double-Quotes
How to Find a Model's Relationships
Using the Value of a Variable as Another Variables Name in Ruby
Strategies for Overriding Database.Yml
How to Include a Yaml File Inside a Yaml File in Ruby
Rake Assets:Precompile Gets Killed When There Is a Console Session Open in Production
Circular Dependency Detected While Autoloading Constant When Loading Constant
How to Specify "Http Request Header" in Openuri
How to Replace the Last Occurrence of a Substring in Ruby
Mongoid: Find Through Array of Ids
Printing to Screen in a Rake Task
Using Ruby and Net-Ssh, How to Authenticate Using the Key_Data Parameter with Net::Ssh.Start