Skip to content

Commit d268774

Browse files
committed
Align date pattern to regex conversion with DateTimeFormatter
CachingDateFormatter formats dates with DateTimeFormatter, but the regex conversion and its documentation still assumed SimpleDateFormat. - support the 'L' (stand-alone month) pattern letter in CharSequenceToRegexMapper - add DatePatternToRegexUtil(String, Locale) so that symbols can be taken from a locale other than the default one - FileNamePattern.toRegexForFixedDate(Date) now delegates to the Instant overload - add DatePatternToRegexUtilTest, checking that dates formatted in 16 locales match their regex - check the regex against DateTimeFormatter output in DatePatternToRegexTest Signed-off-by: ceki <[email protected]>
1 parent b4b4828 commit d268774

7 files changed

Lines changed: 330 additions & 42 deletions

File tree

‎logback-core/src/main/java/ch/qos/logback/core/rolling/helper/DateTokenConverter.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@
2424
import ch.qos.logback.core.util.DatePatternToRegexUtil;
2525

2626
/**
27-
* Returns a date formatted by SimpleDateFormatter.
28-
*
27+
* Returns a date formatted by {@link CachingDateFormatter}, which relies on
28+
* {@link java.time.format.DateTimeFormatter}.
29+
*
2930
* @author Ceki G&uuml;lc&uuml;
3031
*/
3132
public class DateTokenConverter<E> extends DynamicConverter<E> implements MonoTypedConverter {

‎logback-core/src/main/java/ch/qos/logback/core/rolling/helper/FileNamePattern.java‎

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -194,28 +194,17 @@ public String getPattern() {
194194
/**
195195
* Given date, convert this instance to a regular expression.
196196
*
197-
* Used to compute sub-regex when the pattern has both %d and %i, and the date
198-
* is known.
199-
*
197+
* <p>Used to compute sub-regex when the pattern has both %d and %i, and the date
198+
* is known.</p>
199+
*
200+
* <p>Implemented by delegating to {@link #toRegexForFixedDate(Instant)}.</p>
201+
*
200202
* @param date - known date
201203
*/
202204
public String toRegexForFixedDate(Date date) {
203-
StringBuilder buf = new StringBuilder();
204-
Converter<Object> p = headTokenConverter;
205-
while (p != null) {
206-
if (p instanceof LiteralConverter) {
207-
buf.append(escapeRegexMetaCharacters(p.convert(null)));
208-
} else if (p instanceof IntegerTokenConverter) {
209-
buf.append("(\\d+)");
210-
} else if (p instanceof DateTokenConverter) {
211-
buf.append(escapeRegexMetaCharacters(p.convert(date)));
212-
}
213-
p = p.getNext();
214-
}
215-
return buf.toString();
205+
return toRegexForFixedDate(date.toInstant());
216206
}
217207

218-
219208
/**
220209
* Given date, convert this instance to a regular expression.
221210
*

‎logback-core/src/main/java/ch/qos/logback/core/util/CharSequenceToRegexMapper.java‎

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,37 @@
1414
package ch.qos.logback.core.util;
1515

1616
import java.text.DateFormatSymbols;
17+
import java.time.Month;
18+
import java.time.format.DateTimeFormatter;
19+
import java.util.Locale;
1720

1821
/**
1922
* This class supports mapping character sequences to regular expressions as
20-
* appropriate for SimpleDateFormatter.
23+
* appropriate for DateTimeFormatter.
2124
*
2225
* @author Ceki
2326
*/
2427
class CharSequenceToRegexMapper {
2528

26-
DateFormatSymbols symbols = DateFormatSymbols.getInstance();
29+
final Locale locale;
30+
final DateFormatSymbols symbols;
31+
32+
/**
33+
* Create a mapper using the symbols of the default locale.
34+
*/
35+
CharSequenceToRegexMapper() {
36+
this(Locale.getDefault(Locale.Category.FORMAT));
37+
}
38+
39+
/**
40+
* Create a mapper using the symbols of the given locale.
41+
*
42+
* @param locale the locale whose month, weekday and am/pm symbols are used
43+
*/
44+
CharSequenceToRegexMapper(Locale locale) {
45+
this.locale = locale;
46+
this.symbols = DateFormatSymbols.getInstance(locale);
47+
}
2748

2849
String toRegex(CharSequenceState css) {
2950
final int occurrences = css.occurrences;
@@ -39,6 +60,11 @@ else if (occurrences == 3)
3960
return getRegexForShortMonths();
4061
else
4162
return getRegexForLongMonths();
63+
case 'L':
64+
if (occurrences <= 2)
65+
return number(occurrences);
66+
else
67+
return getRegexForStandaloneMonths(occurrences);
4268
case 'y':
4369
case 'w':
4470
case 'W':
@@ -105,6 +131,18 @@ String getRegexForShortMonths() {
105131
return symbolArrayToRegex(symbols.getShortMonths());
106132
}
107133

134+
// DateFormatSymbols does not provide stand-alone month names. They are obtained
135+
// by formatting each month with the stand-alone pattern itself.
136+
private String getRegexForStandaloneMonths(int occurrences) {
137+
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("L".repeat(occurrences), locale);
138+
Month[] months = Month.values();
139+
String[] monthNames = new String[months.length];
140+
for (int i = 0; i < months.length; i++) {
141+
monthNames[i] = dtf.format(months[i]);
142+
}
143+
return symbolArrayToRegex(monthNames);
144+
}
145+
108146
private String symbolArrayToRegex(String[] symbolArray) {
109147
int[] minMax = findMinMaxLengthsInSymbols(symbolArray);
110148
return ".{" + minMax[0] + "," + minMax[1] + "}";

‎logback-core/src/main/java/ch/qos/logback/core/util/DatePatternToRegexUtil.java‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,43 @@
1313
*/
1414
package ch.qos.logback.core.util;
1515

16-
import java.text.SimpleDateFormat;
16+
import java.time.format.DateTimeFormatter;
1717
import java.util.ArrayList;
1818
import java.util.List;
19+
import java.util.Locale;
1920

2021
/**
2122
* This class is concerned with computing a regex corresponding to a date
22-
* pattern (in {@link SimpleDateFormat} format).
23+
* pattern (in {@link DateTimeFormatter} format).
2324
*
2425
* @author Ceki G&uuml;lc&uuml;
2526
*/
2627
public class DatePatternToRegexUtil {
2728

2829
final String datePattern;
2930
final int datePatternLength;
30-
final CharSequenceToRegexMapper regexMapper = new CharSequenceToRegexMapper();
31+
final CharSequenceToRegexMapper regexMapper;
3132

33+
/**
34+
* Create an instance using the symbols of the default locale.
35+
*
36+
* @param datePattern date pattern in {@link DateTimeFormatter} format
37+
*/
3238
public DatePatternToRegexUtil(String datePattern) {
39+
this(datePattern, Locale.getDefault(Locale.Category.FORMAT));
40+
}
41+
42+
/**
43+
* Create an instance using the symbols of the given locale.
44+
*
45+
* @param datePattern date pattern in {@link DateTimeFormatter} format
46+
* @param locale the locale whose month, weekday and am/pm symbols are used
47+
* @since 1.7.0
48+
*/
49+
public DatePatternToRegexUtil(String datePattern, Locale locale) {
3350
this.datePattern = datePattern;
3451
datePatternLength = datePattern.length();
52+
regexMapper = new CharSequenceToRegexMapper(locale);
3553
}
3654

3755
public String toRegex() {

‎logback-core/src/test/java/ch/qos/logback/core/util/CharSequenceToRegexMapperTest.java‎

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
*/
1414
package ch.qos.logback.core.util;
1515

16-
import org.junit.jupiter.api.AfterEach;
1716
import org.junit.jupiter.api.Assertions;
1817
import org.junit.jupiter.api.Disabled;
1918
import org.junit.jupiter.api.Test;
@@ -25,12 +24,6 @@
2524

2625
public class CharSequenceToRegexMapperTest {
2726
static Locale KO_LOCALE = new Locale("ko", "KR");
28-
Locale oldLocale = Locale.getDefault();
29-
30-
@AfterEach
31-
public void tearDown() {
32-
Locale.setDefault(oldLocale);
33-
}
3427

3528
@Test
3629
public void findMinMaxLengthsInSymbolsWithTrivialInputs() {
@@ -48,22 +41,47 @@ public void emptyStringValuesShouldBeIgnoredByFindMinMaxLengthsInSymbols() {
4841
assertEquals(3, results[1]);
4942
}
5043

44+
@Test
45+
public void localeArgumentIsHonored() {
46+
CharSequenceToRegexMapper usMapper = new CharSequenceToRegexMapper(Locale.US);
47+
// "Monday" is the shortest US weekday, "Wednesday" the longest
48+
assertEquals(".{6,9}", usMapper.toRegex(makeState('E', 4)));
49+
50+
CharSequenceToRegexMapper germanMapper = new CharSequenceToRegexMapper(Locale.GERMANY);
51+
// "Montag" is the shortest German weekday, "Donnerstag" the longest
52+
assertEquals(".{6,10}", germanMapper.toRegex(makeState('E', 4)));
53+
}
54+
55+
@Test
56+
public void defaultConstructorUsesDefaultLocale() {
57+
CharSequenceToRegexMapper defaultMapper = new CharSequenceToRegexMapper();
58+
CharSequenceToRegexMapper explicitMapper = new CharSequenceToRegexMapper(
59+
Locale.getDefault(Locale.Category.FORMAT));
60+
assertEquals(explicitMapper.symbols, defaultMapper.symbols);
61+
}
62+
63+
private CharSequenceState makeState(char c, int occurrences) {
64+
CharSequenceState state = new CharSequenceState(c);
65+
for (int i = 1; i < occurrences; i++) {
66+
state.incrementOccurrences();
67+
}
68+
return state;
69+
}
70+
5171
@Test
5272
@Disabled
5373
public void noneOfTheSymbolsAreOfZeroLengthForKorean() {
54-
Locale.setDefault(KO_LOCALE);
55-
noneOfTheSymbolsAreOfZeroLength();
74+
noneOfTheSymbolsAreOfZeroLength(KO_LOCALE);
5675
}
5776

5877
@Test
5978
@Disabled
6079
public void noneOfTheSymbolsAreOfZeroLengthForSwiss() {
61-
Locale.setDefault(new Locale("fr", "CH"));
62-
noneOfTheSymbolsAreOfZeroLength();
80+
noneOfTheSymbolsAreOfZeroLength(new Locale("fr", "CH"));
6381
}
6482

65-
private void noneOfTheSymbolsAreOfZeroLength() {
66-
DateFormatSymbols dateFormatSymbols = DateFormatSymbols.getInstance();
83+
private void noneOfTheSymbolsAreOfZeroLength(Locale locale) {
84+
DateFormatSymbols dateFormatSymbols = new CharSequenceToRegexMapper(locale).symbols;
6785
// checkEmptyString(dateFormatSymbols.getShortMonths(), "ShortMonths");
6886
// checkEmptyString(dateFormatSymbols.getMonths(), "Months");
6987
checkEmptyString(dateFormatSymbols.getShortWeekdays(), "ShortWeekdays");

‎logback-core/src/test/java/ch/qos/logback/core/util/DatePatternToRegexTest.java‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
*/
1414
package ch.qos.logback.core.util;
1515

16-
import java.text.SimpleDateFormat;
16+
import java.time.ZoneId;
17+
import java.time.format.DateTimeFormatter;
1718
import java.util.ArrayList;
1819
import java.util.Calendar;
1920
import java.util.List;
@@ -115,9 +116,10 @@ void doTest(String datePattern, Calendar calendar, Locale locale) {
115116
}
116117

117118
try {
118-
SimpleDateFormat sdf = new SimpleDateFormat(datePattern);
119+
// as in CachingDateFormatter
120+
DateTimeFormatter dtf = DateTimeFormatter.ofPattern(datePattern).withZone(ZoneId.systemDefault());
119121
DateTokenConverter<?> dtc = makeDTC(datePattern);
120-
verify(sdf, calendar, dtc);
122+
verify(dtf, calendar, dtc);
121123
} finally {
122124
if (locale != null)
123125
Locale.setDefault(oldDefaultLocale);
@@ -131,8 +133,8 @@ void doTest(String datePattern, Calendar calendar, Locale locale) {
131133
// doTest(datePattern, calendar, false);
132134
// }
133135

134-
void verify(SimpleDateFormat sdf, Calendar calendar, DateTokenConverter<?> dtc) {
135-
String expected = sdf.format(calendar.getTime());
136+
void verify(DateTimeFormatter dtf, Calendar calendar, DateTokenConverter<?> dtc) {
137+
String expected = dtf.format(calendar.toInstant());
136138
// if (slashified) {
137139
// expected = expected.replace('\\', '/');
138140
// }

0 commit comments

Comments
 (0)