在Ruby项目中需要ActiveSupport :: TimeWithZone(在Rails之外)

时间:2018-08-12 21:53:05

标签: ruby-on-rails ruby

我想在Rails项目之外利用ActiveSupport::TimeWithZone的功能。如您所知,Time.now有一些严重的局限性:

1)根据ENV ['TZ']系统环境变量确定时间,如果未定义,则根据/ etc / localtime中的时区确定时间。

2)它简单地表示自1970年Unix时间以来的秒数,它表示为基于/ etc / localtime中时区的整数。但这并不能为您提供在不同时区工作的灵活性。

现在,在Rails项目中,您可以使用所需的时区进行配置,并且时间还可以通过ActiveSupport :: TimeWithZone打包,它为您提供了使用时区的实用方法:

ActiveSupport::TimeZone.all.map(&:name)
Time.use_zone(current_user.time_zone), &block)
Time.current
Time.zone.now
5.days.ago
2.weeks.ago
1.hour.ago
Time.current.to_date

我认为我所需要的只是需要ActiveSupport。所以我启动了irb:

2.1.2 :006 >   require 'active_support'
 => true 
2.1.2 :007 > Time.current
NoMethodError: undefined method `current' for Time:Class
    from (irb):7
    from /Users/dviglione/.rvm/rubies/ruby-2.1.2/bin/irb:11:in `<main>'
2.1.2 :008 > ActiveSupport::TimeZone
NameError: uninitialized constant ActiveSupport::TimeZone

我需要ActiveSupport,但TimeWithZone仍然不可用。我在做什么错了?

0 个答案:

没有答案