From 2031a056c741557e8aa970836a2379985c23ad3f Mon Sep 17 00:00:00 2001 From: Andreia Gaita Date: Sat, 12 Mar 2022 07:52:33 +0100 Subject: [PATCH] [.NET Timexlib] Fix timex-to-string conversion of English ordinals 11-13 (#2896) --- .../TestTimex.cs | 11 +++++++++++ .../English/TimexConvertEnglish.cs | 5 ++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/.NET/Microsoft.Recognizers.Text.DataTypes.DataDrivenTests/TestTimex.cs b/.NET/Microsoft.Recognizers.Text.DataTypes.DataDrivenTests/TestTimex.cs index 63e30ba63c..1b890c29a0 100644 --- a/.NET/Microsoft.Recognizers.Text.DataTypes.DataDrivenTests/TestTimex.cs +++ b/.NET/Microsoft.Recognizers.Text.DataTypes.DataDrivenTests/TestTimex.cs @@ -127,6 +127,17 @@ public void DataTypes_Timex_FromTime() Assert.AreEqual("T23:59:30", TimexProperty.FromTime(new Time(23, 59, 30)).TimexValue); } + [TestMethod] + public void DataTypes_Timex_FromDateTime_ToString() + { + var timex = new TimexProperty("2022-03-11"); + Assert.AreEqual("11th March 2022", timex.ToString()); + timex = new TimexProperty("2022-03-12"); + Assert.AreEqual("12th March 2022", timex.ToString()); + timex = new TimexProperty("2022-03-13"); + Assert.AreEqual("13th March 2022", timex.ToString()); + } + private static void Roundtrip(string timex) { Assert.AreEqual(timex, new TimexProperty(timex).TimexValue); diff --git a/.NET/Microsoft.Recognizers.Text.DataTypes.TimexExpression/English/TimexConvertEnglish.cs b/.NET/Microsoft.Recognizers.Text.DataTypes.TimexExpression/English/TimexConvertEnglish.cs index d651f858bf..b57f023770 100644 --- a/.NET/Microsoft.Recognizers.Text.DataTypes.TimexExpression/English/TimexConvertEnglish.cs +++ b/.NET/Microsoft.Recognizers.Text.DataTypes.TimexExpression/English/TimexConvertEnglish.cs @@ -97,7 +97,10 @@ public static string ConvertDate(TimexProperty timex) } var date = timex.DayOfMonth.Value.ToString(CultureInfo.InvariantCulture); - var abbreviation = TimexConstantsEnglish.DateAbbreviation[int.Parse(date[date.Length - 1].ToString(CultureInfo.InvariantCulture), CultureInfo.InvariantCulture)]; + var dayOfMonth = int.Parse(date, CultureInfo.InvariantCulture); + + // Ordinals 11 to 13 are special in english as they end in th + var abbreviation = TimexConstantsEnglish.DateAbbreviation[(dayOfMonth is > 9 and < 14 ? 9 : dayOfMonth) % 10]; if (timex.Month != null) {