Repository navigation
Fix inconsistent parsing of Durations with both months and years - #27610
Conversation
|
@Envek hmm, you could also make the case that the current behavior of and if I merge this PR it'll break this: So you could argue that the PR should be the other way round - change |
|
Thanks @Envek. @pixeltrix I can see what you're pointing at, but you can't have any proper definition of duration where 1 year = 12 months. And making some year be 365 days and other years be 366 days as well as making month length arbitrary in the same way just to make sure than 12.months == 1.year isn't really going to help make things consistent. |
|
@boazy if you take your examples which are inconsistent, they're only inconsistent because of the use of fixed constants in 1.year. If you used the epoch date then that inconsistency would go away. You argue that it's not possible to have Also the year value of 365.25 days is for a Julian year, but we should be using a Gregorian year value of 365.2425 to account for leap years in 2000, 2400, etc. My biggest problem with switching to using |
|
In 5.0 and current master things are not consistent: 1.year == 12.months # => false
ActiveSupport::Duration.parse("P1Y") == ActiveSupport::Duration.parse("P12M") # => trueIn current version of this PR things get consistent by changing parsing to match the numeric extensions: 1.year == 12.months # => false
ActiveSupport::Duration.parse("P1Y") == ActiveSupport::Duration.parse("P12M") # => falseThe calculation on times and dates with durations are working same as numeric (Date.current + 1.year) == (Date.current + 12.months) # true
ActiveSupport::Duration.new(42, [[:months, 12]]) == 12.months # => false
now = Time.current;
now + ActiveSupport::Duration.new(42, [[:months, 12]]) == now + 12.months # => trueMay be the best approach is to ignore |
|
I was looking at the values for 1.month and 1.year and noticed that 365.2425 days is divisible by 12 so we could have 31556952 seconds for years and 2629746 seconds for months. This would mean that 12.months would then equal 1.year automatically. The value for 365.2425 days is what we should be using anyway since that's the gregorian value. The problem is that this would break 1.month == 30.days, but that's nonsense anyway plus I doubt anyone was using 365.25.days == 1.year wdyt ? |
|
@pixeltrix, sounds reasonable for me. I've added these changes in separate commits to this pull request. Please take a look and play with them. |
|
There's also couple of comments that'll need changing where we state that 1.month == 30.days and 1.year == 365.25.days. |
|
Also I think changing 365.25 to 365.2425 can only be on master - the backport will have to use the old values for year and month with |
|
Searched repository for usage of x.month(s) and y.year(s) and corrected tests and comments accordingly. If everything is fine I will squash and rebase and then create separate PR with these changes without changed constants to |
|
@Envek 👍 |
…g 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.
|
Squashed and rebased. Partial backport is on the way. |
|
@pixeltrix @Envek |
|
@boazy: Ruby's Rails' |
|
@Envek Thanks. Since this PR removes reliance on |
|
@Envek thanks for your patience! 👍 |
Since 1.month no longer equals 30.days add some tests to ensure that addition maintains the same day in the month or is the last day in the month if the month has less days than the current day. Also add a test for the behaviour of 12.months == 1.year.
Rails 5.1 switched from julian to gregorian calendar so 1.month and 1.year changed in value. Note, duration_of_report_step consumers should be 30.days and sometimes 1.month, ugh rails/rails#27610 (comment) rails/rails@2a5ae2b Rails PR: #29971
Rails 5.1 switched from julian to gregorian calendar so 1.month and 1.year changed in value. Note, duration_of_report_step consumers should be 30.days and sometimes 1.month, ugh rails/rails#27610 (comment) rails/rails@2a5ae2b Rails PR: #29971
Rails 5.1 switched from julian to gregorian calendar so 1.month and 1.year changed in value. Note, duration_of_report_step consumers should be 30.days and sometimes 1.month, ugh rails/rails#27610 (comment) rails/rails@2a5ae2b Rails PR: #29971
Rails 5.1 switched from julian to gregorian calendar so 1.month and 1.year changed in value. Note, duration_of_report_step consumers should be 30.days and sometimes 1.month, ugh rails/rails#27610 (comment) rails/rails@2a5ae2b Rails PR: #29971
Rails 5.1 switched from julian to gregorian calendar so 1.month and 1.year changed in value. Note, duration_of_report_step consumers should be 30.days and sometimes 1.month, ugh rails/rails#27610 (comment) rails/rails@2a5ae2b Rails PR: #29971
Rails 5.1 switched from julian to gregorian calendar so 1.month and 1.year changed in value. Note, duration_of_report_step consumers should be 30.days and sometimes 1.month, ugh rails/rails#27610 (comment) rails/rails@2a5ae2b Rails PR: #29971
Rails 5.1 switched from julian to gregorian calendar so 1.month and 1.year changed in value. Note, duration_of_report_step consumers should be 30.days and sometimes 1.month, ugh rails/rails#27610 (comment) rails/rails@2a5ae2b Rails PR: #29971
Rails 5.1 switched from julian to gregorian calendar so 1.month and 1.year changed in value. Note, duration_of_report_step consumers should be 30.days and sometimes 1.month, ugh rails/rails#27610 (comment) rails/rails@2a5ae2b Rails PR: #29971
Rails 5.1 switched from julian to gregorian calendar so 1.month and 1.year changed in value. Note, duration_of_report_step consumers should be 30.days and sometimes 1.month, ugh rails/rails#27610 (comment) rails/rails@2a5ae2b Rails PR: #29971
Rails 5.1 switched from julian to gregorian calendar so 1.month and 1.year changed in value. Note, duration_of_report_step consumers should be 30.days and sometimes 1.month, ugh rails/rails#27610 (comment) rails/rails@2a5ae2b Rails PR: #29971
Rails 5.1 switched from julian to gregorian calendar so 1.month and 1.year changed in value. Note, duration_of_report_step consumers should be 30.days and sometimes 1.month, ugh rails/rails#27610 (comment) rails/rails@2a5ae2b Rails PR: #29971
Rails 5.1 switched from julian to gregorian calendar so 1.month and 1.year changed in value. Note, duration_of_report_step consumers should be 30.days and sometimes 1.month, ugh rails/rails#27610 (comment) rails/rails@2a5ae2b Rails PR: #29971
Rails 5.1 switched from julian to gregorian calendar so 1.month and 1.year changed in value. Note, duration_of_report_step consumers should be 30.days and sometimes 1.month, ugh rails/rails#27610 (comment) rails/rails@2a5ae2b Rails PR: #29971
Rails 5.1 switched from julian to gregorian calendar so 1.month and 1.year changed in value. Note, duration_of_report_step consumers should be 30.days and sometimes 1.month, ugh rails/rails#27610 (comment) rails/rails@2a5ae2b Rails PR: #29971
Rails 5.1 switched from julian to gregorian calendar so 1.month and 1.year changed in value. Note, duration_of_report_step consumers should be 30.days and sometimes 1.month, ugh rails/rails#27610 (comment) rails/rails@2a5ae2b Rails PR: #29971
Rails 5.1 switched from julian to gregorian calendar so 1.month and 1.year changed in value. Note, duration_of_report_step consumers should be 30.days and sometimes 1.month, ugh rails/rails#27610 (comment) rails/rails@2a5ae2b Rails PR: #29971
Rails 5.1 switched from julian to gregorian calendar so 1.month and 1.year changed in value. Note, duration_of_report_step consumers should be 30.days and sometimes 1.month, ugh rails/rails#27610 (comment) rails/rails@2a5ae2b Rails PR: #29971
Rails 5.1 switched from julian to gregorian calendar so 1.month and 1.year changed in value. Note, duration_of_report_step consumers should be 30.days and sometimes 1.month, ugh rails/rails#27610 (comment) rails/rails@2a5ae2b Rails PR: #29971
Rails 5.1 switched from julian to gregorian calendar so 1.month and 1.year changed in value. Note, duration_of_report_step consumers should be 30.days and sometimes 1.month, ugh rails/rails#27610 (comment) rails/rails@2a5ae2b Rails PR: #29971
Rails 5.1 switched from julian to gregorian calendar so 1.month and 1.year changed in value. Note, duration_of_report_step consumers should be 30.days and sometimes 1.month, ugh rails/rails#27610 (comment) rails/rails@2a5ae2b Rails PR: #29971
Rails 5.1 switched from julian to gregorian calendar so 1.month and 1.year changed in value. Note, duration_of_report_step consumers should be 30.days and sometimes 1.month, ugh rails/rails#27610 (comment) rails/rails@2a5ae2b Rails PR: #29971
Rails 5.1 switched from julian to gregorian calendar so 1.month and 1.year changed in value. Note, duration_of_report_step consumers should be 30.days and sometimes 1.month, ugh rails/rails#27610 (comment) rails/rails@2a5ae2b Rails PR: #29971
Rails 5.1 switched from julian to gregorian calendar so 1.month and 1.year changed in value. Note, duration_of_report_step consumers should be 30.days and sometimes 1.month, ugh rails/rails#27610 (comment) rails/rails@2a5ae2b Rails PR: #29971
Rails 5.1 switched from julian to gregorian calendar so 1.month and 1.year changed in value. Note, duration_of_report_step consumers should be 30.days and sometimes 1.month, ugh rails/rails#27610 (comment) rails/rails@2a5ae2b Rails PR: #29971
Rails 5.1 switched from julian to gregorian calendar so 1.month and 1.year changed in value. Note, duration_of_report_step consumers should be 30.days and sometimes 1.month, ugh rails/rails#27610 (comment) rails/rails@2a5ae2b Rails PR: #29971
Rails 5.1 switched from julian to gregorian calendar so 1.month and 1.year changed in value. Note, duration_of_report_step consumers should be 30.days and sometimes 1.month, ugh rails/rails#27610 (comment) rails/rails@2a5ae2b Rails PR: #29971
Rails 5.1 switched from julian to gregorian calendar so 1.month and 1.year changed in value. Note, duration_of_report_step consumers should be 30.days and sometimes 1.month, ugh rails/rails#27610 (comment) rails/rails@2a5ae2b Rails PR: #29971
Rails 5.1 switched from julian to gregorian calendar so 1.month and 1.year changed in value. Note, duration_of_report_step consumers should be 30.days and sometimes 1.month, ugh rails/rails#27610 (comment) rails/rails@2a5ae2b Rails PR: #29971
Rails 5.1 switched from julian to gregorian calendar so 1.month and 1.year changed in value. Note, duration_of_report_step consumers should be 30.days and sometimes 1.month, ugh rails/rails#27610 (comment) rails/rails@2a5ae2b Rails PR: #29971
@boazy reported in #16919 (comment) that the following code may fail (success depends on the current time):
The solution is make duration parsing independent of any moment of time and just assign fixed values of seconds to each duration part (as it's really done in methods like
2.days).I have not used suggested solution with
total_seconds += value.send(part.to_sym)as it will create many intermediate durations, slowing down parsing and littering memory. Instead I've added a part-to-seconds mapping hash constant to durations itself.Failing regression test is included.
Duration parsing got 1.5x faster: https://gist.github.com/Envek/d0763db337d34ef78bf8d0a1aceca9aa
r? @pixeltrix
P.S>
Methods like beforementioned
2.dayscan be changed to use this constant too, this will speed up them, move all magic duration constants in single file and constants and eliminate creation of a bunch of intermediate durations. Now calling2.yearswill call365.25.dayswhich in turn will call24.hourswhich in turn will create duration for2 * 365.25 * 24 * 3600seconds from which seconds will be taken and all 3 intermediate durations will be thrown. I can change them too in this or separate pull request.So
will become