Skip to content

Commit f5426e3

Browse files
authored
[EN .NET] Workaround for TimexProperty.ToString() to not crash on DateTimeRanges (#2894)
* Workaround for TimexProperty.ToString() to not crash on DateTimeRanges * Add TODO for fixing the TimexProperty date range representation properly according to review
1 parent 2031a05 commit f5426e3

2 files changed

Lines changed: 29 additions & 2 deletions

File tree

.NET/Microsoft.Recognizers.Text.DataTypes.DataDrivenTests/TestTimex.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,14 @@ public void DataTypes_Timex_FromDateTime_ToString()
138138
Assert.AreEqual("13th March 2022", timex.ToString());
139139
}
140140

141+
[TestMethod]
142+
public void DataTypes_Timex_FromDateTimeRange_ToString()
143+
{
144+
// TODO: This test documents a workaround to avoid exceptions when calling TimexProperty.ToString(). Proper fix for date range representation is needed.
145+
var timex = new TimexProperty("(2022-03-15T16,2022-03-15T18,PT2H)");
146+
Assert.AreEqual("15th March 2022 4PM", timex.ToString());
147+
}
148+
141149
private static void Roundtrip(string timex)
142150
{
143151
Assert.AreEqual(timex, new TimexProperty(timex).TimexValue);

.NET/Microsoft.Recognizers.Text.DataTypes.TimexExpression/English/TimexConvertEnglish.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT License.
33

44
using System;
5+
using System.Collections.Generic;
56
using System.Globalization;
67

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

217218
private static string ConvertDateTimeRange(TimexProperty timex)
218219
{
219-
if (timex.Types.Contains(Constants.TimexTypes.TimeRange))
220+
var parts = new List<string>();
221+
222+
var types = timex.Types;
223+
if (types.Contains(Constants.TimexTypes.Date))
224+
{
225+
parts.Add(ConvertDate(timex));
226+
}
227+
228+
if (types.Contains(Constants.TimexTypes.Time))
229+
{
230+
parts.Add(ConvertTime(timex));
231+
}
232+
233+
if (timex.PartOfDay is not null)
234+
{
235+
parts.Add(ConvertTimeRange(timex));
236+
}
237+
238+
if (parts.Count > 0)
220239
{
221-
return $"{ConvertDate(timex)} {ConvertTimeRange(timex)}";
240+
return string.Join(" ", parts);
222241
}
223242

224243
// date + time + duration

0 commit comments

Comments
 (0)