配置Rails控制台的默认日期时间格式。

huangapple go评论74阅读模式
英文:

Configure Rails console's default date time format

问题

默认情况下,我们的Rails应用程序显示日期时间如下:

> Sun, 08 Jan 2023 16:52:46.610977000 UTC +00:00

这也在Rails控制台中显示,当检查时间对象时。

是否可以仅为Rails控制台设置不同的日期时间格式?

英文:

By default, our Rails application displays date time like this

> Sun, 08 Jan 2023 16:52:46.610977000 UTC +00:00

This is also shown in Rails console, when a time object is inspected.

Is it possible to set a different date time format, just for the Rails console alone?

答案1

得分: 2

以下是代码部分的翻译:

# File activesupport/lib/active_support/time_with_zone.rb, line 150
def inspect
  "#{time.strftime('%a, %d %b %Y %H:%M:%S.%9N')} #{zone} #{formatted_offset}"
end
# config/application.rb

module YourApp
  class Application < Rails::Application

    console do
      # this block is called only when running console

      class ActiveSupport::TimeWithZone
        def inspect
          time.strftime('%Y-%m-%d %H:%M:%S')
        end
      end
    end

  end
end
$ bin/rails console
Loading development environment (Rails 7.0.6)
irb(main):001:0> Time.current
=> 2023-07-20 12:36:10
$ bin/rails runner 'p Time.current'
Thu, 20 Jul 2023 12:38:47.934270000 CEST +02:00

虽然 Rails 不会更改 Time#inspect,但它会修改 Ruby 内置类 DateDateTimeinspect 方法,以使它们更易读,参见 core_ext/date/conversions.rbcore_ext/date_time/conversions.rb。如果需要,您也可以通过修补这些类来为控制台使用提供自己的 inspect 实现。

但请记住,修补类和更改现有行为始终存在破坏某些东西的风险。

英文:

That format is the output from TimeWithZone#inspect, which is hard-coded:

# File activesupport/lib/active_support/time_with_zone.rb, line 150
def inspect
  "#{time.strftime('%a, %d %b %Y %H:%M:%S.%9N')} #{zone} #{formatted_offset}"
end

Rails however provides a console block to register code which gets only executed when running the Rails console (casually mentioned in the docs for config.console). You could use it to provide your own inspect method while running console, e.g.:

# config/application.rb

module YourApp
  class Application < Rails::Application

    console do
      # this block is called only when running console

      class ActiveSupport::TimeWithZone
        def inspect
          time.strftime('%Y-%m-%d %H:%M:%S')
        end
      end
    end

  end
end
$ bin/rails console
Loading development environment (Rails 7.0.6)
irb(main):001:0> Time.current
=> 2023-07-20 12:36:10

Other rails execution contexts are not affected:

$ bin/rails runner 'p Time.current'
Thu, 20 Jul 2023 12:38:47.934270000 CEST +02:00

Note that Time.current is a Rails method which (usually) returns an instance of ActiveSupport::TimeWithZone, not an instance of Ruby's built-in Time class.

Although Rails doesn't change Time#inspect, it alters the inspect methods for Ruby's built-in classes Date and DateTime to make them more readable, see core_ext/date/conversions.rb and core_ext/date_time/conversions.rb. If needed, you can provide your own inspect implementation for console use by patching those classes as well.

But keep in mind that patching classes and altering existing behavior always comes at the risk of breaking something.

huangapple
  • 本文由 发表于 2023年7月20日 17:42:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76728595.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定