Skip to content

Commit 8eb8704

Browse files
committed
Fix formatting of microseconds in Time#strftime's fast-path.
1 parent 2d75a98 commit 8eb8704

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
@@ -655,18 +655,22 @@ public static Rope formatToRopeBuilderFast(Token[] compiledPattern, ZonedDateTim
655655

656656
case FORMAT_NANOSEC: {
657657
final int nano = dt.getNano();
658-
final LazyIntRope nanoRope = new LazyIntRope(nano);
658+
final LazyIntRope microSecondRope = new LazyIntRope(nano / 1000);
659659

660+
// This fast-path only handles the '%6N' format, so output will always be 6 characters long.
660661
final int length = 6;
661-
final int padding = length - nanoRope.characterLength();
662+
final int padding = length - microSecondRope.characterLength();
662663

664+
// `padding` is guaranteed to be >= 0 because `nano` can be at most 9 digits long before the
665+
// conversion to microseconds. The division further constrains the rope to be at most 6 digits long.
663666
if (padding == 0) {
664-
appendRope = nanoRope;
665-
} else if (padding < 0) {
666-
appendRope = substringNode.executeSubstring(nanoRope, 0, length);
667+
appendRope = microSecondRope;
667668
} else {
668669
appendRope = concatNode
669-
.executeConcat(nanoRope, RopeConstants.paddingZeros(padding), UTF8Encoding.INSTANCE);
670+
.executeConcat(
671+
RopeConstants.paddingZeros(padding),
672+
microSecondRope,
673+
UTF8Encoding.INSTANCE);
670674
}
671675
}
672676
break;

0 commit comments

Comments
 (0)