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)); }
/// <summary> /// FhirPath toDateTime() function. /// </summary> /// <param name="focus"></param> /// <returns></returns> public static PartialDateTime?ToDateTime(this ITypedElement focus) { var val = focus?.Value; if (val == null) { return(null); } switch (val) { case PartialDateTime pdt: return(pdt); case string s: return(convertString(s)); default: return(null); } PartialDateTime?convertString(string si) => PartialDateTime.TryParse(si, out var result) ? result : (PartialDateTime?)null; }
private static object parseValue(string rep) { if (rep.ToLower() == "true") { return(true); } if (rep.ToLower() == "false") { return(false); } if (rep.Contains("-") || rep.Contains(":")) { PartialDateTime dt; if (PartialDateTime.TryParse(rep, out dt)) { return(dt); } } if (rep.Contains(".")) { try { return(XmlConvert.ToDecimal(rep)); } catch { ; // Fall through to next case } } try { return(XmlConvert.ToInt64(rep)); } catch { ; // Fall through to next case } return(rep); // If all else fails, it's probably just a string }