Skip to content

Commit

Permalink
[.NET Timexlib] Fix timex-to-string conversion of English ordinals 11…
Browse files Browse the repository at this point in the history
…-13 (#2896)
  • Loading branch information
shana authored Mar 12, 2022
1 parent 4589775 commit 2031a05
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down

0 comments on commit 2031a05

Please sign in to comment.