2727import org .elasticsearch .common .Strings ;
2828import org .elasticsearch .common .collect .Tuple ;
2929import org .elasticsearch .common .component .AbstractComponent ;
30- import org .elasticsearch .common .joda .FormatDateTimeFormatter ;
3130import org .elasticsearch .common .regex .Regex ;
3231import org .elasticsearch .common .settings .Settings ;
32+ import org .elasticsearch .common .time .DateFormatter ;
33+ import org .elasticsearch .common .time .DateFormatters ;
3334import org .elasticsearch .common .time .DateMathParser ;
34- import org .elasticsearch .common .time .DateUtils ;
35+ import org .elasticsearch .common .time .JavaDateMathParser ;
3536import org .elasticsearch .common .util .set .Sets ;
3637import org .elasticsearch .index .Index ;
3738import org .elasticsearch .index .IndexNotFoundException ;
3839import org .elasticsearch .indices .IndexClosedException ;
3940import org .elasticsearch .indices .InvalidIndexNameException ;
40- import org .joda .time .DateTimeZone ;
41- import org .joda .time .format .DateTimeFormat ;
42- import org .joda .time .format .DateTimeFormatter ;
4341
42+ import java .time .Instant ;
43+ import java .time .ZoneId ;
44+ import java .time .ZoneOffset ;
4445import java .util .ArrayList ;
4546import java .util .Arrays ;
4647import java .util .Collections ;
4748import java .util .HashMap ;
4849import java .util .HashSet ;
4950import java .util .List ;
50- import java .util .Locale ;
5151import java .util .Map ;
5252import java .util .Set ;
5353import java .util .SortedMap ;
@@ -62,7 +62,7 @@ public class IndexNameExpressionResolver extends AbstractComponent {
6262 public IndexNameExpressionResolver (Settings settings ) {
6363 super (settings );
6464 expressionResolvers = Arrays .asList (
65- dateMathExpressionResolver = new DateMathExpressionResolver (settings ),
65+ dateMathExpressionResolver = new DateMathExpressionResolver (),
6666 new WildcardExpressionResolver ()
6767 );
6868 }
@@ -815,24 +815,14 @@ private static List<String> resolveEmptyOrTrivialWildcard(IndicesOptions options
815815
816816 static final class DateMathExpressionResolver implements ExpressionResolver {
817817
818+ private static final DateFormatter DEFAULT_DATE_FORMATTER = DateFormatters .forPattern ("uuuu.MM.dd" );
818819 private static final String EXPRESSION_LEFT_BOUND = "<" ;
819820 private static final String EXPRESSION_RIGHT_BOUND = ">" ;
820821 private static final char LEFT_BOUND = '{' ;
821822 private static final char RIGHT_BOUND = '}' ;
822823 private static final char ESCAPE_CHAR = '\\' ;
823824 private static final char TIME_ZONE_BOUND = '|' ;
824825
825- private final DateTimeZone defaultTimeZone ;
826- private final String defaultDateFormatterPattern ;
827- private final DateTimeFormatter defaultDateFormatter ;
828-
829- DateMathExpressionResolver (Settings settings ) {
830- String defaultTimeZoneId = settings .get ("date_math_expression_resolver.default_time_zone" , "UTC" );
831- this .defaultTimeZone = DateTimeZone .forID (defaultTimeZoneId );
832- defaultDateFormatterPattern = settings .get ("date_math_expression_resolver.default_date_format" , "YYYY.MM.dd" );
833- this .defaultDateFormatter = DateTimeFormat .forPattern (defaultDateFormatterPattern );
834- }
835-
836826 @ Override
837827 public List <String > resolve (final Context context , List <String > expressions ) {
838828 List <String > result = new ArrayList <>(expressions .size ());
@@ -896,13 +886,12 @@ String resolveExpression(String expression, final Context context) {
896886 int dateTimeFormatLeftBoundIndex = inPlaceHolderString .indexOf (LEFT_BOUND );
897887 String mathExpression ;
898888 String dateFormatterPattern ;
899- DateTimeFormatter dateFormatter ;
900- final DateTimeZone timeZone ;
889+ DateFormatter dateFormatter ;
890+ final ZoneId timeZone ;
901891 if (dateTimeFormatLeftBoundIndex < 0 ) {
902892 mathExpression = inPlaceHolderString ;
903- dateFormatterPattern = defaultDateFormatterPattern ;
904- dateFormatter = defaultDateFormatter ;
905- timeZone = defaultTimeZone ;
893+ dateFormatter = DEFAULT_DATE_FORMATTER ;
894+ timeZone = ZoneOffset .UTC ;
906895 } else {
907896 if (inPlaceHolderString .lastIndexOf (RIGHT_BOUND ) != inPlaceHolderString .length () - 1 ) {
908897 throw new ElasticsearchParseException ("invalid dynamic name expression [{}]. missing closing `}` for date math format" , inPlaceHolderString );
@@ -915,20 +904,18 @@ String resolveExpression(String expression, final Context context) {
915904 int formatPatternTimeZoneSeparatorIndex = dateFormatterPatternAndTimeZoneId .indexOf (TIME_ZONE_BOUND );
916905 if (formatPatternTimeZoneSeparatorIndex != -1 ) {
917906 dateFormatterPattern = dateFormatterPatternAndTimeZoneId .substring (0 , formatPatternTimeZoneSeparatorIndex );
918- timeZone = DateTimeZone . forID (dateFormatterPatternAndTimeZoneId .substring (formatPatternTimeZoneSeparatorIndex + 1 ));
907+ timeZone = ZoneId . of (dateFormatterPatternAndTimeZoneId .substring (formatPatternTimeZoneSeparatorIndex + 1 ));
919908 } else {
920909 dateFormatterPattern = dateFormatterPatternAndTimeZoneId ;
921- timeZone = defaultTimeZone ;
910+ timeZone = ZoneOffset . UTC ;
922911 }
923- dateFormatter = DateTimeFormat .forPattern (dateFormatterPattern );
912+ dateFormatter = DateFormatters .forPattern (dateFormatterPattern );
924913 }
925- DateTimeFormatter parser = dateFormatter .withZone (timeZone );
926- FormatDateTimeFormatter formatter = new FormatDateTimeFormatter (dateFormatterPattern , parser , Locale .ROOT );
927- DateMathParser dateMathParser = formatter .toDateMathParser ();
928- long millis = dateMathParser .parse (mathExpression , context ::getStartTime , false ,
929- DateUtils .dateTimeZoneToZoneId (timeZone ));
914+ DateFormatter formatter = dateFormatter .withZone (timeZone );
915+ DateMathParser dateMathParser = new JavaDateMathParser (formatter );
916+ long millis = dateMathParser .parse (mathExpression , context ::getStartTime , false , timeZone );
930917
931- String time = formatter .printer (). print (millis );
918+ String time = formatter .format ( Instant . ofEpochMilli (millis ) );
932919 beforePlaceHolderSb .append (time );
933920 inPlaceHolderSb = new StringBuilder ();
934921 inPlaceHolder = false ;
@@ -968,18 +955,4 @@ String resolveExpression(String expression, final Context context) {
968955 return beforePlaceHolderSb .toString ();
969956 }
970957 }
971-
972- /**
973- * Returns <code>true</code> iff the given expression resolves to the given index name otherwise <code>false</code>
974- */
975- public final boolean matchesIndex (String indexName , String expression , ClusterState state ) {
976- final String [] concreteIndices = concreteIndexNames (state , IndicesOptions .lenientExpandOpen (), expression );
977- for (String index : concreteIndices ) {
978- if (Regex .simpleMatch (index , indexName )) {
979- return true ;
980- }
981- }
982- return indexName .equals (expression );
983- }
984-
985958}
0 commit comments