|
20 | 20 | import org.elasticsearch.index.mapper.DateFieldMapper.Resolution; |
21 | 21 | import org.elasticsearch.test.ESTestCase; |
22 | 22 |
|
| 23 | +import java.time.ZoneId; |
23 | 24 | import java.time.ZoneOffset; |
24 | 25 | import java.util.ArrayList; |
25 | 26 | import java.util.List; |
@@ -245,4 +246,103 @@ public void testBadIp() { |
245 | 246 | assertThat(e.getMessage(), containsString("mapping")); |
246 | 247 | assertThat(e.getMessage(), containsString("IP address")); |
247 | 248 | } |
| 249 | + |
| 250 | + /** |
| 251 | + * <p>Test that if we format a datetime using the `epoch_second` format, we can then parse the result |
| 252 | + * back into the same value we started with, for all timezones</p> |
| 253 | + * |
| 254 | + * <p>"Why would you put a timezone on epoch_seconds? it doesn't make sense" you might be asking. |
| 255 | + * I was. The key to remember here is that we use the same time zone parameter for date histogram |
| 256 | + * bucket generation and formatting. So, while asking for (e.g.) epoch_seconds in New York time |
| 257 | + * is nonsensical, asking for day-buckets in New York time with the keys formatted in epoch_seconds |
| 258 | + * is pretty standard.</p> |
| 259 | + * |
| 260 | + * <p>This test validates that if someone does this in composite, we can then parse the after key |
| 261 | + * we generated into the correct value. Parsing also happens on missing values.</p> |
| 262 | + */ |
| 263 | + public void testParseEpochSecondsTimezone() { |
| 264 | + ZoneId zone = randomZone(); |
| 265 | + DocValueFormat.DateTime formatter = new DocValueFormat.DateTime( |
| 266 | + DateFormatter.forPattern("epoch_second"), |
| 267 | + zone, |
| 268 | + Resolution.MILLISECONDS |
| 269 | + ); |
| 270 | + long millis = randomNonNegativeLong(); |
| 271 | + // Convert to seconds |
| 272 | + millis -= (millis % 1000); |
| 273 | + assertEquals( |
| 274 | + "failed formatting for tz " + zone, |
| 275 | + millis, |
| 276 | + formatter.parseLong(formatter.format(millis), false, () -> { throw new UnsupportedOperationException("don't use now"); }) |
| 277 | + ); |
| 278 | + } |
| 279 | + |
| 280 | + public void testParseEpochMillisTimezone() { |
| 281 | + ZoneId zone = randomZone(); |
| 282 | + DocValueFormat.DateTime formatter = new DocValueFormat.DateTime( |
| 283 | + DateFormatter.forPattern("epoch_millis"), |
| 284 | + zone, |
| 285 | + Resolution.MILLISECONDS |
| 286 | + ); |
| 287 | + long millis = randomNonNegativeLong(); |
| 288 | + assertEquals( |
| 289 | + "failed formatting for tz " + zone, |
| 290 | + millis, |
| 291 | + formatter.parseLong(formatter.format(millis), false, () -> { throw new UnsupportedOperationException("don't use now"); }) |
| 292 | + ); |
| 293 | + } |
| 294 | + |
| 295 | + |
| 296 | + public void testDateHMSTimezone() { |
| 297 | + DocValueFormat.DateTime tokyo = new DocValueFormat.DateTime( |
| 298 | + DateFormatter.forPattern("date_hour_minute_second"), |
| 299 | + ZoneOffset.ofHours(9), |
| 300 | + Resolution.MILLISECONDS |
| 301 | + ); |
| 302 | + DocValueFormat.DateTime utc = new DocValueFormat.DateTime( |
| 303 | + DateFormatter.forPattern("date_hour_minute_second"), |
| 304 | + ZoneOffset.UTC, |
| 305 | + Resolution.MILLISECONDS |
| 306 | + ); |
| 307 | + long millis = 1622567918000L; |
| 308 | + assertEquals("2021-06-01T17:18:38", utc.format(millis)); |
| 309 | + assertEquals("2021-06-02T02:18:38", tokyo.format(millis)); |
| 310 | + assertEquals( |
| 311 | + "couldn't parse UTC", |
| 312 | + millis, |
| 313 | + utc.parseLong(utc.format(millis), false, () -> { throw new UnsupportedOperationException("don't use now"); }) |
| 314 | + ); |
| 315 | + assertEquals( |
| 316 | + "couldn't parse Tokyo", |
| 317 | + millis, |
| 318 | + tokyo.parseLong(tokyo.format(millis), false, () -> { throw new UnsupportedOperationException("don't use now"); }) |
| 319 | + ); |
| 320 | + } |
| 321 | + |
| 322 | + public void testDateTimeWithTimezone() { |
| 323 | + |
| 324 | + DocValueFormat.DateTime tokyo = new DocValueFormat.DateTime( |
| 325 | + DateFormatter.forPattern("basic_date_time_no_millis"), |
| 326 | + ZoneOffset.ofHours(9), |
| 327 | + Resolution.MILLISECONDS |
| 328 | + ); |
| 329 | + DocValueFormat.DateTime utc = new DocValueFormat.DateTime( |
| 330 | + DateFormatter.forPattern("basic_date_time_no_millis"), |
| 331 | + ZoneOffset.UTC, |
| 332 | + Resolution.MILLISECONDS |
| 333 | + ); |
| 334 | + long millis = 1622567918000L; |
| 335 | + assertEquals("20210601T171838Z", utc.format(millis)); |
| 336 | + assertEquals("20210602T021838+09:00", tokyo.format(millis)); |
| 337 | + assertEquals( |
| 338 | + "couldn't parse UTC", |
| 339 | + millis, |
| 340 | + utc.parseLong(utc.format(millis), false, () -> { throw new UnsupportedOperationException("don't use now"); }) |
| 341 | + ); |
| 342 | + assertEquals( |
| 343 | + "couldn't parse Tokyo", |
| 344 | + millis, |
| 345 | + tokyo.parseLong(tokyo.format(millis), false, () -> { throw new UnsupportedOperationException("don't use now"); }) |
| 346 | + ); |
| 347 | + } |
248 | 348 | } |
0 commit comments