Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

#localtime fails for TimeWithZone-wrapped DateTimes

#5344

Simple test case:

DateTime.now.in_time_zone.localtime
# => NoMethodError: undefined method `getlocal' for Mon, 09 Aug 2010 23:59:31 +0000:DateTime
         from /Users/jfire/.rvm/gems/jruby-1.5.1/gems/activesupport-3.0.0.rc/lib/active_support/time_with_zone.rb:75:in `localtime'
         from (irb):25

Reported by John Firebaugh · August 10th, 2010 @ 01:13 AM

State: committed
Milestone: 3.0.2
Assigned to: José Valim José Valim
Importance: Low

Activity

  1. chrispanda
    chrispanda

    I am also seeing this error with ruby 1.9.2
    DateTime.now.in_time_zone.localtime
    NoMethodError: undefined method getlocal' for Mon, 30 Aug 2010 13:28:15 +0000:DateTime<br/>

    from /home/chris/.rvm/gems/ruby-1.9.2-rc2/gems/activesupport-3.0.0.rc/lib/active_support/time_with_zone.rb:75:in `localtime'
    from (irb):1
    

    August 30th, 2010 @ 06:21 PM

  2. MikZ
    MikZ
    • Tag set to datetime, method_missing, timewithzone, timezone

    Same problem here.

    I'm unable to save records with time zone.

    ruby-1.9.2-p0 > new.class
    ActiveSupport::TimeWithZone < Object
    ruby-1.9.2-p0 > new
    Thu, 16 Sep 2010 18:00:00 CEST +02:00
    ruby-1.9.2-p0 > new.localtime
    NoMethodError: undefined method `getlocal' for Thu, 16 Sep 2010 16:00:00 +0000:DateTime
        from /Users/mikz/.rvm/gems/ruby-1.9.2-p0@avion-is/gems/activesupport-3.0.0/lib/active_support/time_with_zone.rb:75:in `localtime'
        from (irb):14
    

    September 10th, 2010 @ 03:13 PM

  3. MikZ
    MikZ

    Meanwhile you can use this workaround:

    class DateTime
      def getlocal

    self.to_time.getlocal.to_datetime
    
    
    
    
    end end

    September 10th, 2010 @ 03:28 PM

  4. Álvaro Bautista
    Álvaro Bautista
    • Tag changed from datetime, method_missing, timewithzone, timezone to datetime, method_missing, patch, timewithzone, timezone

    The ActiveSupport::TimeWithZone#localtime method is not covered by any test. It fails when calling getlocal on a DateTime instance. A quick (and simple) workaround would be to convert it into a Time object before calling getlocal, since the documentation says the returned value is a Time object (the same as calling Time.local()).

    Here is a patch adding both tests (with and without DateTime) and a slight (maybe a little ugly?) modification to the localtime method.

    It does not break any tests (under ruby 1.8.7 p266 and using the sqlite3 and mysql adapters for activerecord tests) so, do you think it would be a valid fix?

    September 11th, 2010 @ 08:16 PM

  5. José Valim
    José Valim
    • State changed from new to open
    • Milestone set to 3.0.2
    • Assigned user set to José Valim
    • Importance changed from to Low

    November 8th, 2010 @ 01:24 PM

  6. Repository
    Repository
    • State changed from open to committed

    November 11th, 2010 @ 03:33 AM

  7. Repository