Skip to content

Fix Time.new parsing (time/new_tags.txt)#8890

Merged
enebo merged 1 commit intojruby:masterfrom
PChambino:time-new-parse
Jul 5, 2025
Merged

Fix Time.new parsing (time/new_tags.txt)#8890
enebo merged 1 commit intojruby:masterfrom
PChambino:time-new-parse

Conversation

@PChambino
Copy link
Contributor

The idea was to help towards this #8736 but most of the fixes here are specifically around parsing the time string, not timezone related.

@enebo I noticed you did recent changes around Time.new parsing as well, in case you want to review.

I'll add some comments in the PR to highlight which changes fix which parts, but here is the TLDR:

  • Time.new parsing should ignore "whitespace" chars, not only space chars

  • precision keyword argument should only be used by Time.new parsing and ignored otherwise (see Ruby docs), and it should default to 9

  • RubyTimeParser.parseInt now returns nil if no digits (so missing year can be detected) and only to a set number of digits (for subsec precision)

  • correctly convert subsec to microseconds in RubyTimeParser when more than 9 digits

The idea was to help towards this jruby#8736 but most of the fixes here are
specifically around parsing the time string, not timezone related.

@enebo I noticed you did recent changes around Time.new parsing as well,
in case you want to review.

I'll add some comments in the PR to highlight which changes fix which
parts, but here is the TLDR:

- Time.new parsing should ignore "whitespace" chars, not only space chars

- precision keyword argument should only be used by Time.new parsing
  and ignored otherwise (see Ruby docs), and it should default to 9

- RubyTimeParser.parseInt now returns nil if no digits (so missing year
  can be detected) and only to a set number of digits (for subsec precision)

- correctly convert subsec to microseconds in RubyTimeParser when more
  than 9 digits
fails(not implemented, jruby/jruby#6161):Time.new with a timezone argument subject's class implements .find_timezone method calls .find_timezone to build a time object at loading marshaled data
fails(only during full spec run):Time.new with a utc_offset argument raises ArgumentError if the String argument is not of the form (+|-)HH:MM
fails:Time.new with a timezone argument returned value by #utc_to_local and #local_to_utc methods cannot have arbitrary #utc_offset if it is an instance of Time
fails(https://github.com/jruby/jruby/issues/8736):Time.new with a timezone argument :in keyword argument allows omitting minor arguments
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was already passing, I think I might have fixed it in my previous PR: #8858
But I wasn't aware of these tag files.

fails:Time.new with a timezone argument returned value by #utc_to_local and #local_to_utc methods cannot have arbitrary #utc_offset if it is an instance of Time
fails(https://github.com/jruby/jruby/issues/8736):Time.new with a timezone argument :in keyword argument allows omitting minor arguments
fails(https://github.com/jruby/jruby/issues/8736):Time.new with a timezone argument Time.new with a String argument accepts precision keyword argument and truncates specified digits of sub-second part
fails(https://github.com/jruby/jruby/issues/8736):Time.new with a timezone argument Time.new with a String argument converts precision keyword argument into Integer if is not nil
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removing the type error in RubyTime basically fixed this. We already call toLong in the RubyTimeParser.

I also noticed that the precision keyword argument is only supposed to be used when parsing a ruby time string and ignore otherwise, and it should default to 9.

https://arc.net/l/quote/hugtggyb

fails(only during full spec run):Time.new with a utc_offset argument raises ArgumentError if the String argument is not of the form (+|-)HH:MM
fails:Time.new with a timezone argument returned value by #utc_to_local and #local_to_utc methods cannot have arbitrary #utc_offset if it is an instance of Time
fails(https://github.com/jruby/jruby/issues/8736):Time.new with a timezone argument :in keyword argument allows omitting minor arguments
fails(https://github.com/jruby/jruby/issues/8736):Time.new with a timezone argument Time.new with a String argument accepts precision keyword argument and truncates specified digits of sub-second part
Copy link
Contributor Author

Choose a reason for hiding this comment

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

RubyTime only has precision up to 9 (nanoseconds) so it is not possible to remove this tag, but the microseconds conversion was wrong ( + 3 vs - 3) when there were more than 9 digits. So it now truncates correctly.

fails(https://github.com/jruby/jruby/issues/8736):Time.new with a timezone argument Time.new with a String argument converts precision keyword argument into Integer if is not nil
fails(https://github.com/jruby/jruby/issues/8736):Time.new with a timezone argument Time.new with a String argument raises ArgumentError if date/time parts values are not valid
fails(https://github.com/jruby/jruby/issues/8736):Time.new with a timezone argument Time.new with a String argument raises ArgumentError if utc offset parts are not valid
fails(https://github.com/jruby/jruby/issues/8736):Time.new with a timezone argument Time.new with a String argument raises ArgumentError if string doesn't start with year
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed the RubyTimeParser.parseInt to return nil if no digits, so we can raise the correct error. Also, this error needs to have the str.inspect() in the error message instead of the plain string.

Comment on lines -18 to -19
fails(https://github.com/jruby/jruby/issues/8736):Time.new with a timezone argument Time.new with a String argument raises ArgumentError when there are leading space characters
fails(https://github.com/jruby/jruby/issues/8736):Time.new with a timezone argument Time.new with a String argument raises ArgumentError when there are trailing whitespaces
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These two were just about ignoring more "whitespace" chars, not just spaces.

fails(https://github.com/jruby/jruby/issues/8736):Time.new with a timezone argument Time.new with a String argument raises ArgumentError if string doesn't start with year
fails(https://github.com/jruby/jruby/issues/8736):Time.new with a timezone argument Time.new with a String argument raises ArgumentError when there are leading space characters
fails(https://github.com/jruby/jruby/issues/8736):Time.new with a timezone argument Time.new with a String argument raises ArgumentError when there are trailing whitespaces
fails:Time.new with a timezone argument Time.new with a String argument returns Time with correct subseconds when given seconds fraction is longer than 9 digits
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was fixed by adding support for "precision" in RubyTimeParser.parseInt.

fails:Time.new with a utc_offset argument returns a Time with a UTC offset of the specified number of Rational seconds
fails:Time.new with a utc_offset argument raises ArgumentError if the String argument is not in an ASCII-compatible encoding
fails:Time.new with a utc_offset argument with an argument that responds to #to_r coerces using #to_r
fails:Time.new with a utc_offset argument raises ArgumentError if the month is greater than 12
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was already passing, so I just removed it.

@enebo enebo merged commit d094c08 into jruby:master Jul 5, 2025
71 of 72 checks passed
@enebo
Copy link
Member

enebo commented Jul 5, 2025

@PChambino cool. The -3 vs +3 makes sense and explains a confusion I had when working on the other recent PR I did 😄.

I am mildly confused about the already passing specs since I thought I had did a mspec-tag to untag passing things but I must have done that manually?

@enebo enebo added this to the JRuby 10.0.1.0 milestone Jul 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants