|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.common.time; |
| 21 | + |
| 22 | +import org.elasticsearch.test.ESTestCase; |
| 23 | + |
| 24 | +import java.time.ZoneId; |
| 25 | +import java.time.ZonedDateTime; |
| 26 | +import java.time.format.DateTimeParseException; |
| 27 | +import java.time.temporal.TemporalAccessor; |
| 28 | + |
| 29 | +import static org.hamcrest.Matchers.containsString; |
| 30 | +import static org.hamcrest.Matchers.is; |
| 31 | + |
| 32 | +public class DateFormattersTests extends ESTestCase { |
| 33 | + |
| 34 | + // the epoch milli parser is a bit special, as it does not use date formatter, see comments in DateFormatters |
| 35 | + public void testEpochMilliParser() { |
| 36 | + CompoundDateTimeFormatter formatter = DateFormatters.forPattern("epoch_millis"); |
| 37 | + |
| 38 | + DateTimeParseException e = expectThrows(DateTimeParseException.class, () -> formatter.parse("invalid")); |
| 39 | + assertThat(e.getMessage(), containsString("invalid number")); |
| 40 | + |
| 41 | + // different zone, should still yield the same output, as epoch is time zoned independent |
| 42 | + ZoneId zoneId = randomZone(); |
| 43 | + CompoundDateTimeFormatter zonedFormatter = formatter.withZone(zoneId); |
| 44 | + assertThat(zonedFormatter.printer.getZone(), is(zoneId)); |
| 45 | + |
| 46 | + // test with negative and non negative values |
| 47 | + assertThatSameDateTime(formatter, zonedFormatter, randomNonNegativeLong() * -1); |
| 48 | + assertThatSameDateTime(formatter, zonedFormatter, randomNonNegativeLong()); |
| 49 | + assertThatSameDateTime(formatter, zonedFormatter, 0); |
| 50 | + assertThatSameDateTime(formatter, zonedFormatter, -1); |
| 51 | + assertThatSameDateTime(formatter, zonedFormatter, 1); |
| 52 | + |
| 53 | + // format() output should be equal as well |
| 54 | + assertSameFormat(formatter, randomNonNegativeLong() * -1); |
| 55 | + assertSameFormat(formatter, randomNonNegativeLong()); |
| 56 | + assertSameFormat(formatter, 0); |
| 57 | + assertSameFormat(formatter, -1); |
| 58 | + assertSameFormat(formatter, 1); |
| 59 | + } |
| 60 | + |
| 61 | + private void assertThatSameDateTime(CompoundDateTimeFormatter formatter, CompoundDateTimeFormatter zonedFormatter, long millis) { |
| 62 | + String millisAsString = String.valueOf(millis); |
| 63 | + ZonedDateTime formatterZonedDateTime = DateFormatters.toZonedDateTime(formatter.parse(millisAsString)); |
| 64 | + ZonedDateTime zonedFormatterZonedDateTime = DateFormatters.toZonedDateTime(zonedFormatter.parse(millisAsString)); |
| 65 | + assertThat(formatterZonedDateTime.toInstant().toEpochMilli(), is(zonedFormatterZonedDateTime.toInstant().toEpochMilli())); |
| 66 | + } |
| 67 | + |
| 68 | + private void assertSameFormat(CompoundDateTimeFormatter formatter, long millis) { |
| 69 | + String millisAsString = String.valueOf(millis); |
| 70 | + TemporalAccessor accessor = formatter.parse(millisAsString); |
| 71 | + assertThat(millisAsString, is(formatter.format(accessor))); |
| 72 | + } |
| 73 | +} |
0 commit comments