Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

undefined method `zone` for Time:Class after requiring active_support/time_with_zone

I'm on Ruby 2.2.1 and have active_support 4.2 installed so I want to use

Time.zone.parse('2007-02-10 15:30:45')

as described here: http://api.rubyonrails.org/classes/ActiveSupport/TimeWithZone.html

But even after requiring active_support and active_support/time_with_zone, I doesn't seem like Time.zone still doesn't work. Observe:

2.2.1 :002 >   require "active_support"
 => true
2.2.1 :003 > Time.zone
NoMethodError: undefined method `zone' for Time:Class
2.2.1 :004 > require "active_support/time_with_zone"
 => true
2.2.1 :005 > Time.zone
NoMethodError: undefined method `zone' for Time:Class

What's going on?

like image 243
User314159 Avatar asked Jan 28 '16 09:01

User314159


2 Answers

The zone class method for the Time class is actually in active_support/core_ext/time/zones. You can require that class if you really want to use Time.zone but a better approach might to require active_support/all

Suggested Solution

require "active_support/all"

If you want to check out the source code fort for active_support look at the github repo

like image 62
Tyler Ferraro Avatar answered Nov 20 '22 12:11

Tyler Ferraro


active_support/time_with_zone provides the ActiveSupport::TimeWithZone class, but it doesn't extend the core Time class.

You can require active_support/time instead:

require 'active_support'
require 'active_support/time'

Time.zone = 'Eastern Time (US & Canada)' #=> "Eastern Time (US & Canada)"
Time.zone.parse('2007-02-10 15:30:45')   #=> Sat, 10 Feb 2007 15:30:45 EST -05:00
like image 8
Stefan Avatar answered Nov 20 '22 11:11

Stefan