Skip to content

Change 1.week to create 1 week durations instead of 7 days durations. - #22806

Merged
jeremy merged 1 commit into
rails:masterfrom
Envek:solid_durations
Apr 18, 2016
Merged

jeremy merged 1 commit into
rails:masterfrom
Envek:solid_durations

Conversation

@Envek

@Envek Envek commented Dec 27, 2015

Copy link
Copy Markdown
Contributor

As it was proposed by @pixeltrix in discussion at #16917 (comment) I'm changing ActiveSupport::Duration creation with numeric methods like 1.week to create durations with more predictable and ISO8601-conformant parts. Because when 1.hour is actually 3600 seconds it's logically correct, but not expected and confusing.

See original discussion for details: #16917 (diff)

Before:

[1.hour.inspect, 1.hour.value, 1.hour.parts] # => ["3600 seconds", 3600, [[:seconds, 3600]]]
[1.week.inspect, 1.week.value, 1.week.parts] # => ["7 days", 604800, [[:days, 7]]]

After:

[1.hour.inspect, 1.hour.value, 1.hour.parts] # => ["1 hour", 3600, [[:hours, 1]]]
[1.week.inspect, 1.week.value, 1.week.parts] # => ["1 week", 604800, [[:weeks, 1]]]

r? @pixeltrix

@rails-bot

Copy link
Copy Markdown

Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @pixeltrix (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@Envek

Envek commented Jan 11, 2016

Copy link
Copy Markdown
Contributor Author

@pixeltrix, could you please review?

@Envek

Envek commented Apr 14, 2016

Copy link
Copy Markdown
Contributor Author

Can anyone review? Is there anything stop this small PR from being merged?

P.S> Now with signed commit!

@jeremy

jeremy commented Apr 17, 2016

Copy link
Copy Markdown
Member

Nice, @Envek!

What do Rails users need to know about compatibility?

Does this affect current apps in any way?

@Envek

Envek commented Apr 17, 2016

Copy link
Copy Markdown
Contributor Author

It should not affect current apps.

Duration's value (number of seconds) remains the same, only hash of parts (and inspect value) is changed and only when it's constructed by calling methods on integers. Manual construction of Durations isn't affected. Inside the ActiveSupport::Duration itself most operations rely only on value of value.

So this is to remove astonishment from getting 3600 seconds from 1.hour.
Also it will help in #16919 when you have PT1H in the database and getting 3600 seconds in Ruby and then after save you get PT3600S in database. Durations just need to be a bit more predictable.

@jeremy

jeremy commented Apr 17, 2016

Copy link
Copy Markdown
Member

Sounds good! Would you please update the CHANGELOG message to cover this too?

This is just to remove astonishment from getting `3600 seconds` from typing `1.hour`.
@Envek

Envek commented Apr 18, 2016

Copy link
Copy Markdown
Contributor Author

@jeremy, updated CHANGELOG, squashed and rebased.

It should not affect current apps as duration's `value` (number of seconds) remains the same,
only hash of `parts` (and `inspect` value) is changed and only when it's constructed by calling
methods on integers. Manual construction of Durations isn't affected.
Inside the ActiveSupport::Duration itself most operations rely only on number of seconds.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this line can be removed from CHANGELOG. It can be moved to commit message :)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Infact the whole para can be moved to commit message.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@prathamesh-sonpatki It's important for the changelog because it answers Rails users' primary question: will this break compatibility for my app / library / plugin?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Got it.

@jeremy
jeremy merged commit 1bf9fe7 into rails:master Apr 18, 2016
jeremy added a commit that referenced this pull request Apr 18, 2016
Introduce weeks and hours periods to Duration.

Change 1.week to create a 1 week duration instead of 7 days and 1.hour
to create a 1 hour duration instead of 3600 seconds.
if type == :seconds
t.since(sign * number)
elsif [:hours, :minutes].include?(type)
t.in_time_zone.advance(type => sign * number)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regression:

>> Time.zone = 'Central Time (US & Canada)'
=> "Central Time (US & Canada)"
>> Time.now.utc
=> 2016-04-27 19:27:01 UTC
>> Time.now.utc + 1.second
=> 2016-04-27 19:27:05 UTC
>> Time.now.utc + 1.minute
=> Wed, 27 Apr 2016 14:28:36 CDT -05:00
>> Time.now.utc + 1.hour
=> Wed, 27 Apr 2016 15:27:39 CDT -05:00
>> Time.now.utc + 1.day
=> 2016-04-28 19:27:42 UTC

Why'd you do time zone conversion for hours & minutes only?

@jeremy

jeremy commented Apr 27, 2016

Copy link
Copy Markdown
Member

Reverting this pending regression consideration, fix, and test.

@jeremy

jeremy commented Apr 27, 2016

Copy link
Copy Markdown
Member

Reverted @ c1ad19c

@pixeltrix

Copy link
Copy Markdown
Contributor

@Envek it looks like you're using in_time_zone to make the test NumericExtDateTest#test_date_plus_duration pass - this is causing UTC times to be converted to a local time. A better way to do this would be to convert back to seconds and use since like this:

def sum(sign, time = ::Time.current) #:nodoc:
  parts.inject(time) do |t,(type,number)|
    if t.acts_like?(:time) || t.acts_like?(:date)
      if type == :seconds
        t.since(sign * number)
      elsif type == :minutes
        t.since(sign * number * 60)
      elsif type == :hours
        t.since(sign * number * 3600)
      else
        t.advance(type => sign * number)
      end
    else
      raise ::ArgumentError, "expected a time or date, got #{time.inspect}"
    end
  end
end

Can you submit a new PR - thanks!

@Envek

Envek commented Apr 28, 2016

Copy link
Copy Markdown
Contributor Author

@jeremy, thank you for figuring out the regression.
@pixeltrix, thank you for solution! Added it in #24762.

jeremy added a commit that referenced this pull request Apr 29, 2016
Use the updated changelog from the first merge: 7254517

References #22806, #24762.

[ci skip]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants