24
24
import org .elasticsearch .common .time .DateFormatter ;
25
25
import org .elasticsearch .common .time .DateFormatters ;
26
26
import org .elasticsearch .common .time .DateMathParser ;
27
+ import org .elasticsearch .common .util .LocaleUtils ;
27
28
import org .elasticsearch .test .ESTestCase ;
28
29
import org .joda .time .DateTime ;
29
30
import org .joda .time .DateTimeZone ;
30
31
import org .joda .time .format .ISODateTimeFormat ;
32
+ import org .junit .BeforeClass ;
31
33
34
+ import java .time .Instant ;
32
35
import java .time .LocalDateTime ;
33
36
import java .time .ZoneId ;
34
37
import java .time .ZoneOffset ;
@@ -46,9 +49,47 @@ public class JavaJodaTimeDuellingTests extends ESTestCase {
46
49
protected boolean enableWarningsCheck () {
47
50
return false ;
48
51
}
52
+ @ BeforeClass
53
+ public static void checkJvmProperties (){
54
+ assert ("SPI,COMPAT" .equals (System .getProperty ("java.locale.providers" )))
55
+ : "`-Djava.locale.providers=SPI,COMPAT` needs to be set" ;
56
+ }
57
+
58
+ public void testTimezoneParsing () {
59
+ /** this testcase won't work in joda. See comment in {@link #testPartialTimeParsing()}
60
+ * assertSameDateAs("2016-11-30T+01", "strict_date_optional_time", "strict_date_optional_time");
61
+ */
62
+ assertSameDateAs ("2016-11-30T00+01" , "strict_date_optional_time" , "strict_date_optional_time" );
63
+ assertSameDateAs ("2016-11-30T00+0100" , "strict_date_optional_time" , "strict_date_optional_time" );
64
+ assertSameDateAs ("2016-11-30T00+01:00" , "strict_date_optional_time" , "strict_date_optional_time" );
65
+ }
66
+
67
+ public void testPartialTimeParsing () {
68
+ /*
69
+ This does not work in Joda as it reports 2016-11-30T01:00:00Z
70
+ because StrictDateOptionalTime confuses +01 with an hour (which is a signed fixed length digit)
71
+ assertSameDateAs("2016-11-30T+01", "strict_date_optional_time", "strict_date_optional_time");
72
+ ES java.time implementation does not suffer from this,
73
+ but we intentionally not allow parsing timezone without an time part as it is not allowed in iso8601
74
+ */
75
+ assertJavaTimeParseException ("2016-11-30T+01" ,"strict_date_optional_time" );
76
+
77
+ assertSameDateAs ("2016-11-30T12+01" , "strict_date_optional_time" , "strict_date_optional_time" );
78
+ assertSameDateAs ("2016-11-30T12:00+01" , "strict_date_optional_time" , "strict_date_optional_time" );
79
+ assertSameDateAs ("2016-11-30T12:00:00+01" , "strict_date_optional_time" , "strict_date_optional_time" );
80
+ assertSameDateAs ("2016-11-30T12:00:00.000+01" , "strict_date_optional_time" , "strict_date_optional_time" );
81
+
82
+ //without timezone
83
+ assertSameDateAs ("2016-11-30T" , "strict_date_optional_time" , "strict_date_optional_time" );
84
+ assertSameDateAs ("2016-11-30T12" , "strict_date_optional_time" , "strict_date_optional_time" );
85
+ assertSameDateAs ("2016-11-30T12:00" , "strict_date_optional_time" , "strict_date_optional_time" );
86
+ assertSameDateAs ("2016-11-30T12:00:00" , "strict_date_optional_time" , "strict_date_optional_time" );
87
+ assertSameDateAs ("2016-11-30T12:00:00.000" , "strict_date_optional_time" , "strict_date_optional_time" );
88
+ }
89
+
49
90
// date_optional part of a parser names "strict_date_optional_time" or "date_optional"time
50
91
// means that date part can be partially parsed.
51
- public void testPartialParsing () {
92
+ public void testPartialDateParsing () {
52
93
assertSameDateAs ("2001" , "strict_date_optional_time_nanos" , "strict_date_optional_time" );
53
94
assertSameDateAs ("2001-01" , "strict_date_optional_time_nanos" , "strict_date_optional_time" );
54
95
assertSameDateAs ("2001-01-01" , "strict_date_optional_time_nanos" , "strict_date_optional_time" );
@@ -167,19 +208,17 @@ public void testCustomTimeFormats() {
167
208
assertSameDate ("Nov 24 01:29:01 -0800" , "MMM dd HH:mm:ss Z" );
168
209
}
169
210
170
- // this test requires tests to run with -Djava.locale.providers=COMPAT in order to work
171
- // public void testCustomLocales() {
172
- //
173
- // // also ensure that locale based dates are the same
174
- // assertSameDate("Di., 05 Dez. 2000 02:55:00 -0800", "E, d MMM yyyy HH:mm:ss Z", LocaleUtils.parse("de"));
175
- // assertSameDate("Mi., 06 Dez. 2000 02:55:00 -0800", "E, d MMM yyyy HH:mm:ss Z", LocaleUtils.parse("de"));
176
- // assertSameDate("Do., 07 Dez. 2000 00:00:00 -0800", "E, d MMM yyyy HH:mm:ss Z", LocaleUtils.parse("de"));
177
- // assertSameDate("Fr., 08 Dez. 2000 00:00:00 -0800", "E, d MMM yyyy HH:mm:ss Z", LocaleUtils.parse("de"));
178
- //
179
- // DateTime dateTimeNow = DateTime.now(DateTimeZone.UTC);
180
- // ZonedDateTime javaTimeNow = Instant.ofEpochMilli(dateTimeNow.getMillis()).atZone(ZoneOffset.UTC);
181
- // assertSamePrinterOutput("E, d MMM yyyy HH:mm:ss Z", LocaleUtils.parse("de"), javaTimeNow, dateTimeNow);
182
- // }
211
+ public void testCustomLocales () {
212
+ // also ensure that locale based dates are the same
213
+ assertSameDate ("Di, 05 Dez 2000 02:55:00 -0800" , "E, d MMM yyyy HH:mm:ss Z" , LocaleUtils .parse ("de" ));
214
+ assertSameDate ("Mi, 06 Dez 2000 02:55:00 -0800" , "E, d MMM yyyy HH:mm:ss Z" , LocaleUtils .parse ("de" ));
215
+ assertSameDate ("Do, 07 Dez 2000 00:00:00 -0800" , "E, d MMM yyyy HH:mm:ss Z" , LocaleUtils .parse ("de" ));
216
+ assertSameDate ("Fr, 08 Dez 2000 00:00:00 -0800" , "E, d MMM yyyy HH:mm:ss Z" , LocaleUtils .parse ("de" ));
217
+
218
+ DateTime dateTimeNow = DateTime .now (DateTimeZone .UTC );
219
+ ZonedDateTime javaTimeNow = Instant .ofEpochMilli (dateTimeNow .getMillis ()).atZone (ZoneOffset .UTC );
220
+ assertSamePrinterOutput ("E, d MMM yyyy HH:mm:ss Z" , javaTimeNow , dateTimeNow , LocaleUtils .parse ("de" ));
221
+ }
183
222
184
223
public void testDuellingFormatsValidParsing () {
185
224
assertSameDate ("1522332219" , "epoch_second" );
@@ -859,9 +898,28 @@ public void testParsingMissingTimezone() {
859
898
}
860
899
861
900
private void assertSamePrinterOutput (String format , ZonedDateTime javaDate , DateTime jodaDate ) {
901
+ DateFormatter dateFormatter = DateFormatter .forPattern (format );
902
+ JodaDateFormatter jodaDateFormatter = Joda .forPattern (format );
903
+
904
+ assertSamePrinterOutput (format , javaDate , jodaDate , dateFormatter , jodaDateFormatter );
905
+ }
906
+
907
+ private void assertSamePrinterOutput (String format , ZonedDateTime javaDate , DateTime jodaDate , Locale locale ) {
908
+ DateFormatter dateFormatter = DateFormatter .forPattern (format ).withLocale (locale );
909
+ DateFormatter jodaDateFormatter = Joda .forPattern (format ).withLocale (locale );
910
+
911
+ assertSamePrinterOutput (format , javaDate , jodaDate , dateFormatter , jodaDateFormatter );
912
+ }
913
+
914
+ private void assertSamePrinterOutput (String format ,
915
+ ZonedDateTime javaDate ,
916
+ DateTime jodaDate ,
917
+ DateFormatter dateFormatter ,
918
+ DateFormatter jodaDateFormatter ) {
919
+ String javaTimeOut = dateFormatter .format (javaDate );
920
+ String jodaTimeOut = jodaDateFormatter .formatJoda (jodaDate );
921
+
862
922
assertThat (jodaDate .getMillis (), is (javaDate .toInstant ().toEpochMilli ()));
863
- String javaTimeOut = DateFormatter .forPattern (format ).format (javaDate );
864
- String jodaTimeOut = Joda .forPattern (format ).formatJoda (jodaDate );
865
923
866
924
if (JavaVersion .current ().getVersion ().get (0 ) == 8 && javaTimeOut .endsWith (".0" )
867
925
&& (format .equals ("epoch_second" ) || format .equals ("epoch_millis" ))) {
@@ -880,6 +938,12 @@ private void assertSameDate(String input, String format) {
880
938
assertSameDate (input , format , jodaFormatter , javaFormatter );
881
939
}
882
940
941
+ private void assertSameDate (String input , String format , Locale locale ) {
942
+ DateFormatter jodaFormatter = Joda .forPattern (format ).withLocale (locale );
943
+ DateFormatter javaFormatter = DateFormatter .forPattern (format ).withLocale (locale );
944
+ assertSameDate (input , format , jodaFormatter , javaFormatter );
945
+ }
946
+
883
947
private void assertSameDate (String input , String format , DateFormatter jodaFormatter , DateFormatter javaFormatter ) {
884
948
DateTime jodaDateTime = jodaFormatter .parseJoda (input );
885
949
0 commit comments