God messing with Date Operations
This is a weired think. Follow my steps:
With开发者_如何转开发out god, on console:
> d=Date.parse("2010-02-01")
=> Mon, 01 Feb 2010
> d+1.day
=> Tue, 02 Feb 2010
Perfect.
Then, I go to my Gemfile and add
gem 'god'
and run
bundle install
After that, on console again:
> d=Date.parse("2010-02-01")
=> Mon, 01 Feb 2010
> d+1.day
=> Sun, 23 Aug 2246
Do you know what could be happening?
Odd that this is happening in a console. I could have understood it in other scenarios, where 1.day is being used as an input in one place and extracted for use somewhere else, since 1.day is the Fixnum 86400, with some special metadata (#steps
) mixed into it.
Date
treats, for example, + 1
to mean "add one day". Rails adds some behaviour so that it understands the 1.day
thing (86400 "seconds", but with a step
of [1, :days]
) to actually mean + 1
instead of + 86400
. This is what you're losing:
ruby-1.9.2-p290 :171 > d = Date.parse("2010-02-01")
=> #<Date: 2010-02-01 (4910457/2,0,2299161)>
ruby-1.9.2-p290 :172 > d + 86400
=> #<Date: 2246-08-23 (5083257/2,0,2299161)>
ruby-1.9.2-p290 :173 >
So the value 1.day
is being interpreted as a Fixnum, rather than a Fixnum with ActiveSupport::Duration
.
irb(main):001:0> Date.parse("2010-02-01") + 1.day.to_i
=> Sun, 23 Aug 2246
So if you're using this 1.day
value in a context where it is not being immediately consumed, don't... use the Fixnum 1 instead ;)
"god" makes a mess of Dates. This is a problem with "god". A gem used with Rails should not alter Rails conventions. If Date.current - 4.days does one thing without "god", then it should do that with "god". Period.
According to the GitHub open issue, i tried this solution :
Gemfile
gem 'god', :require => false
Instead of :
gem 'god'
The everything seem to work again :
1.9.3p0 :001 > d = Date.parse('2012-04-16')
=> Mon, 16 Apr 2012
1.9.3p0 :002 > d + 1.day
=> Tue, 17 Apr 2012
精彩评论