예제 #1
0
        public void ToDateTimeOffset()
        {
            var plusOne = new TimeSpan(1, 0, 0);
            var plusTwo = new TimeSpan(2, 0, 0);

            var pt  = PartialTime.Parse("13:45:56");
            var dto = pt.ToDateTimeOffset(2019, 7, 23, plusOne);

            Assert.AreEqual(2019, dto.Year);
            Assert.AreEqual(7, dto.Month);
            Assert.AreEqual(23, dto.Day);
            Assert.AreEqual(13, dto.Hour);
            Assert.AreEqual(45, dto.Minute);
            Assert.AreEqual(56, dto.Second);
            Assert.AreEqual(plusOne, dto.Offset);

            pt  = PartialTime.Parse("13:45:56.456+02:00");
            dto = pt.ToDateTimeOffset(2019, 7, 23, plusOne);
            Assert.AreEqual(13, dto.Hour);
            Assert.AreEqual(45, dto.Minute);
            Assert.AreEqual(56, dto.Second);
            Assert.AreEqual(456, dto.Millisecond);
            Assert.AreEqual(plusTwo, dto.Offset);

            pt  = PartialTime.Parse("13+02:00");
            dto = pt.ToDateTimeOffset(2019, 7, 23, plusOne);
            Assert.AreEqual(13, dto.Hour);
            Assert.AreEqual(0, dto.Minute);
            Assert.AreEqual(0, dto.Second);
            Assert.AreEqual(plusTwo, dto.Offset);
        }
예제 #2
0
 public void TimeComparison()
 {
     Assert.IsTrue(PartialTime.Parse("13:00:00Z") > PartialTime.Parse("12:00:00Z"));
     Assert.IsTrue(PartialTime.Parse("13:00:00Z") < PartialTime.Parse("18:00:00+02:00"));
     Assert.IsTrue(PartialTime.Parse("12:34:00+00:00") > PartialTime.Parse("12:33:55+00:00"));
     Assert.IsTrue(PartialTime.Parse("13:00:00+00:00") < PartialTime.Parse("15:01:00+02:00"));
     Assert.IsTrue(PartialTime.Parse("13:00:00+00:00") > PartialTime.Parse("14:59:00+02:00"));
 }
예제 #3
0
 public void CheckOrdering()
 {
     Assert.AreEqual(1, PartialTime.Parse("13:00:00Z").CompareTo(PartialTime.Parse("12:00:00Z")));
     Assert.AreEqual(-1, PartialTime.Parse("13:00:00Z").CompareTo(PartialTime.Parse("18:00:00+02:00")));
     Assert.AreEqual(1, PartialTime.Parse("12:34:00+00:00").CompareTo(PartialTime.Parse("12:33:55+00:00")));
     Assert.AreEqual(-1, PartialTime.Parse("13:00:00+00:00").CompareTo(PartialTime.Parse("15:01:00+02:00")));
     Assert.AreEqual(0, PartialTime.Parse("13:45:02+01:00").CompareTo(PartialTime.Parse("13:45:02+01:00")));
 }
예제 #4
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(PartialTime.Parse("12:34:00+00:00") > PartialTime.Parse("12:33:55+00:00"));
            Assert.True(PartialTime.Parse("13:00:00+00:00") < PartialTime.Parse("15:01:00+02:00"));
            Assert.True(PartialTime.Parse("13:00:00+00:00") > PartialTime.Parse("14:59:00+02:00"));
        }
예제 #5
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, PartialTime.Parse("12:34:00+00:00").CompareTo(PartialTime.Parse("12:33:55+00:00")));
            Assert.Equal(-1, PartialTime.Parse("13:00:00+00:00").CompareTo(PartialTime.Parse("15:01:00+02:00")));
            Assert.Equal(0, PartialTime.Parse("13:45:02+01:00").CompareTo(PartialTime.Parse("13:45:02+01:00")));
        }
예제 #6
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()));
        }
예제 #7
0
        public void TimeConstructor()
        {
            PartialTime.Parse("12:34:44+02:00");
            PartialTime.Parse("12:34:44");
            PartialTime.Parse("12:34:44Z");

            Assert.True(PartialTime.TryParse("12:34:44Z", out PartialTime pd));
            Assert.Equal(pd, PartialTime.Parse("12:34:44Z"));
            Assert.Equal("12:34:44Z", pd.ToString());

            Assert.False(PartialTime.TryParse("92:34:44Z", out pd));
        }
예제 #8
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(PartialTime.Parse("13:45:02Z") == PartialTime.Parse("13:45:02+00:00"));
            Assert.True(PartialTime.Parse("13:45:02+01:00") == PartialTime.Parse("13:45:02+01:00"));
            Assert.True(PartialTime.Parse("13:45:02+00:00") != PartialTime.Parse("13:45:02+01:00"));
        }
예제 #9
0
        public void FhirPath_Gramm_Literal()
        {
            var parser = Grammar.Literal.End();

            AssertParser.SucceedsMatch(parser, "'hi there'", new ConstantExpression("hi there"));
            AssertParser.SucceedsMatch(parser, "3", new ConstantExpression(3L));
            AssertParser.SucceedsMatch(parser, "3.14", new ConstantExpression(3.14m));
            AssertParser.SucceedsMatch(parser, "@2013-12", new ConstantExpression(PartialDateTime.Parse("2013-12")));
            AssertParser.SucceedsMatch(parser, "@T12:23:34Z", new ConstantExpression(PartialTime.Parse("12:23:34Z")));
            AssertParser.SucceedsMatch(parser, "true", new ConstantExpression(true));
            AssertParser.SucceedsMatch(parser, "@2014-12-13T12:00:00+02:00", new ConstantExpression(PartialDateTime.Parse("2014-12-13T12:00:00+02:00")));

            AssertParser.FailsMatch(parser, "%constant");
            AssertParser.FailsMatch(parser, "\"quotedstring\"");
            AssertParser.FailsMatch(parser, "A23identifier");
        }
예제 #10
0
        public void ConvertToDateTime()
        {
            var now    = PartialDateTime.Parse("2019-01-11T15:47:00+01:00");
            var inputs = ElementNode.CreateList(new DateTimeOffset(2019, 1, 11, 15, 47, 00, new TimeSpan(1, 0, 0)),
                                                "2019-01", "2019-01-11T15:47:00+01:00");
            var vals = new[] { now, PartialDateTime.Parse("2019-01"), now };

            inputs.Zip(vals, (i, v) => (i, v))
            .ToList()
            .ForEach(c => Assert.AreEqual(c.i.ToDateTime(), c.v));
            inputs.ToList().ForEach(c => Assert.IsTrue(c.ConvertsToDateTime()));

            var wrong = ElementNode.CreateList("hi", 2.6m, false, PartialTime.Parse("16:05:49")).ToList();

            wrong.ForEach(c => Assert.IsNull(c.ToDateTime()));
            wrong.ForEach(c => Assert.IsFalse(c.ConvertsToDateTime()));
        }
예제 #11
0
        public void FhirPath_Lex_Time()
        {
            var parser = Lexer.Time.End();

            accept("@T12:34:34.345674");
            accept("@T12:34:34");
            accept("@T12:35");
            accept("@T12");

            reject("@T12:34:34+02:30");
            reject("@T12:34:00Z");
            reject("2001-01-01T12:34:34+02:30");
            reject("@2001-01-01T12:34:34+02:30");
            reject("T12:34:34+02:30");
            reject("12:34:34+02:30");
            reject("@12:34:34+02:30");
            reject("@T12:34:34+48:30");

            void accept(string s) => AssertParser.SucceedsMatch(parser, s, PartialTime.Parse(s.Substring(2)));
            void reject(string s) => AssertParser.FailsMatch(parser, s);
        }
        private static object convertXmlStringToPrimitive(Type to, string value)
        {
            if (typeof(Boolean) == to)
            {
                return(XmlConvert.ToBoolean(value));
            }
            if (typeof(Byte) == to)
            {
                return(XmlConvert.ToByte(value));        // Not used in FHIR serialization
            }
            if (typeof(Char) == to)
            {
                return(XmlConvert.ToChar(value));        // Not used in FHIR serialization
            }
            if (typeof(DateTime) == to)
            {
                return(ConvertToDatetimeOffset(value).UtcDateTime);  // Obsolete: use DateTimeOffset instead!!
            }
            if (typeof(Decimal) == to)
            {
                return(XmlConvert.ToDecimal(value));
            }
            if (typeof(Double) == to)
            {
                return(XmlConvert.ToDouble(value));      // Could lead to loss in precision
            }
            if (typeof(Int16) == to)
            {
                return(XmlConvert.ToInt16(value));       // Could lead to loss in precision
            }
            if (typeof(Int32) == to)
            {
                return(XmlConvert.ToInt32(value));
            }
            if (typeof(Int64) == to)
            {
                return(XmlConvert.ToInt64(value));       // Not used in FHIR serialization
            }
            if (typeof(SByte) == to)
            {
                return(XmlConvert.ToSByte(value));       // Not used in FHIR serialization
            }
            if (typeof(Single) == to)
            {
                return(XmlConvert.ToSingle(value));      // Not used in FHIR serialization
            }
            if (typeof(UInt16) == to)
            {
                return(XmlConvert.ToUInt16(value));      // Not used in FHIR serialization
            }
            if (typeof(UInt32) == to)
            {
                return(XmlConvert.ToUInt32(value));      // Not used in FHIR serialization
            }
            if (typeof(UInt64) == to)
            {
                return(XmlConvert.ToUInt64(value));      // Not used in FHIR serialization
            }
            if (typeof(byte[]) == to)
            {
                return(System.Convert.FromBase64String(value));
            }
            if (typeof(DateTimeOffset) == to)
            {
                return(ConvertToDatetimeOffset(value));
            }
            if (typeof(System.Uri) == to)
            {
                return(new Uri(value, UriKind.RelativeOrAbsolute));
            }
            if (typeof(PartialDateTime) == to)
            {
                return(PartialDateTime.Parse(value));
            }
            if (typeof(PartialTime) == to)
            {
                return(PartialTime.Parse(value));
            }
            if (typeof(BigInteger) == to)
            {
                return(BigInteger.Parse(value));
            }
            if (to.IsEnum())
            {
                var result = EnumUtility.ParseLiteral(value, to);
                if (result == null)
                {
                    throw Error.NotSupported($"String value '{value}' is not a known literal in enum '{to.Name}'");
                }

                return(result);
            }

            throw Error.NotSupported($"Cannot convert string value '{value}' to a {to.Name}");
        }
        private static object convertXmlStringToPrimitive(Type to, string value)
        {
            if (typeof(Boolean) == to)
            {
                return(XmlConvert.ToBoolean(value));
            }
            if (typeof(Byte) == to)
            {
                return(XmlConvert.ToByte(value));        // Not used in FHIR serialization
            }
            if (typeof(Char) == to)
            {
                return(XmlConvert.ToChar(value));        // Not used in FHIR serialization
            }
            if (typeof(DateTime) == to)
            {
                return(ConvertToDatetimeOffset(value).UtcDateTime);  // Obsolete: use DateTimeOffset instead!!
            }
            if (typeof(Decimal) == to)
            {
                if (FORBIDDEN_DECIMAL_PREFIXES.Any(prefix => value.StartsWith(prefix)) || value.EndsWith("."))
                {
                    // decimal cannot start with '+', '-' or '00' and cannot end with '.'
                    throw new FormatException("Input string was not in a correct format.");
                }
                return(decimal.Parse(value, NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent | NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture));
            }
            if (typeof(Double) == to)
            {
                return(XmlConvert.ToDouble(value));      // Could lead to loss in precision
            }
            if (typeof(Int16) == to)
            {
                return(XmlConvert.ToInt16(value));       // Could lead to loss in precision
            }
            if (typeof(Int32) == to)
            {
                return(XmlConvert.ToInt32(value));
            }
            if (typeof(Int64) == to)
            {
                return(XmlConvert.ToInt64(value));       // Not used in FHIR serialization
            }
            if (typeof(SByte) == to)
            {
                return(XmlConvert.ToSByte(value));       // Not used in FHIR serialization
            }
            if (typeof(Single) == to)
            {
                return(XmlConvert.ToSingle(value));      // Not used in FHIR serialization
            }
            if (typeof(UInt16) == to)
            {
                return(XmlConvert.ToUInt16(value));      // Not used in FHIR serialization
            }
            if (typeof(UInt32) == to)
            {
                return(XmlConvert.ToUInt32(value));      // Not used in FHIR serialization
            }
            if (typeof(UInt64) == to)
            {
                return(XmlConvert.ToUInt64(value));      // Not used in FHIR serialization
            }
            if (typeof(byte[]) == to)
            {
                return(System.Convert.FromBase64String(value));
            }
            if (typeof(DateTimeOffset) == to)
            {
                return(ConvertToDatetimeOffset(value));
            }
            if (typeof(System.Uri) == to)
            {
                return(new Uri(value, UriKind.RelativeOrAbsolute));
            }
            if (typeof(PartialDateTime) == to)
            {
                return(PartialDateTime.Parse(value));
            }
            if (typeof(PartialTime) == to)
            {
                return(PartialTime.Parse(value));
            }
            if (typeof(BigInteger) == to)
            {
                return(BigInteger.Parse(value));
            }
            if (to.IsEnum())
            {
                var result = EnumUtility.ParseLiteral(value, to);
                if (result == null)
                {
                    throw Error.NotSupported($"String value '{value}' is not a known literal in enum '{to.Name}'");
                }

                return(result);
            }

            throw Error.NotSupported($"Cannot convert string value '{value}' to a {to.Name}");
        }
예제 #14
0
 private void SucceedsTime(Parser <PartialTime> parser, string s)
 {
     AssertParser.SucceedsMatch(parser, s, PartialTime.Parse(s.Substring(2)));
 }