Ruby/Rails - Change the Timezone of a Time, Without Changing the Value

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.

Ruby: How modify Timezone without change time

take this date in variable say currentDateTime:

then use following:

currentDateTime.change(:offset => "+0000")

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

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.

Change timezone without changing the value of a DateTime object

You must set timezone when creating DateTime object, and not when it is already created; then change DateTime object to UTC timezone and save it to you db:

# create DateTime based on user timezone
$dt = new DateTime('2014-09-18 17:00', new DateTimezone('Australia/Sydney'));
# change time to UTC timezone
$dt->setTimezone(new DateTimezone('UTC'));

Rails change default timezone

All of a sudden I've localized the problem.
I run Win7 and my WEBrick server is showing the correct time, but the project itself shows a wrong one. In the same time, my production server is working correctly, so, obviously, the problem is with the OS.
UPD: The problem is fixed. I should have restarted the WEBrick server after changing the application.rb file.

How to change default timezone for Active Record in Rails?

adding following to application.rb works

 config.time_zone = 'Eastern Time (US & Canada)'
config.active_record.default_timezone = :local # Or :utc


Related Topics



Leave a reply



Submit