37
37
import static org .hamcrest .Matchers .not ;
38
38
import static org .hamcrest .Matchers .nullValue ;
39
39
import static org .hamcrest .Matchers .sameInstance ;
40
+ import static org .hamcrest .Matchers .startsWith ;
40
41
41
42
public class DateFormattersTests extends ESTestCase {
42
43
@@ -55,35 +56,50 @@ public void testEpochMillisParser() {
55
56
assertThat (instant .getEpochSecond (), is (0L ));
56
57
assertThat (instant .getNano (), is (0 ));
57
58
}
59
+ {
60
+ Instant instant = Instant .from (formatter .parse ("123.123456" ));
61
+ assertThat (instant .getEpochSecond (), is (0L ));
62
+ assertThat (instant .getNano (), is (123123456 ));
63
+ }
58
64
}
59
65
60
66
public void testEpochMilliParser () {
61
- DateFormatter formatter = DateFormatters .forPattern ("epoch_millis " );
67
+ DateFormatter formatter = DateFormatter .forPattern ("8epoch_millis " );
62
68
DateTimeParseException e = expectThrows (DateTimeParseException .class , () -> formatter .parse ("invalid" ));
63
69
assertThat (e .getMessage (), containsString ("could not be parsed" ));
64
70
65
71
e = expectThrows (DateTimeParseException .class , () -> formatter .parse ("123.1234567" ));
66
- assertThat (e .getMessage (), containsString ("unparsed text found at index 3 " ));
72
+ assertThat (e .getMessage (), containsString ("unparsed text found" ));
67
73
}
68
74
69
75
// this is not in the duelling tests, because the epoch second parser in joda time drops the milliseconds after the comma
70
76
// but is able to parse the rest
71
77
// as this feature is supported it also makes sense to make it exact
72
- public void testEpochSecondParser () {
78
+ public void testEpochSecondParserWithFraction () {
73
79
DateFormatter formatter = DateFormatters .forPattern ("epoch_second" );
74
80
75
- DateTimeParseException e = expectThrows (DateTimeParseException .class , () -> formatter .parse ("1234.1" ));
76
- assertThat (e .getMessage (), is ("Text '1234.1' could not be parsed, unparsed text found at index 4" ));
77
- e = expectThrows (DateTimeParseException .class , () -> formatter .parse ("1234." ));
78
- assertThat (e .getMessage (), is ("Text '1234.' could not be parsed, unparsed text found at index 4" ));
79
- e = expectThrows (DateTimeParseException .class , () -> formatter .parse ("abc" ));
81
+ TemporalAccessor accessor = formatter .parse ("1234.1" );
82
+ Instant instant = DateFormatters .toZonedDateTime (accessor ).toInstant ();
83
+ assertThat (instant .getEpochSecond (), is (1234L ));
84
+ assertThat (DateFormatters .toZonedDateTime (accessor ).toInstant ().getNano (), is (100_000_000 ));
85
+
86
+ accessor = formatter .parse ("1234" );
87
+ instant = DateFormatters .toZonedDateTime (accessor ).toInstant ();
88
+ assertThat (instant .getEpochSecond (), is (1234L ));
89
+ assertThat (instant .getNano (), is (0 ));
90
+
91
+ DateTimeParseException e = expectThrows (DateTimeParseException .class , () -> formatter .parse ("abc" ));
80
92
assertThat (e .getMessage (), is ("Text 'abc' could not be parsed, unparsed text found at index 0" ));
93
+
81
94
e = expectThrows (DateTimeParseException .class , () -> formatter .parse ("1234.abc" ));
82
- assertThat (e .getMessage (), is ("Text '1234.abc' could not be parsed, unparsed text found at index 4" ));
95
+ assertThat (e .getMessage (), is ("Text '1234.abc' could not be parsed, unparsed text found at index 5" ));
96
+
97
+ e = expectThrows (DateTimeParseException .class , () -> formatter .parse ("1234.1234567890" ));
98
+ assertThat (e .getMessage (), is ("Text '1234.1234567890' could not be parsed, unparsed text found at index 14" ));
83
99
}
84
100
85
101
public void testEpochMilliParsersWithDifferentFormatters () {
86
- DateFormatter formatter = DateFormatter .forPattern ("strict_date_optional_time ||epoch_millis" );
102
+ DateFormatter formatter = DateFormatter .forPattern ("8strict_date_optional_time ||epoch_millis" );
87
103
TemporalAccessor accessor = formatter .parse ("123" );
88
104
assertThat (DateFormatters .toZonedDateTime (accessor ).toInstant ().toEpochMilli (), is (123L ));
89
105
assertThat (formatter .pattern (), is ("strict_date_optional_time||epoch_millis" ));
@@ -148,6 +164,26 @@ public void testForceJava8() {
148
164
assertThat (formatter , instanceOf (JavaDateFormatter .class ));
149
165
}
150
166
167
+ public void testEpochFormatting () {
168
+ long seconds = randomLongBetween (0 , 130L * 365 * 86400 ); // from 1970 epoch till around 2100
169
+ long nanos = randomLongBetween (0 , 999_999_999L );
170
+ Instant instant = Instant .ofEpochSecond (seconds , nanos );
171
+
172
+ DateFormatter millisFormatter = DateFormatter .forPattern ("8epoch_millis" );
173
+ String millis = millisFormatter .format (instant );
174
+ Instant millisInstant = Instant .from (millisFormatter .parse (millis ));
175
+ assertThat (millisInstant .toEpochMilli (), is (instant .toEpochMilli ()));
176
+ assertThat (millisFormatter .format (Instant .ofEpochSecond (42 , 0 )), is ("42000" ));
177
+ assertThat (millisFormatter .format (Instant .ofEpochSecond (42 , 123456789L )), is ("42123.456789" ));
178
+
179
+ DateFormatter secondsFormatter = DateFormatter .forPattern ("8epoch_second" );
180
+ String formattedSeconds = secondsFormatter .format (instant );
181
+ Instant secondsInstant = Instant .from (secondsFormatter .parse (formattedSeconds ));
182
+ assertThat (secondsInstant .getEpochSecond (), is (instant .getEpochSecond ()));
183
+
184
+ assertThat (secondsFormatter .format (Instant .ofEpochSecond (42 , 0 )), is ("42" ));
185
+ }
186
+
151
187
public void testParsingStrictNanoDates () {
152
188
DateFormatter formatter = DateFormatters .forPattern ("strict_date_optional_time_nanos" );
153
189
formatter .format (formatter .parse ("2016-01-01T00:00:00.000" ));
@@ -185,6 +221,7 @@ public void testRoundupFormatterWithEpochDates() {
185
221
}
186
222
187
223
private void assertRoundupFormatter (String format , String input , long expectedMilliSeconds ) {
224
+ assertThat (format , startsWith ("8" ));
188
225
JavaDateFormatter dateFormatter = (JavaDateFormatter ) DateFormatter .forPattern (format );
189
226
dateFormatter .parse (input );
190
227
DateTimeFormatter roundUpFormatter = dateFormatter .getRoundupParser ();
0 commit comments