-
Notifications
You must be signed in to change notification settings - Fork 21.8k
Methods for ActiveSupport::Duration parsing from ISO 8601 and output to it #16917
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
end | ||
end | ||
|
||
def test_iso8601_parsing_valid_patterns |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does this test just repeat part of what's already tested in test_iso8601_output_and_reparsing
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it repeat. This test only checks that validations doesn't complain. test_iso8601_output_and_reparsing
tries to test some logic (as possible with weird ActiveSupport::Duration
).
Should I remove this test case?
Thanks to @egilburg comment in rails/rails#16917 pull request for catching this.
Thanks to @Envek for catching it in rails/rails#16917
# Initialize new duration | ||
time = ::Time.now | ||
new(time.advance(parts) - time, parts) | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's enough going on here to extract a separate ISO8601DurationParser
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where it should be placed? Same file (inside Duration) or separate file? Its location?
Fixed some issues:
Method Rebased branch on top of current master due to changes in |
Fixed some more remarks. I've noticed that now there is next error occurs when I run activerecord's tests for #16919 on top of this branch:
How can I fix it? The reason seems to be in that I'm explicitly requires file with parser in |
To avoid circular require warnings (absolutely don't understand why are they appearing such a lot) included ISO8601 parser into file |
Did you require duration in the parser and vice versa? That sounds like it could cause the circular requires. |
No, I had required parser only from duration (no requires from file with parser at all): commit that caused a lot of warnings. |
|
raise ParsingError.new("Invalid ISO 8601 duration: #{iso8601duration} (only last part can be fractional)") | ||
end | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✂️ this line
I've accounted all the notes and force pushed changes in the same commit. I've tried split initializer on separate methods, but dislike current implementations (they are linked too tight with each other). P.S> in 6f8d9bd only one author specified. Git doesn't support multiple authors (at least with each one's email) |
@fxn I did remember seeing commits like that. I assumed it was for GitHub and not the contributors app, but it was the other way. Sorry about that! |
Ah, sorry, misunderstood you. Added both authors in |
parse!(iso8601duration) | ||
rescue ISO8601Parser::ParsingError | ||
nil | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO we don't need a method that swallows the parse error.
If we're pulling in code under MIT license, we need to include the license and copyright statement. Also, if we're pulling in code, perhaps we should do a gem dep instead? Nice work on this, and patience 😁 |
@jeremy there is not a much code left from the original code from ISO8601 gem as I've rewritten parser from regexp to string scanner by @pixeltrix suggestion. As far as I can see for now only duration examples strings inside tests are same with ISO8601. See the original code and tests. Anyway where I should place license and copyright statement if I should? @arnau what do you think? Gem dependency means that we should throw away whole @jeremy please take a look at #22806 while I'm fixing your notes— it's extracted from this pull request. |
@jeremy fixed your notes |
@Envek We can't replace AS::Duration with ISO8601::Duration, but perhaps we could delegate implementation to it. You tell me :) If you've rewritten the code, then you're no longer using the original code and aren't redistributing it, so you needn't pull in its license. If you are reusing code, include the license+copyright along with the code, in a Ruby comment. |
@Envek do whatever makes more sense to you :) |
@@ -224,6 +224,9 @@ def test_comparable | |||
assert_equal(1, (61 <=> 1.minute)) | |||
end | |||
|
|||
# ISO8601 string examples are taken from ISO8601 gem at https://github.com/arnau/ISO8601/blob/b93d466840/spec/iso8601/duration_spec.rb | |||
# published under the conditions of MIT license: https://github.com/arnau/ISO8601/blob/b93d466840/LICENSE | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The license is pretty clear about what we need to include here:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
So we need to include the license text in a comment here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Included the license text in a comment
@@ -130,6 +133,23 @@ def respond_to_missing?(method, include_private=false) #:nodoc: | |||
@value.respond_to?(method, include_private) | |||
end | |||
|
|||
# Creates a new Duration from string formatted according to ISO 8601 Duration. | |||
# | |||
# See http://en.wikipedia.org/wiki/ISO_8601#Durations |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See http://en.wikipedia.org/wiki/ISO_8601#Durations .
class Duration | ||
# Parses a string formatted according to ISO 8601 Duration into the hash. | ||
# | ||
# See [ISO 8601](http://en.wikipedia.org/wiki/ISO_8601#Durations) for more information. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😢 Sorry this is rdoc, my bad. Let me see.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So rdoc makes sure the link is proper even if it's followed by a '.' Anyway, lets change to -
See {ISO 8601}[http://en.wikipedia.org/wiki/ISO_8601#Durations] for more information.
Merged! 04c512d |
[ci skip]
Add #16917 to release notes
This PR add methods to
ActiveSupport::Duration
to allow present it in ISO 8601 Duration format and instantiate from it.ISO 8601 Duration format is standartized way to represent duration for interchange, it's already recognized by some database engines (e.g. PostgreSQL) and client side libraries (e.g. plugin for Moment.js durations). So, I think it should be part of
ActiveSupport::Duration
.Some parts of code and tests are taken from ISO8601 gem by Arnau Siches (@arnau) and contributors. Many thanks to them.
This PR is required for PostgreSQL
interval
datatype support (for converting it from and toActiveSupport::Duration
) as I've proposed in Google Group here. See #16919. Because of that my parsing allows individual datetime parts to be negative (see PostgreSQL interval output).There is no backward incompatible changes as I want it to be included in upcoming 4.2 release, if it still possible (pleeeaasee!).