From 2b62b3b6d7cea883b0640b3411f57922d5a2538c Mon Sep 17 00:00:00 2001 From: songrui Date: Mon, 25 Oct 2021 15:55:36 +0800 Subject: [PATCH 01/17] add datetime filters --- .../Filters/DateFiltersTests.cs | 46 +++++++ .../Filters/DateFilters.cs | 124 ++++++++++++------ 2 files changed, 128 insertions(+), 42 deletions(-) diff --git a/src/Microsoft.Health.Fhir.Liquid.Converter.UnitTests/Filters/DateFiltersTests.cs b/src/Microsoft.Health.Fhir.Liquid.Converter.UnitTests/Filters/DateFiltersTests.cs index cdb5d195a..565e81a5c 100644 --- a/src/Microsoft.Health.Fhir.Liquid.Converter.UnitTests/Filters/DateFiltersTests.cs +++ b/src/Microsoft.Health.Fhir.Liquid.Converter.UnitTests/Filters/DateFiltersTests.cs @@ -23,6 +23,23 @@ public static IEnumerable GetValidDataForAddHyphensDate() yield return new object[] { @"19850101000000", @"1985-01-01" }; } + public static IEnumerable GetValidDataForAddSeconds() + { + yield return new object[] { null, 60, "local", null }; + yield return new object[] { string.Empty, 60, "local", string.Empty }; + + yield return new object[] { @"1970-01-01T00:01:00+10:00", -60, "utc", @"1969-12-31T14:00:00Z" }; + + yield return new object[] { @"1970-01-01T00:01:00Z", 60, "preserve", @"1970-01-01T00:02:00Z" }; + yield return new object[] { @"1970-01-01T00:01:00+06:00", 60, "preserve", @"1970-01-01T00:02:00+06:00" }; + yield return new object[] { @"2001-01", 60, "preserve", @"2001-01-01T00:01:00" }; + + // Skip this test in pipeline, as the local time zone is different + // yield return new object[] { @"1924-10-10", 60000, "utc", @"1924-10-10T08:40:00Z" }; + // yield return new object[] { @"1970-01-01T00:01:00+06:00", 60, "local", @"1970-01-01T02:02:00+08:00" }; + // yield return new object[] { @"1924-10-10", 60000, "local", @"1924-10-10T16:40:00" }; + } + public static IEnumerable GetValidDataForFormatAsDateTime() { // TimeZoneHandling does not affect dateTime without time @@ -90,6 +107,35 @@ public static IEnumerable GetInvalidTimeZoneHandling() yield return new object[] { @"19701231115959+0600", "abc" }; } + public static IEnumerable GetInvalidDataForAddSeconds() + { + yield return new object[] { @"20badInput" }; + yield return new object[] { @"20140130080051--0500" }; + yield return new object[] { @"2014.051-0500" }; + yield return new object[] { @"20140130080051123+0500" }; + yield return new object[] { @"20201" }; + yield return new object[] { @"2020060" }; + yield return new object[] { @"1970-01-01T00:01:00" }; + yield return new object[] { @"1970-01-01T00:01" }; + yield return new object[] { @"2001-01T" }; + } + + [Theory] + [MemberData(nameof(GetValidDataForAddSeconds))] + public void GivenSeconds_WhenAddOnValidDateTime_CorrectDateTimeShouldBeReturned(string originalDateTime, double seconds, string timeZoneHandling, string expectedDateTime) + { + var result = Filters.AddSeconds(originalDateTime, seconds, timeZoneHandling); + Assert.Equal(expectedDateTime, result); + } + + [Theory] + [MemberData(nameof(GetInvalidDataForAddSeconds))] + public void GivenSeconds_WhenAddOnInvalidDateTime_ExceptionShouldBeThrow(string originalDateTime) + { + var exception = Assert.Throws(() => Filters.AddSeconds(originalDateTime, 0)); + Assert.Equal(FhirConverterErrorCode.InvalidDateTimeFormat, exception.FhirConverterErrorCode); + } + [Theory] [MemberData(nameof(GetValidDataForAddHyphensDate))] public void GivenADate_WhenAddHyphensDate_ConvertedDateShouldBeReturned(string input, string expected) diff --git a/src/Microsoft.Health.Fhir.Liquid.Converter/Filters/DateFilters.cs b/src/Microsoft.Health.Fhir.Liquid.Converter/Filters/DateFilters.cs index e0fcde9b1..673d40639 100644 --- a/src/Microsoft.Health.Fhir.Liquid.Converter/Filters/DateFilters.cs +++ b/src/Microsoft.Health.Fhir.Liquid.Converter/Filters/DateFilters.cs @@ -15,22 +15,30 @@ namespace Microsoft.Health.Fhir.Liquid.Converter /// public partial class Filters { - private static readonly Regex DateTimeRegex = new Regex(@"^((?\d{4})((?\d{2})((?\d{2})(?