public void GivenAValidString_WhenParsed_ThenCorrectPartialDateTimeShouldBeCreated(
            string s,
            int year,
            int?month,
            int?day,
            int?hour,
            int?minute,
            int?second,
            decimal?fraction,
            int?utcOffsetInMinute)
        {
            PartialDateTime dateTime = PartialDateTime.Parse(s);

            Assert.NotNull(dateTime);
            Assert.Equal(year, dateTime.Year);
            Assert.Equal(month, dateTime.Month);
            Assert.Equal(day, dateTime.Day);
            Assert.Equal(hour, dateTime.Hour);
            Assert.Equal(minute, dateTime.Minute);
            Assert.Equal(second, dateTime.Second);
            Assert.Equal(fraction, dateTime.Fraction);

            TimeSpan?utcOffset = null;

            if (utcOffsetInMinute != null)
            {
                utcOffset = TimeSpan.FromMinutes(utcOffsetInMinute.Value);
            }

            Assert.Equal(utcOffset, dateTime.UtcOffset);
        }
Ejemplo n.º 2
0
        public void DateTimeConstructor()
        {
            PartialDateTime.Parse("2012-03");
            PartialDateTime.Parse("2012-03-04");
            PartialDateTime.Parse("2012-03-04T12:34:34+02:00");
            PartialDateTime.Parse("2012-03-04T12:34:34Z");

            PartialDateTime pd;

            Assert.True(PartialDateTime.TryParse("2012-03", out pd));
            Assert.Equal(pd, PartialDateTime.Parse("2012-03"));
            Assert.Equal("2012-03", pd.ToString());

            Assert.False(PartialDateTime.TryParse("2012-03T12:34", out pd));
            Assert.False(PartialDateTime.TryParse("20120304", out pd));
            Assert.True(PartialDateTime.TryParse("2012-03-04T12:04:45", out pd));     //FHIR does not allow this, ISO8601 does.
            Assert.True(PartialDateTime.TryParse("2012-03-04T12:04:45Z", out pd));
            Assert.False(PartialDateTime.TryParse("T12:04:45Z", out pd));
            Assert.False(PartialDateTime.TryParse("12:04:45Z", out pd));

            Assert.True(PartialDateTime.Parse("2012-03-04") > PartialDateTime.Parse("2012-03-01"));

            Assert.Equal(PartialDateTime.Today().ToString(), PartialDateTime.FromDateTime(DateTime.Today).ToString().Substring(0, 10));
            Assert.Equal(PartialDateTime.Now().ToString().Substring(0, 19), PartialDateTime.FromDateTime(DateTimeOffset.Now).ToString().Substring(0, 19));
        }
        public void GivenAValidPartialDateTime_WhenToStringIsCalled_ThenCorrectStringShouldBeReturned(string input, string expected)
        {
            PartialDateTime dateTime = PartialDateTime.Parse(input);

            Assert.NotNull(dateTime);
            Assert.Equal(expected, dateTime.ToString());
        }
Ejemplo n.º 4
0
        public void GiveAStartDateTimeThatIsEarlierThanOrEqualToEndDateTime_WhenInitialized_ThenNoExceptionShouldBeThrown(string start, string end)
        {
            PartialDateTime startDateTime = PartialDateTime.Parse(start);
            PartialDateTime endDateTime   = PartialDateTime.Parse(end);

            new DateTimeSearchValue(startDateTime, endDateTime);
        }
Ejemplo n.º 5
0
        public void GivenAStartDateTimeLaterThanEndDateTime_WhenInitializing_ThenNoExceptionShouldBeThrown(string start, string end)
        {
            PartialDateTime startDateTime = PartialDateTime.Parse(start);
            PartialDateTime endDateTime   = PartialDateTime.Parse(end);

            new DateTimeSearchValue(startDateTime, endDateTime);
        }
        public void GivenAStartDateTimeLaterThanEndDateTime_WhenInitializing_ThenExceptionShouldBeThrown(string start, string end)
        {
            PartialDateTime startDateTime = PartialDateTime.Parse(start);
            PartialDateTime endDateTime   = PartialDateTime.Parse(end);

            Assert.Throws <ArgumentOutOfRangeException>(ParamNameStartDateTime, () => new DateTimeSearchValue(startDateTime, endDateTime));
        }
Ejemplo n.º 7
0
        public void FhirPath_Lex_DateTime()
        {
            var parser = Lexer.DateTime.End();

            accept("@2015-01T");
            accept("@2015-01-02T12:34:00Z");
            accept("@2015-01-03T12:34:34+02:30");
            accept("@2015-01-03T12:34:34");
            accept("@2015-01-01T23");
            accept("@2015-01-01T");
            accept("@2015-01-01T23Z");
            accept("@2015-01-01T+01:00");

            reject("@2015-01");          // must have a T suffix
            reject("@2015-01-02");       // since this is a date, not a dateTime
            reject("@2015-01-02+01:00"); // since this is a date, not a dateTime

            reject("T12:34:34+02:30");
            reject("12:34:34+02:30");
            reject("@T12:34:34+02:30");
            reject("@12:34:34+02:30");
            reject("@20150103T12:34:34+02:30");
            reject("@-2015-01");

            void accept(string s) => AssertParser.SucceedsMatch(parser, s, PartialDateTime.Parse(Lexer.CleanupDateTimeLiteral(s)));
            void reject(string s) => AssertParser.FailsMatch(parser, s);
        }
        public void GivenAPartialDateTimeWithMissingComponents_WhenToDateTimeOffsetIsCalled_ThenCorrectDateTimeOffsetIsReturned()
        {
            const int     expectedMonth     = 2;
            const int     expectedDay       = 10;
            const int     expectedHour      = 15;
            const int     expectedMinute    = 13;
            const int     expectedSecond    = 12;
            const decimal expectedFraction  = 0.3009953m;
            var           expectedUtcOffset = TimeSpan.FromMinutes(30);

            var dateTime = PartialDateTime.Parse("2013");

            var actualOffset = dateTime.ToDateTimeOffset(
                expectedMonth,
                (year, month) => expectedDay,
                expectedHour,
                expectedMinute,
                expectedSecond,
                expectedFraction,
                expectedUtcOffset);

            var expectedOffset = new DateTimeOffset(
                2013,
                expectedMonth,
                expectedDay,
                expectedHour,
                expectedMinute,
                expectedSecond,
                expectedUtcOffset);

            expectedOffset = expectedOffset.AddTicks((long)(expectedFraction * TimeSpan.TicksPerSecond));

            Assert.Equal(expectedOffset, actualOffset);
        }
        public void GivenAPartialDateTimeWithNoMissingComponent_WhenToDateTimeOffsetIsCalled_ThenCorrectDateTimeOffsetIsReturned()
        {
            var dateTime = PartialDateTime.Parse("2013-10-12T23:01:35.9995555+02:00");

            var actualOffset = dateTime.ToDateTimeOffset(
                2,
                (year, month) => 10,
                15,
                13,
                12,
                0.300250m,
                TimeSpan.FromMinutes(30));

            var expectedOffset = new DateTimeOffset(
                2013,
                10,
                12,
                23,
                01,
                35,
                TimeSpan.FromMinutes(120));

            expectedOffset = expectedOffset.AddTicks((long)(0.9995555m * TimeSpan.TicksPerSecond));

            Assert.Equal(expectedOffset, actualOffset);
        }
Ejemplo n.º 10
0
        public override bool Equals(object obj)
        {
            if (obj is Date)
            {
                var other      = (Date)obj;
                var otherValue = !Object.ReferenceEquals(other, null) ? other.Value : null;

                if (Value == null)
                {
                    return(otherValue == null);
                }
                if (otherValue == null)
                {
                    return(false);
                }

                if (this.Value == otherValue)
                {
                    return(true);                          // Default reference/string comparison works in most cases
                }
                var left  = PartialDateTime.Parse(Value);
                var right = PartialDateTime.Parse(otherValue);

                return(left == right);
            }
            else
            {
                return(false);
            }
        }
        public async Task GivenAnEverythingOperationRequest_WhenValid_ThenProperResponseShouldBeReturned()
        {
            _mediator.Send(Arg.Any <EverythingOperationRequest>(), Arg.Any <CancellationToken>()).Returns(Task.FromResult(GetEverythingOperationResponse()));

            var result = await _everythingController.PatientEverythingById(
                idParameter : "123",
                start : PartialDateTime.Parse("2019"),
                end : PartialDateTime.Parse("2020"),
                since : PartialDateTime.Parse("2021"),
                type : ResourceType.Observation.ToString(),
                ct : null) as FhirResult;

            await _mediator.Received().Send(
                Arg.Is <EverythingOperationRequest>(
                    r => string.Equals(r.EverythingOperationType, ResourceType.Patient.ToString(), StringComparison.Ordinal) &&
                    string.Equals(r.ResourceId.ToString(), "123", StringComparison.OrdinalIgnoreCase) &&
                    string.Equals(r.Start.ToString(), "2019", StringComparison.Ordinal) &&
                    string.Equals(r.End.ToString(), "2020", StringComparison.Ordinal) &&
                    string.Equals(r.Since.ToString(), "2021", StringComparison.Ordinal) &&
                    string.Equals(r.ResourceTypes, ResourceType.Observation.ToString(), StringComparison.Ordinal) &&
                    r.ContinuationToken == null),
                Arg.Any <CancellationToken>());

            _mediator.ClearReceivedCalls();

            var bundleResource = (result?.Result as ResourceElement)?.ResourceInstance as Bundle;

            Assert.Equal(System.Net.HttpStatusCode.OK, result?.StatusCode);
            Assert.Equal(Bundle.BundleType.Searchset, bundleResource?.Type);
        }
        /// <summary>
        /// Parses the string value to an instance of <see cref="DateTimeSearchValue"/>.
        /// </summary>
        /// <param name="s">The string to be parsed.</param>
        /// <returns>An instance of <see cref="DateTimeSearchValue"/>.</returns>
        public static DateTimeSearchValue Parse(string s)
        {
            EnsureArg.IsNotNullOrWhiteSpace(s, nameof(s));

            PartialDateTime dateTime = PartialDateTime.Parse(s);

            return(new DateTimeSearchValue(dateTime));
        }
Ejemplo n.º 13
0
        public void GivenACulture_WhenToStringisCalled_ThenCorrectStringShouldBeReturned(string culture, string input, string expected)
        {
            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(culture);
            PartialDateTime dateTime = PartialDateTime.Parse(input);

            Assert.NotNull(dateTime);
            Assert.Equal(expected, dateTime.ToString());
        }
        public void GivenHourIsSpecifiedWithoutMinutes_WhenParsingPartialDateTime_ThenFormatExceptionShouldBeThrown(string inputString)
        {
            Exception ex = Assert.Throws <FormatException>(() => PartialDateTime.Parse(inputString));

            string expectedMessage = string.Format(Resources.DateTimeStringIsIncorrectlyFormatted, inputString);

            Assert.Equal(expectedMessage, ex.Message);
        }
        public void GivenACulture_WhenToStringIsCalled_ThenCorrectStringShouldBeReturned(string culture, string inputString, string expectedString)
        {
            System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo(culture);
            var dateTime = PartialDateTime.Parse(inputString);

            Assert.NotNull(dateTime);
            Assert.Equal(expectedString, dateTime.ToString());
        }
        [InlineData("2013-05-18T23:57:09.999999999999999999999999+01:00")] // Fraction cannot be rounded up to 1 minute.
        public void GivenAOutOfRangeParameter_WhenInitializing_ThenFormatExceptionShouldBeThrown(string inputString)
        {
            Exception ex = Assert.Throws <FormatException>(() => PartialDateTime.Parse(inputString));

            string expectedMessage = string.Format(Resources.DateTimeStringIsIncorrectlyFormatted, inputString);

            Assert.Equal(expectedMessage, ex.Message);
        }
Ejemplo n.º 17
0
        public void TimeComparison()
        {
            Assert.True(PartialDateTime.Parse("2012-03-04T13:00:00Z") > PartialDateTime.Parse("2012-03-04T12:00:00Z"));
            Assert.True(PartialDateTime.Parse("2012-03-04T13:00:00Z") < PartialDateTime.Parse("2012-03-04T18:00:00+02:00"));

            Assert.True(Time.Parse("12:34:00+00:00") > Time.Parse("12:33:55+00:00"));
            Assert.True(Time.Parse("13:00:00+00:00") < Time.Parse("15:01:00+02:00"));
            Assert.True(Time.Parse("13:00:00+00:00") > Time.Parse("14:59:00+02:00"));
        }
        /// <summary>
        /// Parses the string value to an instance of <see cref="DateTimeSearchValue"/>.
        /// </summary>
        /// <param name="startDateTime">The string to be parsed as start time.</param>
        /// <param name="endDateTime">The string to be parsed as end time.</param>
        /// <returns>An instance of <see cref="DateTimeSearchValue"/>.</returns>
        public static DateTimeSearchValue Parse(string startDateTime, string endDateTime)
        {
            EnsureArg.IsNotNullOrWhiteSpace(startDateTime, nameof(startDateTime));
            EnsureArg.IsNotNullOrWhiteSpace(endDateTime, nameof(endDateTime));

            PartialDateTime startPartial = PartialDateTime.Parse(startDateTime);
            PartialDateTime endPartial   = PartialDateTime.Parse(endDateTime);

            return(new DateTimeSearchValue(startPartial, endPartial));
        }
Ejemplo n.º 19
0
        public void CheckOrdering()
        {
            Assert.Equal(1, PartialDateTime.Parse("2012-03-04T13:00:00Z").CompareTo(PartialDateTime.Parse("2012-03-04T12:00:00Z")));
            Assert.Equal(-1, PartialDateTime.Parse("2012-03-04T13:00:00Z").CompareTo(PartialDateTime.Parse("2012-03-04T18:00:00+02:00")));
            Assert.Equal(0, PartialDateTime.Parse("2015-01-01").CompareTo(PartialDateTime.Parse("2015-01-01")));

            Assert.Equal(1, Time.Parse("12:34:00+00:00").CompareTo(Time.Parse("12:33:55+00:00")));
            Assert.Equal(-1, Time.Parse("13:00:00+00:00").CompareTo(Time.Parse("15:01:00+02:00")));
            Assert.Equal(0, Time.Parse("13:45:02+01:00").CompareTo(Time.Parse("13:45:02+01:00")));
        }
Ejemplo n.º 20
0
        public void TestFhirPathTrace()
        {
            var patient = new Hl7.Fhir.Model.Patient()
            {
                Id = "pat45", Active = false
            };

            patient.Meta = new Meta()
            {
                LastUpdated = new DateTimeOffset(2018, 5, 24, 14, 48, 0, TimeSpan.Zero)
            };
            var nav = patient.ToTypedElement();

            EvaluationContext ctx = new FhirEvaluationContext();
            var result            = nav.Select("Resource.meta.trace('log').lastUpdated", ctx);

            Assert.IsNotNull(result.FirstOrDefault());
            Assert.AreEqual(PartialDateTime.Parse("2018-05-24T14:48:00+00:00"), result.First().Value);

            bool traced = false;

            ctx.Tracer = (string name, System.Collections.Generic.IEnumerable <ITypedElement> results) =>
            {
                System.Diagnostics.Trace.WriteLine($"{name}");
                Assert.AreEqual("log", name);
                foreach (ITypedElement item in results)
                {
                    var fhirValue = item.Annotation <IFhirValueProvider>();
                    System.Diagnostics.Trace.WriteLine($"--({fhirValue.FhirValue.GetType().Name}): {item.Value} {fhirValue.FhirValue}");
                    Assert.AreEqual(patient.Meta, fhirValue.FhirValue);
                    traced = true;
                }
            };
            result = nav.Select("Resource.meta.trace('log').lastUpdated", ctx);
            Assert.IsNotNull(result.FirstOrDefault());
            Assert.AreEqual(PartialDateTime.Parse("2018-05-24T14:48:00+00:00"), result.First().Value);
            Assert.IsTrue(traced);

            traced     = false;
            ctx.Tracer = (string name, System.Collections.Generic.IEnumerable <ITypedElement> results) =>
            {
                System.Diagnostics.Trace.WriteLine($"{name}");
                Assert.IsTrue(name == "id" || name == "log");
                foreach (ITypedElement item in results)
                {
                    var fhirValue = item.Annotation <IFhirValueProvider>();
                    System.Diagnostics.Trace.WriteLine($"--({fhirValue.FhirValue.GetType().Name}): {item.Value} {fhirValue.FhirValue}");
                    traced = true;
                }
            };
            result = nav.Select("Resource.trace('id', id).meta.trace('log', lastUpdated).lastUpdated", ctx);
            Assert.IsNotNull(result.FirstOrDefault());
            Assert.AreEqual(PartialDateTime.Parse("2018-05-24T14:48:00+00:00"), result.First().Value);
            Assert.IsTrue(traced);
        }
Ejemplo n.º 21
0
        public void GivenAPartialDateTime_WhenInitialized_ThenCorrectStartDateTimeAndEndDateTimeShouldBeAssigned(string input, string start, string end)
        {
            PartialDateTime inputDateTime         = PartialDateTime.Parse(input);
            DateTimeOffset  expectedStartDateTime = DateTimeOffset.Parse(start);
            DateTimeOffset  expectedEndDateTime   = DateTimeOffset.Parse(end);

            DateTimeSearchValue value = new DateTimeSearchValue(inputDateTime);

            Assert.Equal(expectedStartDateTime, value.Start);
            Assert.Equal(expectedEndDateTime, value.End);
        }
Ejemplo n.º 22
0
 public Hl7.FhirPath.PartialDateTime?ToPartialDateTime()
 {
     if (Value != null)
     {
         return(PartialDateTime.Parse(Value));
     }
     else
     {
         return(null);
     }
 }
Ejemplo n.º 23
0
        public void ConvertToString()
        {
            var inputs = ElementNode.CreateList("hoi", 4L, 3.4m, true, false, PartialTime.Parse("15:47:00+01:00"),
                                                PartialDateTime.Parse("2019-01-11T15:47:00+01:00"));
            var vals = new[] { "hoi", "4", "3.4", "true", "false", "15:47:00+01:00", "2019-01-11T15:47:00+01:00" };

            inputs.Zip(vals, (i, v) => (i, v))
            .ToList()
            .ForEach(c => Assert.AreEqual(c.v, c.i.ToString()));
            inputs.ToList().ForEach(c => Assert.IsTrue(c.ConvertsToString()));
        }
        protected override IEnumerable <ISearchValue> ConvertTo(Period value)
        {
            PartialDateTime start = string.IsNullOrWhiteSpace(value.Start) ?
                                    PartialDateTime.MinValue :
                                    PartialDateTime.Parse(value.Start);

            PartialDateTime end = string.IsNullOrWhiteSpace(value.End) ?
                                  PartialDateTime.MaxValue :
                                  PartialDateTime.Parse(value.End);

            yield return(new DateTimeSearchValue(start, end));
        }
Ejemplo n.º 25
0
        public void TimeEquality()
        {
            Assert.True(PartialDateTime.Parse("2015-01-01") == PartialDateTime.Parse("2015-01-01"));
            Assert.True(PartialDateTime.Parse("2015-01-01") != PartialDateTime.Parse("2015-01"));
            Assert.True(PartialDateTime.Parse("2015-01-01T13:40:50+02:00") == PartialDateTime.Parse("2015-01-01T13:40:50+02:00"));
            Assert.True(PartialDateTime.Parse("2015-01-01T13:40:50+00:00") == PartialDateTime.Parse("2015-01-01T13:40:50Z"));
            Assert.True(PartialDateTime.Parse("2015-01-01T13:40:50+00:10") != PartialDateTime.Parse("2015-01-01T13:40:50Z"));
            Assert.True(PartialDateTime.Parse("2015-01-01T13:40:50+00:10") != PartialDateTime.Parse("2015-01-01"));

            Assert.True(Time.Parse("13:45:02Z") == Time.Parse("13:45:02+00:00"));
            Assert.True(Time.Parse("13:45:02+01:00") == Time.Parse("13:45:02+01:00"));
            Assert.True(Time.Parse("13:45:02+00:00") != Time.Parse("13:45:02+01:00"));
        }
Ejemplo n.º 26
0
        public void GivenADateWithComparatorOfSingleBinaryOperator_WhenBuilt_ThenCorrectExpressionShouldBeCreated(string prefix, string dateTimeInput, FieldName fieldName, BinaryOperator binaryOperator, bool expectStartTimeValue)
        {
            var partialDateTime     = PartialDateTime.Parse(dateTimeInput);
            var dateTimeSearchValue = new DateTimeSearchValue(partialDateTime);

            Validate(
                CreateSearchParameter(SearchParamType.Date),
                null,
                prefix + dateTimeInput,
                e => ValidateDateTimeBinaryOperatorExpression(
                    e,
                    fieldName,
                    binaryOperator,
                    expectStartTimeValue ? dateTimeSearchValue.Start : dateTimeSearchValue.End));
        }
Ejemplo n.º 27
0
        public void GivenADateWithNeComparator_WhenBuilt_ThenCorrectExpressionShouldBeCreated(string dateTimeInput)
        {
            var partialDateTime     = PartialDateTime.Parse(dateTimeInput);
            var dateTimeSearchValue = new DateTimeSearchValue(partialDateTime);

            Validate(
                CreateSearchParameter(SearchParamType.Date),
                null,
                "ne" + dateTimeInput,
                e => ValidateMultiaryExpression(
                    e,
                    MultiaryOperator.Or,
                    e1 => ValidateDateTimeBinaryOperatorExpression(e1, FieldName.DateTimeStart, BinaryOperator.LessThan, dateTimeSearchValue.Start),
                    e2 => ValidateDateTimeBinaryOperatorExpression(e2, FieldName.DateTimeEnd, BinaryOperator.GreaterThan, dateTimeSearchValue.End)));
        }
        public void GivenADateTimeSearchValue_WhenGenerated_ThenCorrectJObjectShouldBeCreated()
        {
            var value = new DateTimeSearchValue(PartialDateTime.Parse("2000"), PartialDateTime.Parse("2001"));

            var expectedValues = new[]
            {
                CreateTuple("st", "2000-01-01T00:00:00.0000000+00:00"),
                CreateTuple("et", "2001-12-31T23:59:59.9999999+00:00"),
            };

            TestAndValidateOutput(
                "date",
                value,
                expectedValues);
        }
        public void TestCompare()
        {
            Assert.Equal(0, MinMaxValidationExtensions.Compare(PartialDateTime.Parse("1972-11-30"), new Model.FhirDateTime(1972, 11, 30)));
            Assert.Equal(1, MinMaxValidationExtensions.Compare(PartialDateTime.Parse("1972-12-01"), new Model.Date(1972, 11, 30)));
            Assert.Equal(-1,
                         MinMaxValidationExtensions.Compare(PartialDateTime.Parse("1972-12-01T13:00:00Z"),
                                                            new Model.Instant(new DateTimeOffset(1972, 12, 01, 14, 00, 00, TimeSpan.Zero))));
            Assert.Equal(0, MinMaxValidationExtensions.Compare(Hl7.FhirPath.Time.Parse("12:00:00Z"), new Model.Time("12:00:00Z")));
            Assert.Equal(1, MinMaxValidationExtensions.Compare(3.14m, new Model.FhirDecimal(2.14m)));
            Assert.Equal(-1, MinMaxValidationExtensions.Compare(-3L, new Model.Integer(3)));
            Assert.Equal(-1, MinMaxValidationExtensions.Compare("aaa", new Model.FhirString("bbb")));
            Assert.Equal(1, MinMaxValidationExtensions.Compare(new Hl7.FhirPath.Quantity(5.0m, "kg"), new Model.Quantity(4.0m, "kg")));

            Assert.Throws <NotSupportedException>(() => MinMaxValidationExtensions.Compare(PartialDateTime.Parse("1972-11-30"), new Model.Quantity(4.0m, "kg")));
        }
        protected override IEnumerable <ISearchValue> Convert(ITypedElement value)
        {
            var startString = value.Scalar("start")?.ToString();
            var endString   = value.Scalar("end")?.ToString();

            PartialDateTime start = string.IsNullOrWhiteSpace(startString) ?
                                    PartialDateTime.MinValue :
                                    PartialDateTime.Parse(startString);

            PartialDateTime end = string.IsNullOrWhiteSpace(endString) ?
                                  PartialDateTime.MaxValue :
                                  PartialDateTime.Parse(endString);

            yield return(new DateTimeSearchValue(start, end));
        }