Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create filter to format time with a colon #515

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,22 @@ public static IEnumerable<object[]> GetInvalidDataForAddSeconds()
yield return new object[] { @"2001-01T" };
}

public static IEnumerable<object[]> GetValidDataForTimeHandling()
{
yield return new object[] { @"0730", "07:30:00" };
yield return new object[] { @"1730", "17:30:00" };
yield return new object[] { @"18:45", "18:45" };
yield return new object[] { @"09:45:50", "09:45:50" };
yield return new object[] { @"1", "1" };
yield return new object[] { @"abc", "abc" };
yield return new object[] { null, null };
}

public static IEnumerable<object[]> GetInvalidDataForTimeHandling()
{
yield return new object[] { @"9999" };
}

[Theory]
[MemberData(nameof(GetValidDataForAddSeconds))]
public void GivenSeconds_WhenAddOnValidDateTime_CorrectDateTimeStringShouldBeReturned(string originalDateTime, double seconds, string timeZoneHandling, string expectedDateTime)
Expand Down Expand Up @@ -356,6 +372,22 @@ public void GivenAnInvalidDateTime_WhenFormatAsHl7DateTime_ExceptionShouldBeThro
Assert.Equal(FhirConverterErrorCode.InvalidDateTimeFormat, exception.FhirConverterErrorCode);
}

[Theory]
[MemberData(nameof(GetValidDataForTimeHandling))]
public void GivenAValidData_WhenFormatTimeWithColon_FormattedTimeOrOriginalInputShouldBeReturned(string input, string expectedDateTime)
{
var result = Filters.FormatTimeWithColon(input);
Assert.Equal(expectedDateTime, result);
}

[Theory]
[MemberData(nameof(GetInvalidDataForTimeHandling))]
public void GivenInvalidData_WhenFormatAsTimeInterval_InputTimeShouldBeReturnedAndNoExceptionThrown(string input)
{
var exception = Assert.Throws<RenderException>(() => Filters.FormatTimeWithColon(input));
Assert.Equal(FhirConverterErrorCode.InvalidDateTimeFormat, exception.FhirConverterErrorCode);
}

[Fact]
public void NowTest()
{
Expand Down
27 changes: 27 additions & 0 deletions src/Microsoft.Health.Fhir.Liquid.Converter/Filters/DateFilters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
// -------------------------------------------------------------------------------------------------

using System;
using System.Globalization;
using System.Linq;
using Microsoft.Health.Fhir.Liquid.Converter.Exceptions;
using Microsoft.Health.Fhir.Liquid.Converter.Models;

Expand Down Expand Up @@ -96,6 +98,31 @@ public static string Now(string input, string format = "yyyy-MM-ddTHH:mm:ss.FFFZ
return DateTime.UtcNow.ToString(format);
}

/* Converts an HL7v2 time interval to the FHIR format for time
/ HL7v2 - 2.8.35.2 Explicit time interval (ST)
/ FHIR - time https://build.fhir.org/datatypes.html#time
*/
public static string FormatTimeWithColon(string input, string inputFormat = "HHmm")
{
// For backwards compatibility, if the input is not a 4 digit numeric value then just return the value
if (string.IsNullOrEmpty(input) || (!(input.All(char.IsDigit) && (input.Length == 4))))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wi-y Is it better to ignore (input.Length == 4) to make it more as a general filter because there are cases when time is represented as HHmmssSS e.g. when OBX type(OBX.2.1 ) is time(TM) value(OBX.5.1) has seconds and ms. Also there is inputFormat as filter arg which helps parsing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shabanlushaj agree - it is better to make this more generic for the other HL7v2 times. I've updated the pull request so that the filter is more generic.

Question: Is HHmmssSS supported by HL7v2 for TM time? I was looking at the spec and it seemed to be HHMM[SS[.SSSS]][+/-ZZZZ] with the milliseconds following a decimal. But either way both formats HHmmssff and HHmmss.ff should be supported now.

Copy link
Contributor

@shabanlushaj shabanlushaj Jan 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wi-y Whenever it is UTC, timezone can be discarded. I believe same applies for time. It would be nice to have this filter support timezone.

20240107111232-0300 for datetime this is supported by converter (MSH.7 - Date/Time Of Message).

Tried something that might do the work, but we have to ignore check for input length and inputFormat having same length.

var input = "144100.0000+0300";
var inputFormat = "HHmmss.ffffzzzz";
var dt = DateTime.ParseExact(input, inputFormat, CultureInfo.InvariantCulture).TimeOfDay; // might need also an arg for preserve/local/utc output (same as datetime) 

{
return input;
}

var dt = TimeSpan.Zero;
try
{
dt = DateTime.ParseExact(input, inputFormat, CultureInfo.InvariantCulture).TimeOfDay;
}
catch (Exception)
{
throw new RenderException(FhirConverterErrorCode.InvalidDateTimeFormat, string.Format(Resources.InvalidDateTimeFormat, input));
}

return dt.ToString();
}

public static string FormatAsHl7v2DateTime(string input, string timeZoneHandling = "preserve")
{
if (string.IsNullOrEmpty(input))
Expand Down