/// <summary>
        /// FhirPath toTime() function.
        /// </summary>
        /// <param name="focus"></param>
        /// <returns></returns>
        public static PartialTime?ToTime(this ITypedElement focus)
        {
            var val = focus?.Value;

            if (val == null)
            {
                return(null);
            }

            switch (val)
            {
            case PartialTime pt:
                return(pt);

            case string s:
                return(convertString(s));

            default:
                return(null);
            }

            PartialTime?convertString(string si)
            {
                // Inconsistenty, the format for a time requires the 'T' prefix, while
                // convertsToDateTime() does not expect a '@'.
                if (!si.StartsWith("T"))
                {
                    return(null);
                }

                return(PartialTime.TryParse(si.Substring(1), out var result) ?
                       result : (PartialTime?)null);
            }
        }
Exemple #2
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));
        }
        public void TimeConstructor()
        {
            accept("12:34:44.123456+02:00", 12, 34, 44, 123, new TimeSpan(2, 0, 0));
            accept("12:34:44.1+02:00", 12, 34, 44, 100, new TimeSpan(2, 0, 0));
            accept("12:34:44+02:00", 12, 34, 44, null, new TimeSpan(2, 0, 0));
            accept("12:34:44Z", 12, 34, 44, null, TimeSpan.Zero);
            accept("12:34:44+00:00", 12, 34, 44, null, TimeSpan.Zero);
            accept("12:34:44", 12, 34, 44, null, null);

            accept("12:34Z", 12, 34, null, null, TimeSpan.Zero);
            accept("12:34", 12, 34, null, null, null);
            accept("12", 12, null, null, null, null);
            accept("12Z", 12, null, null, null, TimeSpan.Zero);
            accept("12-04:30", 12, null, null, null, new TimeSpan(-4, -30, 0));

            reject("");
            reject("+05:00");
            reject("Z");
            reject("12:34.1234");
            reject("Hi12:34:44");
            reject("12:34:44there");
            reject("12:34:44+A");
            reject("12:34:44+345:432");
            reject("92:34:44");
            reject("12:34:AM");

            void accept(string testValue, int?h, int?m, int?s, int?ms, TimeSpan?o)
            {
                Assert.IsTrue(PartialTime.TryParse(testValue, out PartialTime parsed), "TryParse");
                Assert.AreEqual(h, parsed.Hours, "hours");
                Assert.AreEqual(m, parsed.Minutes, "minutes");
                Assert.AreEqual(s, parsed.Seconds, "seconds");
                Assert.AreEqual(ms, parsed.Millis, "millis");
                Assert.AreEqual(o, parsed.Offset, "offset");
                Assert.AreEqual(testValue, parsed.ToString(), "ToString");
            }

            void reject(string testValue)
            {
                Assert.IsFalse(PartialTime.TryParse(testValue, out _));
            }
        }