Skip to content

Commit

Permalink
[EN .NET] Workaround for TimexProperty.ToString() to not crash on Dat…
Browse files Browse the repository at this point in the history
…eTimeRanges (#2894)

* Workaround for TimexProperty.ToString() to not crash on DateTimeRanges 

* Add TODO for fixing the TimexProperty date range representation properly according to review
  • Loading branch information
shana authored Mar 14, 2022
1 parent 2031a05 commit f5426e3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,14 @@ public void DataTypes_Timex_FromDateTime_ToString()
Assert.AreEqual("13th March 2022", timex.ToString());
}

[TestMethod]
public void DataTypes_Timex_FromDateTimeRange_ToString()
{
// TODO: This test documents a workaround to avoid exceptions when calling TimexProperty.ToString(). Proper fix for date range representation is needed.
var timex = new TimexProperty("(2022-03-15T16,2022-03-15T18,PT2H)");
Assert.AreEqual("15th March 2022 4PM", 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 @@ -2,6 +2,7 @@
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Globalization;

namespace Microsoft.Recognizers.Text.DataTypes.TimexExpression
Expand Down Expand Up @@ -216,9 +217,27 @@ private static string ConvertDateTime(TimexProperty timex)

private static string ConvertDateTimeRange(TimexProperty timex)
{
if (timex.Types.Contains(Constants.TimexTypes.TimeRange))
var parts = new List<string>();

var types = timex.Types;
if (types.Contains(Constants.TimexTypes.Date))
{
parts.Add(ConvertDate(timex));
}

if (types.Contains(Constants.TimexTypes.Time))
{
parts.Add(ConvertTime(timex));
}

if (timex.PartOfDay is not null)
{
parts.Add(ConvertTimeRange(timex));
}

if (parts.Count > 0)
{
return $"{ConvertDate(timex)} {ConvertTimeRange(timex)}";
return string.Join(" ", parts);
}

// date + time + duration
Expand Down

0 comments on commit f5426e3

Please # to comment.