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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:11:10+00:00 2026-06-09T16:11:10+00:00

I beat my head against this for a day and a half before it

  • 0

I beat my head against this for a day and a half before it finally all came together, partly because I was fundamentally misunderstanding some things because of my assumptions of how things should work, and so the results I was getting seemed so completely irrational and confusing that I was starting to think things must be horribly broken in the libraries. They weren’t.

This thread is to share my question, and what worked, partly because it seems to me that a lot of times a datetime record should be tied to the localtime of its location, and a lot of times when applications behave strangely with respect to timezone changes I think it’s because they don’t do that. So here’s how to do that.

The easy part was using the TimeZone gem to get the location’s timezone (I already have the latitude/longitude from using the GMaps4Rails gem), as described here: Ruby gem for finding timezone of location

Initially, I hoped each datetime could have an intrinsic and persistent time zone, even after being saved and retrieved from the DB, since we only care about the local time zone for each event. For the unusual and rare case where the datetime was needed in other-than-local-zone, I could convert. But I guess that doesn’t exist since MySQL seem to have no concept of timezone with its datetime, and just stores everything as a plain ol’ date and time.

So, that’s fine — the DB stores in UTC and I must do the conversions each direction.

How? (See below.)

  • 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-09T16:11:11+00:00Added an answer on June 9, 2026 at 4:11 pm

    User enters a datetime (year, month, day, hour, min, sec) in the appropriate local time for the event location. To save this as the correct UTC time in your DB, and retrieve later retrieve it and use it with its correct timezone, here’s the recipe:

    1. You must first set Time.zone = timezone — where timezone is a named timezone string, like "America/Toronto" (GeoKit/TimeZone style) or "Eastern Time (US & Canada)" (Rails 3 style):

      1.9.3p200 :001 > Time.zone = "America/Toronto"
       => "America/Toronto"
      

      Note that it is nontrivial to map between the GeoKit/TimeZone style and the Rails 3 style. There is some support for that, but many of the mappings that GeoKit returns are not in the Rails mappings, so I’m sticking with the GeoKit/TimeZone names because they seem to work just fine, and I don’t need user-selectable timezones because I’m taking care of all of that automagically for the user using the locations.

    2. Now you can parse and save your datetime variable using ActiveSupport::TimeWithZone, and it will be encoded properly using the Time.zone parameter:

      1.9.3p200 :002 > t = Time.zone.local( 2012, 8, 1, 12, 0, 0 )
       => Wed, 01 Aug 2012 12:00:00 EDT -04:00 
      

      i.e. In my case, for my Event model with instance event:

      event.start = Time.zone.local( year, month, day, hour, min, sec )
      event.save
      

      When the data is saved to the DB, it is in UTC: 2012-08-01 16:00:00.000000

      (Simulated below with call to t.utc)

      1.9.3p200 :003 > t = t.utc
       => 2012-08-01 16:00:00 UTC
      
      1.9.3p200 :004 > t.zone
       => "UTC" 
      
    3. Now, when you read it back from the DB, you need to again use the TimeWithZone support to tell the DateTime what timezone it is in.

      Note that the in_time_zone function does not change the underlying datetime variable’s timezone:

      1.9.3p200 :005 > t.in_time_zone( "America/Toronto" )
       => Wed, 01 Aug 2012 12:00:00 EDT -04:00 
      
      1.9.3p200 :006 > t
       => 2012-08-01 16:00:00 UTC 
      
      1.9.3p200 :007 > t.zone
       => "UTC" 
      

      so you really need to assign the function output back to your datetime variable:

      1.9.3p200 :008 > t = t.in_time_zone( "America/Toronto" )
       => Wed, 01 Aug 2012 12:00:00 EDT -04:00 
      
      1.9.3p200 :009 > t
       => Wed, 01 Aug 2012 12:00:00 EDT -04:00 
      
      1.9.3p200 :010 > t.zone
       => "EDT" 
      
    4. Finally, a warning — do not use t.time! It’s output is completely useless and irrational, with the time and it’s timezone out of sync:

      1.9.3p200 :011 > t.time
       => 2012-08-01 12:00:00 UTC 
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone explain to me this code because it seems to beat my logic?
May be I missed something, but I've already beat my head with this one.
I have run into a problem I have beat my head against the wall.
I am new and I know I will get beat down for this Question
I have been fighting this issue for days now and about to beat my
While I hate to beat a horse to death on this subject (I've read
I am beating my head against a wall trying to figure out why the
I'm really struggling to wrap my head around this: I have a UserModel and
This one has me beat; I have a WPF window with two (important for
Consider the following code snippet: $beat = date('B'); // 1 beat = 86.4 seconds,

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.