8394

rails' utc_to_local and daylight savings time

Question:

> e = Event.first > e.registration_start_utc #registration_start_utc is a datetime column => Sat, 23 Oct 2010 06:38:00 UTC +00:00 > e.registration_start_utc.utc? => true > ActiveSupport::TimeZone.find_tzinfo("America/New_York").utc_to_local(e.registration_start_utc) => Sat, 23 Oct 2010 02:38:00 UTC +00:00

2 questions about this:

1) Why is that last output showing "UTC" -- the hour got converted (6 => 2) but it still says UTC. Why not EST/EDT?

2) What happens after daylight savings time switches over and the offset for New York moves from -4 to -5? The value in the DB doesn't change so my only conclusion is that my app will start showing "1:38" everywhere instead of the correct 2:38?

I'm mostly concerned with #2 here. #1 is more of a curiosity.

Thanks!

Answer1:

2) utc_to_local uses the date to determine which offset is correct, so the output will always be the same for a given date.

You can test for that like this:

t = Time.utc(2011,3, 14, 12) # => 2011-03-14 12:00:00 UTC t2 = Time.utc(2011,3, 11, 12) # => 2011-03-11 12:00:00 UTC ActiveSupport::TimeZone.find_tzinfo("America/New_York").utc_to_local(t) # => 2011-03-14 08:00:00 UTC ActiveSupport::TimeZone.find_tzinfo("America/New_York").utc_to_local(t2) # => 2011-03-14 07:00:00 UTC

1) It doesn't seem right to me either. My guess is that they are interested only in the actual value of the hour, minutes, etc... and disregard the timezone.

In any case, you might be better off using:

e.registration_start_utc.in_time_zone("Eastern Time (US & Canada)")

See also <a href="https://stackoverflow.com/questions/5300493/convert-utc-to-local-time-in-rails-3" rel="nofollow">this question</a> I just asked...

Recommend

  • How to concatenate variables?
  • How to extract source from Google search result “20-pack” entry?
  • send multiple objects via ajax (using angular)
  • Remove non-U.S. state data
  • How to embed a picture (jpg/png/etc,) into html and email it out with mail
  • Error in dev.off() : cannot shut down device 1 (the null device)
  • mysql partitioning
  • windows store certification kit - c++ library validation issues
  • Morphia projection behavior for instance variable with default value
  • better way to communicate between ad hoc wifi windows mobile devices
  • Retrieve RSS feed and display it in text view
  • Switch to a remote branch getting detached head [duplicate]
  • Sign Key HMAC SHA1 with Javascript
  • CSS Border DIV as a Shape
  • Error: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList
  • Timer once a minute on the minute
  • php regex remove digits
  • PostgreSQL 9.1 timezones
  • AngularJS - ngBind and Bootstrap Switch
  • python struct.pack(): pack multiple datas in a list or a tuple
  • Ransack search string arrays stored in db
  • 1º Day of Daylight Saving Time Java and JS showing a different behavior
  • Is a .txt file created in VB different than one I'd randomly create?
  • Does anyone have a Categorized XML Corpus Reader for NLTK?
  • css font-size and line-height not matching the baseline
  • Certain Arabic text gets incorrectly shown while other Arabic text gets showed normally?
  • How to define an array of floats in Shader properties?
  • A new chart every sheet
  • How to implement Deep Linking in Roku SG application?
  • Put value at centre of bins for histogram
  • Default parameter as generic type
  • Sequential (transactional) API calls in angular 4 with state management
  • ActiveRecord query for a count of new users by day
  • How to add a column to a Pandas dataframe made of arrays of the n-preceding values of another column
  • Javascript Callbacks with Object constructor
  • Function pointer “assignment from incompatible pointer type” only when using vararg ellipsis
  • Matrix multiplication with MKL
  • python draw pie shapes with colour filled
  • How to Embed XSL into XML
  • Converting MP3 duration time