| Commit message (Collapse) | Author | Age | Files | Lines |
|
|
|
| |
This basically reverts 8da30ad6be34339124ba4cb4e36aea260dda12bc
|
| |
|
|
|
|
|
|
|
|
|
|
|
| |
Rails 5.1 introduce an `ActiveSupport::Duration::Scalar` class as
a wrapper around a numeric value as a way of ensuring a duration
was the outcome of an expression. However the implementation was
missing support for modulo operations. This commit adds support
for those operations and should result in a duration being
returned from expressions involving them.
Fixes #29603 and #29743.
|
|
|
|
|
|
|
|
|
| |
PR #29163 introduced a change in behavior when a duration was
the denominator in a calculation - this was incorrect as dividing
by a duration should always return a `Numeric`. The behavior of
previous versions of Rails has been restored.
Fixes #29592.
|
| |
|
| |
|
| |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
Previously calculations where the scalar is first would be converted
to a duration of seconds but this causes issues with dates being
converted to times, e.g:
Time.zone = "Beijing" # => Asia/Shanghai
date = Date.civil(2017, 5, 20) # => Mon, 20 May 2017
2 * 1.day # => 172800 seconds
date + 2 * 1.day # => Mon, 22 May 2017 00:00:00 CST +08:00
Now the `ActiveSupport::Duration::Scalar` calculation methods will try
to maintain the part structure of the duration where possible, e.g:
Time.zone = "Beijing" # => Asia/Shanghai
date = Date.civil(2017, 5, 20) # => Mon, 20 May 2017
2 * 1.day # => 2 days
date + 2 * 1.day # => Mon, 22 May 2017
Fixes #29160, #28970.
|
|
|
|
|
|
|
| |
Follow up of 03d3f036.
Some of `respond_to?` were replaced to `respond_to_missing?` in 03d3f036.
But the visibility is still public. It should be private.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
In #28204 we deprecated implicit conversion of durations to a
numeric which represented the number of seconds in the duration
because of unwanted side effects with calculations on durations
and dates. This unfortunately had the side effect of forcing a
explicit cast when configuring third-party libraries like
expiration in Redis, e.g:
redis.expire("foo", 5.minutes)
To work around this we've removed the deprecation and added a
private class that wraps the numeric and can perform calculation
involving durations and ensure that they remain a duration
irrespective of the order of operations.
|
| |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
Currently `ActiveSupport::Duration` implicitly converts to a seconds
value when used in a calculation except for the explicit examples of
addition and subtraction where the duration is the receiver, e.g:
>> 2 * 1.day
=> 172800
This results in lots of confusion especially when using durations
with dates because adding/subtracting a value from a date treats
integers as a day and not a second, e.g:
>> Date.today
=> Wed, 01 Mar 2017
>> Date.today + 2 * 1.day
=> Mon, 10 Apr 2490
To fix this we're implementing `coerce` so that we can provide a
deprecation warning with the intent of removing the implicit coercion
in Rails 5.2, e.g:
>> 2 * 1.day
DEPRECATION WARNING: Implicit coercion of ActiveSupport::Duration
to a Numeric is deprecated and will raise a TypeError in Rails 5.2.
=> 172800
In Rails 5.2 it will raise `TypeError`, e.g:
>> 2 * 1.day
TypeError: ActiveSupport::Duration can't be coerced into Integer
This is the same behavior as with other types in Ruby, e.g:
>> 2 * "foo"
TypeError: String can't be coerced into Integer
>> "foo" * 2
=> "foofoo"
As part of this deprecation add `*` and `/` methods to `AS::Duration`
so that calculations that keep the duration as the receiver work
correctly whether the final receiver is a `Date` or `Time`, e.g:
>> Date.today
=> Wed, 01 Mar 2017
>> Date.today + 1.day * 2
=> Fri, 03 Mar 2017
Fixes #27457.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
It's common in test cases at my job to have code like this:
let(:today) { customer_start_date + 2.weeks }
let(:earlier_date) { today - 5.days }
With this change, we can instead write
let(:today) { 2.weeks.after(customer_start_date) }
let(:earlier_date) { 5.days.before(today) }
Closes #27721
|
|
|
|
|
| |
The Numeric extensions like 1.day, 1.month, etc. shouldn't know
how the internals of ActiveSupport::Duration works.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
durations from code
ActiveSupport::Duration.parse('P3Y') == 3.years # It should be true
Duration parsing made independent from any moment of time:
Fixed length in seconds is assigned to each duration part during parsing.
Changed duration of months and years in seconds to more accurate and logical:
1. The value of 365.2425 days in Gregorian year is more accurate
as it accounts for every 400th non-leap year.
2. Month's length is bound to year's duration, which makes
sensible comparisons like `12.months == 1.year` to be `true`
and nonsensical ones like `30.days == 1.month` to be `false`.
Calculations on times and dates with durations shouldn't be affected as
duration's numeric value isn't used in calculations, only parts are used.
Methods on `Numeric` like `2.days` now use these predefined durations
to avoid duplicating of duration constants through the codebase and
eliminate creation of intermediate durations.
|
| |
|
| |
|
|
|
|
|
|
|
|
|
|
|
|
| |
Prior to this commit, `3.months - 3.months` would result in a duration
that has the "parts" of `[[:months, 3], [:months, -3]]`. This would mean
that it was subtly different than `2.months - 2.months`. When applied to
a time, the date might actually change if the resulting day doesn't
exist however many months in the future, even though in both cases we
just wanted to add `0`, which should always be an identity operation.
With this change, we now store the parts as a hash, so `3.months -
3.months` is simply stored as `{ months: 0 }`.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
Previously `ActiveSupport::Duration.parse` used `Time.current` and
`Time#advance` to calculate the number of seconds in the duration
from an arbitrary collection of parts. However as `advance` tries to
be consistent across DST boundaries this meant that either the
duration was shorter or longer depending on the time of year.
This was fixed by using an absolute reference point in UTC which
isn't subject to DST transitions. An arbitrary date of Jan 1st, 2000
was chosen for no other reason that it seemed appropriate.
Additionally, duration parsing should now be marginally faster as we
are no longer creating instances of `ActiveSupport::TimeWithZone`
every time we parse a duration string.
Fixes #26941.
|
| |
|
|
|
|
|
|
|
|
| |
Style/SpaceBeforeBlockBraces
Style/SpaceInsideBlockBraces
Style/SpaceInsideHashLiteralBraces
Fix all violations in the repository.
|
|
|
|
|
| |
The current code base is not uniform. After some discussion,
we have chosen to go with double quotes by default.
|
|
|
|
| |
This is just to remove astonishment from getting `3600 seconds` from typing `1.hour`.
|
|
|
|
|
|
| |
Regression: adding minutes/hours to a time would change its time zone
This reverts commit 1bf9fe75a6473cb7501cae544cab772713e68cef.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
```ruby
ActiveSupport::Duration.parse('P3Y6M4DT12H30M5S')
(3.years + 3.days).iso8601
```
Inspired by Arnau Siches' [ISO8601 gem](https://github.com/arnau/ISO8601/)
and rewritten by Andrey Novikov with suggestions from Andrew White. Test
data from the ISO8601 gem redistributed under MIT license.
(Will be used to support the PostgreSQL interval data type.)
|
|
|
|
| |
This is just to remove astonishment from getting `3600 seconds` from typing `1.hour`.
|
| |
|
| |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
Hi there,
i have an app without english as available locale. So i got an error when we try to inspect something like 1.day. This is done automatically when we use the dalli cache.
I would like to change the :en to ::I18n.default_locale to be sure that this is always constant and is an available locale.
Tests are all green with this change.
Calculating -------------------------------------
:locale => :en 2.024k i/100ms
:locale => ::I18n.default_locale 2.236k i/100ms
-------------------------------------------------
:locale => :en 25.758k (±26.3%) i/s - 117.392k
:locale => ::I18n.default_locale 26.311k (±18.1%) i/s - 127.452k
|
| |
|
|
|
|
| |
this should be `:nodoc:` in order to be parsed.
|
|\
| |
| | |
Delegate comparison operator to value
|
| | |
|
|/
|
|
|
| |
- Reference : https://github.com/rails/rails/pull/17493#issuecomment-61739359
- Duration stopped inheriting from ProxyObject in https://github.com/rails/rails/pull/16574
|
| |
|
|\
| |
| |
| |
| |
| |
| |
| |
| | |
Added method `#eql?` to `ActiveSupport::Duration`, in addition to `#==`.
Conflicts:
activesupport/CHANGELOG.md
activesupport/lib/active_support/duration.rb
activesupport/test/core_ext/duration_test.rb
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| | |
Currently, the following returns `false`, contrary to expectation:
1.minute.eql?(1.minute)
Adding method `#eql?` will make this behave like expected. Method `#eql?` is
just a bit stricter than `#==`, as it checks whether the argument is also a
uration. Their parts may be different though.
1.minute.eql?(60.seconds) # => true
1.minute.eql?(60) # => false
|
| | |
|
| |
| |
| |
| |
| |
| |
| | |
For the sake of backward-compatibility, we need to make #instance_of?
return true for Fixnum. On the other hand, the method should still
give true for ActiveSupport::Duration itself which was not the case
before.
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| | |
Since Duration is extending from ProxyObject which extends itself from
BasicObject, the Duration object doesn't respond to the #instance_of?
method. Thus, the #method_missing hook get triggered, delegating the
method to its `value` attribute.
However, Rubinius' #eql? definition relies on #instance_of?, thus this
will equal to true with a Fixnum (since its `value` attribute is a
Fixnum) while it should not.
The previous behavior was wrong anyway, no matter the implementation.
|
| | |
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| | |
This fixes:
1.second.eql?(1.second) #=> false
The new `eql?` requires that `other` is an `ActiveSupport::Duration`.
This requirement makes `ActiveSupport::Duration`'s behavior consistent
with other numeric types in Ruby.
1.eql?(1.0) #=> false
1.0.eql?(1) #=> false
1.second.eql?(1) #=> false (was true)
1.eql?(1.second) #=> false
{ 1 => "foo", 1.0 => "bar" }
#=> { 1 => "foo", 1.0 => "bar" }
{ 1 => "foo", 1.second => "bar" }
# now => { 1 => "foo", 1.second => "bar" }
# was => { 1 => "bar" }
And though the behavior here hasn't changed, for reference:
1 == 1.0 #=> true
1.0 == 1 #=> true
1 == 1.second #=> true
1.second == 1 #=> true
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| | |
In Ruby 2.0.0-p353 there was a
[commit](https://github.com/ruby/ruby/commit/66915c507777c5e3a978fa73de25db763efd9206)
that switched case matching from actual sending `===` method to magic lookup,
that does not see it in `method_missing`. It's hard to predict how exactly and
when exactly this bug will be solved so here I suggest a solution of defining
it in Duration directly.
In Ruby 2.0.0-p353 without the added fix added test crashes to segmentation
fault.
|
| |
| |
| |
| |
| | |
This reverts commit e5f5a838b96a362534d9bb60d02334439ed9784c, reversing
changes made to d7567f3290a50952494e9213556a1f283a6cf3a0.
|
| | |
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| | |
* eliminates need for temp Hash
Also added a couple of examples to DurationTest to specify:
* duration can be defined with units out of order e.g.
1.month + 1.year + 1.second + 1.day
* equality with a Fixnum works regardless of which operand is on which
side of the operator
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| | |
In preparing https://github.com/rails/rails/pull/11855, it took me a
minute to understand what was going on due to naming (parts refers first
to an attr_accessor, then to a local, and is then reassigned), but also
because the iterator conditionally builds nulls and then removes them.
I refactored to something much more functional-looking that I find
easier to read, but you may or may not. If you do, great! Enjoy! If
not, oh well, I tried. Can't win 'em all :)
Rationale:
* no name conflict between local var and attr_accessor
* no reassignment of local var
* algorithm spelled out in steps
* unused items in initial list filtered out early
* empty-list case handled early instead of reassigning local var
* no duplication of formatting strings ("0 seconds")
Benchmarks (after PR #11855 merged):
10000.times do
1.second.inspect
end
original #inspect
0.350000 0.000000 0.350000 ( 0.354709)
0.330000 0.000000 0.330000 ( 0.331885)
0.330000 0.000000 0.330000 ( 0.334441)
refactored #inspect
0.340000 0.000000 0.340000 ( 0.340080)
0.340000 0.010000 0.350000 ( 0.345069)
0.330000 0.000000 0.330000 ( 0.335873)
10000.times do
(1.day + 1.month + 2.minutes + 1.day).inspect
end
original #inspect
0.400000 0.000000 0.400000 ( 0.403027)
0.400000 0.000000 0.400000 ( 0.403781)
0.390000 0.000000 0.390000 ( 0.387596)
refactored #inspect
0.400000 0.010000 0.410000 ( 0.399792)
0.400000 0.000000 0.400000 ( 0.404145)
0.400000 0.000000 0.400000 ( 0.403820)
|
|/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
Even though singularize tells a better story, chop is more than 50%
faster and is all that is necessary in the case of Duration#inspect, in
which all of the candidates for chopping appear in the same method and
are all singularized by removing the final 's'.
Benchmarks:
10000.times do
1.second.inspect
end
original #inspect
0.740000 0.000000 0.740000 ( 0.739065)
0.740000 0.000000 0.740000 ( 0.741458)
0.740000 0.010000 0.750000 ( 0.744011)
refactored #inspect
0.330000 0.000000 0.330000 ( 0.333390)
0.330000 0.000000 0.330000 ( 0.331013)
0.320000 0.000000 0.320000 ( 0.330103)
10000.times do
(1.day + 1.month + 2.minutes + 1.day).inspect
end
original #inspect
0.790000 0.000000 0.790000 ( 0.794624)
0.770000 0.000000 0.770000 ( 0.774577)
0.770000 0.010000 0.780000 ( 0.771295)
refactored #inspect
0.390000 0.000000 0.390000 ( 0.392921)
0.400000 0.000000 0.400000 ( 0.397412)
0.370000 0.000000 0.370000 ( 0.379660)
|
|
|
|
|
| |
AS::BasicObject is used for proxy classes. Let's give it a less concerning
name. Also, it avoids the confusion with Ruby's Basic Object.
|
| |
|