@@ -46,9 +46,42 @@ public class JavaJodaTimeDuellingTests extends ESTestCase {
46
46
protected boolean enableWarningsCheck () {
47
47
return false ;
48
48
}
49
+
50
+ public void testTimezoneParsing () {
51
+ /** this testcase won't work in joda. See comment in {@link #testPartialTimeParsing()}
52
+ * assertSameDateAs("2016-11-30T+01", "strict_date_optional_time", "strict_date_optional_time");
53
+ */
54
+ assertSameDateAs ("2016-11-30T00+01" , "strict_date_optional_time" , "strict_date_optional_time" );
55
+ assertSameDateAs ("2016-11-30T00+0100" , "strict_date_optional_time" , "strict_date_optional_time" );
56
+ assertSameDateAs ("2016-11-30T00+01:00" , "strict_date_optional_time" , "strict_date_optional_time" );
57
+ }
58
+
59
+ public void testPartialTimeParsing () {
60
+ /*
61
+ This does not work in Joda as it reports 2016-11-30T01:00:00Z
62
+ because StrictDateOptionalTime confuses +01 with an hour (which is a signed fixed length digit)
63
+ assertSameDateAs("2016-11-30T+01", "strict_date_optional_time", "strict_date_optional_time");
64
+ ES java.time implementation does not suffer from this,
65
+ but we intentionally not allow parsing timezone without an time part as it is not allowed in iso8601
66
+ */
67
+ assertJavaTimeParseException ("2016-11-30T+01" ,"strict_date_optional_time" );
68
+
69
+ assertSameDateAs ("2016-11-30T12+01" , "strict_date_optional_time" , "strict_date_optional_time" );
70
+ assertSameDateAs ("2016-11-30T12:00+01" , "strict_date_optional_time" , "strict_date_optional_time" );
71
+ assertSameDateAs ("2016-11-30T12:00:00+01" , "strict_date_optional_time" , "strict_date_optional_time" );
72
+ assertSameDateAs ("2016-11-30T12:00:00.000+01" , "strict_date_optional_time" , "strict_date_optional_time" );
73
+
74
+ //without timezone
75
+ assertSameDateAs ("2016-11-30T" , "strict_date_optional_time" , "strict_date_optional_time" );
76
+ assertSameDateAs ("2016-11-30T12" , "strict_date_optional_time" , "strict_date_optional_time" );
77
+ assertSameDateAs ("2016-11-30T12:00" , "strict_date_optional_time" , "strict_date_optional_time" );
78
+ assertSameDateAs ("2016-11-30T12:00:00" , "strict_date_optional_time" , "strict_date_optional_time" );
79
+ assertSameDateAs ("2016-11-30T12:00:00.000" , "strict_date_optional_time" , "strict_date_optional_time" );
80
+ }
81
+
49
82
// date_optional part of a parser names "strict_date_optional_time" or "date_optional"time
50
83
// means that date part can be partially parsed.
51
- public void testPartialParsing () {
84
+ public void testPartialDateParsing () {
52
85
assertSameDateAs ("2001" , "strict_date_optional_time_nanos" , "strict_date_optional_time" );
53
86
assertSameDateAs ("2001-01" , "strict_date_optional_time_nanos" , "strict_date_optional_time" );
54
87
assertSameDateAs ("2001-01-01" , "strict_date_optional_time_nanos" , "strict_date_optional_time" );
@@ -883,9 +916,28 @@ public void testParsingMissingTimezone() {
883
916
}
884
917
885
918
private void assertSamePrinterOutput (String format , ZonedDateTime javaDate , DateTime jodaDate ) {
919
+ DateFormatter dateFormatter = DateFormatter .forPattern (format );
920
+ JodaDateFormatter jodaDateFormatter = Joda .forPattern (format );
921
+
922
+ assertSamePrinterOutput (format , javaDate , jodaDate , dateFormatter , jodaDateFormatter );
923
+ }
924
+
925
+ private void assertSamePrinterOutput (String format , ZonedDateTime javaDate , DateTime jodaDate , Locale locale ) {
926
+ DateFormatter dateFormatter = DateFormatter .forPattern (format ).withLocale (locale );
927
+ DateFormatter jodaDateFormatter = Joda .forPattern (format ).withLocale (locale );
928
+
929
+ assertSamePrinterOutput (format , javaDate , jodaDate , dateFormatter , jodaDateFormatter );
930
+ }
931
+
932
+ private void assertSamePrinterOutput (String format ,
933
+ ZonedDateTime javaDate ,
934
+ DateTime jodaDate ,
935
+ DateFormatter dateFormatter ,
936
+ DateFormatter jodaDateFormatter ) {
937
+ String javaTimeOut = dateFormatter .format (javaDate );
938
+ String jodaTimeOut = jodaDateFormatter .formatJoda (jodaDate );
939
+
886
940
assertThat (jodaDate .getMillis (), is (javaDate .toInstant ().toEpochMilli ()));
887
- String javaTimeOut = DateFormatter .forPattern (format ).format (javaDate );
888
- String jodaTimeOut = Joda .forPattern (format ).formatJoda (jodaDate );
889
941
890
942
if (JavaVersion .current ().getVersion ().get (0 ) == 8 && javaTimeOut .endsWith (".0" )
891
943
&& (format .equals ("epoch_second" ) || format .equals ("epoch_millis" ))) {
@@ -904,6 +956,12 @@ private void assertSameDate(String input, String format) {
904
956
assertSameDate (input , format , jodaFormatter , javaFormatter );
905
957
}
906
958
959
+ private void assertSameDate (String input , String format , Locale locale ) {
960
+ DateFormatter jodaFormatter = Joda .forPattern (format ).withLocale (locale );
961
+ DateFormatter javaFormatter = DateFormatter .forPattern (format ).withLocale (locale );
962
+ assertSameDate (input , format , jodaFormatter , javaFormatter );
963
+ }
964
+
907
965
private void assertSameDate (String input , String format , DateFormatter jodaFormatter , DateFormatter javaFormatter ) {
908
966
DateTime jodaDateTime = jodaFormatter .parseJoda (input );
909
967
0 commit comments