Skip to content

Commit ab9024e

Browse files
committed
Fix check for fast format, only %6N is accepted, not %N
1 parent 8822fa1 commit ab9024e

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

src/main/java/org/truffleruby/core/time/RubyDateFormatter.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -574,12 +574,12 @@ public static boolean formatCanBeFast(Token[] compiledPattern) {
574574

575575
switch (format) {
576576
case FORMAT_ENCODING:
577-
// We only care about UTF8 encoding
577+
// Only handle UTF-8 for fast formats
578578
if (token.getData() != UTF8Encoding.INSTANCE) {
579579
return false;
580580
}
581581
break;
582-
case FORMAT_OUTPUT:
582+
case FORMAT_OUTPUT: // only %6N
583583
RubyTimeOutputFormatter formatter = (RubyTimeOutputFormatter) token.getData();
584584

585585
// Check for the attributes present in the default case
@@ -595,14 +595,19 @@ public static boolean formatCanBeFast(Token[] compiledPattern) {
595595
return false;
596596
}
597597
break;
598+
case FORMAT_NANOSEC: // only %6N
599+
if (i - 1 >= 0 && compiledPattern[i - 1].getFormat() == Format.FORMAT_OUTPUT) {
600+
break;
601+
} else {
602+
return false;
603+
}
598604
case FORMAT_STRING:
599605
case FORMAT_DAY:
600606
case FORMAT_HOUR:
601607
case FORMAT_MINUTES:
602608
case FORMAT_MONTH:
603609
case FORMAT_SECONDS:
604610
case FORMAT_YEAR_LONG:
605-
case FORMAT_NANOSEC:
606611
break;
607612
default:
608613
return false;
@@ -646,22 +651,21 @@ public static Rope formatToRopeFast(Token[] compiledPattern, ZonedDateTime dt,
646651

647652
case FORMAT_YEAR_LONG: {
648653
final int value = dt.getYear();
649-
650654
assert value >= 1000;
651655
assert value <= 9999;
652656

653657
appendRope = new LazyIntRope(value, UTF8Encoding.INSTANCE, 4);
654658
}
655659
break;
656660

657-
case FORMAT_NANOSEC: {
661+
case FORMAT_NANOSEC: { // always %6N, checked by formatCanBeFast()
658662
final int nano = dt.getNano();
659663
final LazyIntRope microSecondRope = new LazyIntRope(nano / 1000);
660664

661665
// This fast-path only handles the '%6N' format, so output will always be 6 characters long.
662666
final int length = 6;
663667
final int padding = length - microSecondRope.characterLength();
664-
assert padding >= 0;
668+
assert padding >= 0 : microSecondRope;
665669

666670
// `padding` is guaranteed to be >= 0 because `nano` can be at most 9 digits long before the
667671
// conversion to microseconds. The division further constrains the rope to be at most 6 digits long.

0 commit comments

Comments
 (0)