Skip to content

Commit af1f4e1

Browse files
authored
Merge pull request #6228 from headius/strftime_space_padding
Default to space padding for "special" formats
2 parents 9bf3c0a + 20d7438 commit af1f4e1

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

core/src/main/java/org/jruby/util/RubyDateFormatter.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,12 +275,37 @@ public List<Token> compilePattern(ByteList pattern, boolean dateLibrary) {
275275
lexer.yyreset(reader);
276276

277277
Token token;
278+
RubyTimeOutputFormatter formatter = null;
278279
try {
279280
while ((token = lexer.yylex()) != null) {
280-
if (token.format != Format.FORMAT_SPECIAL) {
281+
if (token.format == Format.FORMAT_OUTPUT) {
282+
formatter = (RubyTimeOutputFormatter) token.data;
283+
} else if (token.format != Format.FORMAT_SPECIAL) {
284+
if (formatter != null) {
285+
compiledPattern.add(new Token(Format.FORMAT_OUTPUT, formatter));
286+
formatter = null;
287+
}
281288
compiledPattern.add(token);
282289
} else {
283290
char c = (Character) token.data;
291+
292+
// prepare padding if necessary
293+
if (formatter != null) {
294+
switch (c) {
295+
default:
296+
// force most formats to use spaces for padding unless otherwise specified
297+
if (formatter.flags == "" || formatter.flags == null) {
298+
compiledPattern.add(new Token(Format.FORMAT_OUTPUT, new RubyTimeOutputFormatter("_", formatter.width)));
299+
break;
300+
}
301+
// fall through
302+
case 'Q':
303+
case '+':
304+
compiledPattern.add(new Token(Format.FORMAT_OUTPUT, formatter));
305+
}
306+
formatter = null;
307+
}
308+
284309
switch (c) {
285310
case 'c':
286311
addToPattern(compiledPattern, "a b e H:M:S Y");
@@ -344,6 +369,11 @@ public List<Token> compilePattern(ByteList pattern, boolean dateLibrary) {
344369
}
345370
}
346371
}
372+
373+
// if formatter is still set we didn't use it, add it as is
374+
if (formatter != null) {
375+
compiledPattern.add(new Token(Format.FORMAT_OUTPUT, formatter));
376+
}
347377
} catch (IOException e) {
348378
throw new AssertionError(e); // IOException never happens
349379
}

0 commit comments

Comments
 (0)