diff --git a/core/src/main/java/org/elasticsearch/common/unit/TimeValue.java b/core/src/main/java/org/elasticsearch/common/unit/TimeValue.java index db8299cdc9ac4..ed67019c10399 100644 --- a/core/src/main/java/org/elasticsearch/common/unit/TimeValue.java +++ b/core/src/main/java/org/elasticsearch/common/unit/TimeValue.java @@ -326,7 +326,10 @@ public static TimeValue parseTimeValue(String sValue, TimeValue defaultValue, St return new TimeValue(parse(sValue, normalized, 2), TimeUnit.MILLISECONDS); } else if (normalized.endsWith("s")) { return new TimeValue(parse(sValue, normalized, 1), TimeUnit.SECONDS); - } else if (normalized.endsWith("m")) { + } else if (sValue.endsWith("m")) { + // parsing minutes should be case sensitive as `M` is generally + // accepted to mean months not minutes. This is the only case where + // the upper and lower case forms indicate different time units return new TimeValue(parse(sValue, normalized, 1), TimeUnit.MINUTES); } else if (normalized.endsWith("h")) { return new TimeValue(parse(sValue, normalized, 1), TimeUnit.HOURS); diff --git a/core/src/test/java/org/elasticsearch/common/unit/TimeValueTests.java b/core/src/test/java/org/elasticsearch/common/unit/TimeValueTests.java index 003d78ce42eca..4d0ac5257a31e 100644 --- a/core/src/test/java/org/elasticsearch/common/unit/TimeValueTests.java +++ b/core/src/test/java/org/elasticsearch/common/unit/TimeValueTests.java @@ -92,10 +92,6 @@ public void testParseTimeValue() { TimeValue.parseTimeValue("10 m", null, "test")); assertEquals(new TimeValue(10, TimeUnit.MINUTES), TimeValue.parseTimeValue("10m", null, "test")); - assertEquals(new TimeValue(10, TimeUnit.MINUTES), - TimeValue.parseTimeValue("10 M", null, "test")); - assertEquals(new TimeValue(10, TimeUnit.MINUTES), - TimeValue.parseTimeValue("10M", null, "test")); assertEquals(new TimeValue(10, TimeUnit.HOURS), TimeValue.parseTimeValue("10 h", null, "test")); @@ -115,6 +111,17 @@ public void testParseTimeValue() { assertEquals(new TimeValue(10, TimeUnit.DAYS), TimeValue.parseTimeValue("10D", null, "test")); + // Time values of months should throw an exception as months are not + // supported. Note that this is the only unit that is not case sensitive + // as `m` is the only character that is overloaded in terms of which + // time unit is expected between the upper and lower case versions + expectThrows(ElasticsearchParseException.class, () -> { + TimeValue.parseTimeValue("10 M", null, "test"); + }); + expectThrows(ElasticsearchParseException.class, () -> { + TimeValue.parseTimeValue("10M", null, "test"); + }); + final int length = randomIntBetween(0, 8); final String zeros = new String(new char[length]).replace('\0', '0'); assertTrue(TimeValue.parseTimeValue("-" + zeros + "1", null, "test") == TimeValue.MINUS_ONE);