Rails 3 to_s(:db) when database time is NOT UTC

about ActiveRecord’s default_timezone setting.
rails31>> Time.now.to_s(:db)
=> "2011-09-23 03:02:52"
rails31>> 1.second.ago.to_s(:db)
=> "2011-09-23 10:02:59"

WHAT??? I know that these are 2 different classes:

rails31>> Time.now.class
=> Time
rails31>> 1.second.ago.class
=> ActiveSupport::TimeWithZone

This is because ActiveSupport sets to UTC before doing the “to_s” which makes sense with the default Rails configuration where all dates are stored in the DB as UTC.

https://github.com/rails/rails/blob/master/activesupport/lib/active_support/time_with_zone.rb

# :db format outputs time in UTC; all others output time in local.
# Uses TimeWithZone's +strftime+, so %Z and %z work correctly.
def to_s(format = :default)
  if format == :db
    utc.to_s(format)
  elsif formatter = ::Time::DATE_FORMATS[format]
    formatter.respond_to?(:call) ? formatter.call(self).to_s : strftime(formatter)
  else
    "#{time.strftime("%Y-%m-%d %H:%M:%S")} #{formatted_offset(false, 'UTC')}" # mimicking Ruby 1.9 Time#to_s format
  end
end
alias_method :to_formatted_s, :to_s

I guess I understand this because I’m dealing with 2 different libraries: ActiveRecord vs ActiveSupport, but it sure threw me for a loop in some of my tests where I would sometimes create values with Time.now and other times with 1.day.ago and the time-zone conversion would fail the test.

My solution to this issue is to start using .to_s(:localdb) rather than .to_s(:db)

Time::DATE_FORMATS.merge!(:localdb=>"%Y-%m-%d %H:%M:%S")
或者设置 config.time_zone = 'Central Time (US & Canada)'
或者不要调用to_s 系统默认的就是需要的时间   通过设置 Time::DATE_FORMATS[:default] = "%Y-%m-%d %H:%M:%S"
 rails2:
ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS.update(:default => '%Y/%m/%d')
ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.update(:default => '%Y/%m/%d %H:%M')

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值