Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3985892
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:56:45+00:00 2026-05-20T05:56:45+00:00

In Ruby 1.8.7, how to set the time zone of a time? In the

  • 0

In Ruby 1.8.7, how to set the time zone of a time?

In the following examples, my system time zone is PST (-8:00 hours from UTC)

Given a time (21 Feb 2011, 20:45), presume that the time is in EST:

#this interprets the time as system time zone, i.e. PST
Time.local(2011,02,21,20,45) 
  #=> Mon Feb 21 20:45:00 -0800 2011

#this **converts** the time into EST, which is wrong!
Time.local(2011,02,21,20,45).in_time_zone "Eastern Time (US & Canada)" 
  #=> Mon, 21 Feb 2011 23:45:00 EST -05:00

But, the output I want is:
Mon Feb 21 20:45:00 -0500 2011 (Note the -0500 (EST) as opposed to -0800 (PST) and the hour is same, i.e. 20, not 23)

UPDATE (see the better version of this below)

I managed to get this to work, but I don’t like it:

DateTime.new(2011,02,21,20,45).change :offset => -(300.0 / 1440.0)
  # => Mon, 21 Feb 2011 20:45:00 +0500

Where
  300 = 5 hrs x 60 minutes
  1440 = number of minutes in a day

or the "right" way:

DateTime.civil(2011,02,21,20,45,0,Rational(-5, 24))

Question: Now, is there a way to determine the accurate(i.e. catering for daylight saving time etc) UTC offset from Time.zone so that I can pass it to the change method?

Reference: DateTime::change method

UPDATE (better version)

Thanks to @ctcherry for all the help!

Determine the accurate time zone info from Time.zone:

DateTime.civil(2011,02,21,20,45,0,Rational((Time.zone.tzinfo.current_period.utc_offset / 3600), 24))
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T05:56:46+00:00Added an answer on May 20, 2026 at 5:56 am

    In ruby 1.8.7 it doesn’t appear to be very easy to do what are asking for according to the documentation:

    http://www.ruby-doc.org/core-1.8.7/classes/Time.html

    However in 1.9 it looks a lot easier by passing the timezone offset to the localtime() method on a Time object:

    http://www.ruby-doc.org/core/classes/Time.html#M000346

    UPDATE

    The offset for Time.zone is easy since its an object on its own: (This is in a Rails console)

    ruby-1.8.7-p248 :001 > Time.zone
     => #<ActiveSupport::TimeZone:0x103150190 @current_period=nil, @name="Central Time (US & Canada)", @tzinfo=#<TZInfo::TimezoneProxy: America/Chicago>, @utc_offset=nil> 
    ruby-1.8.7-p248 :002 > Time.zone.utc_offset
     => -21600 
    ruby-1.8.7-p248 :003 > Time.zone.formatted_offset
     => "-06:00" 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just set up ruby in Aptana studio and am getting the following
Is there a way to iterate over a Time range in Ruby, and set
Using Sinatra in Ruby you can set the server's settings by doing: set :myvariable,
With Ruby, how can I set a variable based on a condition? Something like:
The default in Ruby on Rails is to have this set to false (in
I am trying to set up a Sinatra based Ruby app to connect to
I've set up a new Rails 2.3.2 app and added the Basecamp API ruby
I'm trying to set up Factory Girl with Test::Unit and Shoulda in Ruby on
In my ruby(1.9.3) on rails (3.1) app I'm trying to set some conditional behavior
Hi I am trying to set up categories on my e-commerce website using Ruby

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.