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 7740105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:41:23+00:00 2026-06-01T08:41:23+00:00

I need to calculate the offset, in hours, of a given timezone from UTC

  • 0

I need to calculate the offset, in hours, of a given timezone from UTC in Ruby. This line of code had been working for me, or so I thought:

offset_in_hours = (TZInfo::Timezone.get(self.timezone).current_period.offset.utc_offset).to_f / 3600.0

But, it turns out that was returning to me the Standard Offset, not the DST offset. So for example, assume

self.timezone = "America/New_York"

If I run the above line, offset_in_hours = -5, not -4 as it should, given that the date today is April 1, 2012.

Can anyone advise me how to calculate offset_in_hours from UTC given a valid string TimeZone in Ruby that accounts for both standard time and daylight savings?

Thanks!


Update

Here is some output from IRB. Note that New York is 4 hours behind UTC, not 5, because of daylight savings:

>> require 'tzinfo'
=> false
>> timezone = "America/New_York"
=> "America/New_York"
>> offset_in_hours = TZInfo::Timezone.get(timezone).current_period.utc_offset / (60*60)
=> -5
>> 

This suggests that there is a bug in TZInfo or it is not dst-aware


Update 2

Per joelparkerhender’s comments, the bug in the above code is that I was using utc_offset, not utc_total_offset.

Thus, per my original question, the correct line of code is:

offset_in_hours = (TZInfo::Timezone.get(self.timezone).current_period.offset.utc_total_offset).to_f / 3600.0
  • 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-06-01T08:41:25+00:00Added an answer on June 1, 2026 at 8:41 am

    Yes, use TZInfo like this:

    require 'tzinfo'
    tz = TZInfo::Timezone.get('America/Los_Angeles')
    

    To get the current period:

    current = tz.current_period
    

    To find out if daylight savings time is active:

    current.dst?
    #=> true
    

    To get the base offset of the timezone from UTC in seconds:

    current.utc_offset
    #=> -28800 which is -8 hours; this does NOT include daylight savings
    

    To get the daylight savings offset from standard time:

    current.std_offset
    #=> 3600 which is 1 hour; this is because right now we're in daylight savings
    

    To get the total offset from UTC:

    current.utc_total_offset
    #=> -25200 which is -7 hours
    

    The total offset from UTC is equal to utc_offset + std_offset.

    This is the offset from the local time where daylight savings is in effect, in seconds.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to calculate the total of an invoice. This invoice is created with
I need to calculate the distance (in meters and miles) between two coordinates given
I need to calculate date (year, month, day) which is (for example) 18 working
I need to calculate the within and between run variances from some data as
I need to calculate the quantity of nights (stay at a hotel) from the
I am calculating a padding offset from my css file with Jquery. I need
i need calculate normal of 6 faces from cube. The cube is moved with
For my tile-based game, I need to calculate direction based on a given point
I need to calculate the time till Christmas in milliseconds. I'm using joda time
I need to calculate (in R) the number of positive and negative runs in

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.