From 619f9e501fcc4cce70359d3da7f1912e26d041c4 Mon Sep 17 00:00:00 2001 From: Ewout Kramer Date: Fri, 6 Dec 2024 17:06:53 +0100 Subject: [PATCH 1/3] Now throws in Date when value is null. --- src/Hl7.Fhir.Base/Model/Date.cs | 184 ++++++++-------- src/Hl7.Fhir.Base/Model/FhirDateTime.cs | 274 ++++++++++++------------ src/Hl7.Fhir.Base/Model/Time.cs | 160 +++++++------- 3 files changed, 299 insertions(+), 319 deletions(-) diff --git a/src/Hl7.Fhir.Base/Model/Date.cs b/src/Hl7.Fhir.Base/Model/Date.cs index 97d4f26006..c0a6960da6 100644 --- a/src/Hl7.Fhir.Base/Model/Date.cs +++ b/src/Hl7.Fhir.Base/Model/Date.cs @@ -27,129 +27,121 @@ POSSIBILITY OF SUCH DAMAGE. */ +#nullable enable using System; using System.Diagnostics.CodeAnalysis; using P = Hl7.Fhir.ElementModel.Types; -#nullable enable +namespace Hl7.Fhir.Model; -namespace Hl7.Fhir.Model +public partial class Date { - public partial class Date + public Date(int year, int month, int day) + : this(string.Format(System.Globalization.CultureInfo.InvariantCulture, FhirDateTime.FMT_YEARMONTHDAY, year, month, day)) { - public Date(int year, int month, int day) - : this(string.Format(System.Globalization.CultureInfo.InvariantCulture, FhirDateTime.FMT_YEARMONTHDAY, year, month, day)) - { - } + } - public Date(int year, int month) - : this(string.Format(System.Globalization.CultureInfo.InvariantCulture, FhirDateTime.FMT_YEARMONTH, year, month)) - { - } + public Date(int year, int month) + : this(string.Format(System.Globalization.CultureInfo.InvariantCulture, FhirDateTime.FMT_YEARMONTH, year, month)) + { + } - public Date(int year) : this(string.Format(System.Globalization.CultureInfo.InvariantCulture, FhirDateTime.FMT_YEAR, year)) - { - } + public Date(int year) : this(string.Format(System.Globalization.CultureInfo.InvariantCulture, FhirDateTime.FMT_YEAR, year)) + { + } - public static Date FromDateTimeOffset(DateTimeOffset date) => new(date.Year, date.Month, date.Day); + public static Date FromDateTimeOffset(DateTimeOffset date) => new(date.Year, date.Month, date.Day); - /// - /// Gets the current date in the local timezone. - /// - public static Date Today() => FromDateTimeOffset(DateTimeOffset.Now); + /// + /// Gets the current date in the local timezone. + /// + public static Date Today() => FromDateTimeOffset(DateTimeOffset.Now); - /// - /// Gets the current date in UTC. - /// - public static Date UtcToday() => FromDateTimeOffset(DateTimeOffset.UtcNow); + /// + /// Gets the current date in UTC. + /// + public static Date UtcToday() => FromDateTimeOffset(DateTimeOffset.UtcNow); - [NonSerialized] // To prevent binary serialization from serializing this field - private P.Date? _parsedValue = null; + [NonSerialized] // To prevent binary serialization from serializing this field + private P.Date? _parsedValue = null; - // This is a sentinel value that marks that the current string representation is - // not parseable, so we don't have to try again. It's value is never used, it's just - // checked by reference. - private static readonly P.Date INVALID_VALUE = P.Date.FromDateTimeOffset(DateTimeOffset.MinValue); + // This is a sentinel value that marks that the current string representation is + // not parseable, so we don't have to try again. It's value is never used, it's just + // checked by reference. + private static readonly P.Date INVALID_VALUE = P.Date.FromDateTimeOffset(DateTimeOffset.MinValue); - /// - /// Converts a Fhir Date to a . - /// - /// true if the Fhir Date contains a valid date string, false otherwise. - public bool TryToDate([NotNullWhen(true)] out P.Date? date) + /// + /// Converts a Fhir Date to a . + /// + /// true if the Fhir Date contains a valid date string, false otherwise. + public bool TryToDate([NotNullWhen(true)] out P.Date? date) + { + if (_parsedValue is null) { - if (_parsedValue is null) - { - if (Value is not null && !(P.Date.TryParse(Value, out _parsedValue) && !_parsedValue!.HasOffset)) - _parsedValue = INVALID_VALUE; - } - - if (hasInvalidParsedValue()) - { - date = null; - return false; - } - else - { - date = _parsedValue!; - return true; - } - - bool hasInvalidParsedValue() => ReferenceEquals(_parsedValue, INVALID_VALUE); + if (Value is not null && !(P.Date.TryParse(Value, out _parsedValue) && !_parsedValue!.HasOffset)) + _parsedValue = INVALID_VALUE; } - /// - /// Converts a Fhir Date to a . - /// - /// The Date, or null if the is null. - /// Thrown when the Value does not contain a valid FHIR Date. - public P.Date? ToDate() => TryToDate(out var dt) ? dt : throw new FormatException($"String '{Value}' was not recognized as a valid date."); - - protected override void OnObjectValueChanged() + if (hasInvalidParsedValue()) { - _parsedValue = null; - base.OnObjectValueChanged(); + date = null; + return false; } - /// - /// Converts this Fhir Fhir Date to a . - /// - /// A DateTimeOffset filled out to midnight, january 1 (UTC) in case of a partial date. - public DateTimeOffset? ToDateTimeOffset() - { - if (Value == null) return null; // Note: this behaviour is inconsistent with ToDateTimeOffset() in FhirDateTime + date = _parsedValue!; + return true; - // ToDateTimeOffset() will convert partial date/times by filling out to midnight/january 1 UTC - if (!TryToDate(out var dt)) - throw new FormatException($"Date '{Value}' was not recognized as a valid datetime."); + bool hasInvalidParsedValue() => ReferenceEquals(_parsedValue, INVALID_VALUE); + } - // Since Value is not null and the parsed value is valid, dto will not be null - return dt!.ToDateTimeOffset(TimeSpan.Zero); - } + /// + /// Converts a Fhir Date to a . + /// + /// The Date, or null if the is null. + /// Thrown when the Value does not contain a valid FHIR Date. + public P.Date ToDate() => TryToDate(out var dt) ? dt : throw new FormatException($"String '{Value}' was not recognized as a valid date."); + + protected override void OnObjectValueChanged() + { + _parsedValue = null; + base.OnObjectValueChanged(); + } - /// - /// Convert this Fhir Date to a . - /// - /// True if the value of the Fhir Date is not null and can be parsed as a DateTimeOffset, false otherwise. - public bool TryToDateTimeOffset(out DateTimeOffset dto) + /// + /// Converts this Fhir Fhir Date to a . + /// + /// A DateTimeOffset filled out to midnight, january 1 (UTC) in case of a partial date. + public DateTimeOffset ToDateTimeOffset() + { + if (Value == null) throw new InvalidOperationException("Date's value is null."); + + // TryToDate() will convert partial date/times by filling out to midnight/january 1 UTC + if (!TryToDate(out var dt)) + throw new FormatException($"Date '{Value}' was not recognized as a valid datetime."); + + // Since Value is not null and the parsed value is valid, dto will not be null + return dt.ToDateTimeOffset(TimeSpan.Zero); + } + + /// + /// Convert this Fhir Date to a . + /// + /// True if the value of the Fhir Date is not null and can be parsed as a DateTimeOffset, false otherwise. + public bool TryToDateTimeOffset(out DateTimeOffset dto) + { + if (Value is not null && TryToDate(out var dt)) { - if (Value is not null && TryToDate(out var dt)) - { - dto = dt.ToDateTimeOffset(TimeSpan.Zero); - return true; - } - else - { - dto = default; - return false; - } + dto = dt.ToDateTimeOffset(TimeSpan.Zero); + return true; } - /// - /// Checks whether the given literal is correctly formatted. - /// - public static bool IsValidValue(string value) => P.Date.TryParse(value, out var parsed) && !parsed.HasOffset; + dto = default; + return false; } -} -#nullable restore \ No newline at end of file + /// + /// Checks whether the given literal is correctly formatted. + /// + public static bool IsValidValue(string value) => P.Date.TryParse(value, out var parsed) && !parsed.HasOffset; +} \ No newline at end of file diff --git a/src/Hl7.Fhir.Base/Model/FhirDateTime.cs b/src/Hl7.Fhir.Base/Model/FhirDateTime.cs index f7136581e9..0bf3974afb 100644 --- a/src/Hl7.Fhir.Base/Model/FhirDateTime.cs +++ b/src/Hl7.Fhir.Base/Model/FhirDateTime.cs @@ -35,172 +35,164 @@ POSSIBILITY OF SUCH DAMAGE. using System.Diagnostics.CodeAnalysis; using P = Hl7.Fhir.ElementModel.Types; -namespace Hl7.Fhir.Model +namespace Hl7.Fhir.Model; + +public partial class FhirDateTime { - public partial class FhirDateTime + /// + /// A string.Format pattern to use when formatting a full datetime with timezone. + /// + public const string FMT_FULL = "yyyy-MM-dd'T'HH:mm:ssK"; + + /// + /// A string.Format pattern to use when formatting a year. + /// + public const string FMT_YEAR = "{0:D4}"; + + /// + /// A string.Format pattern to use when formatting a year and month. + /// + public const string FMT_YEARMONTH = "{0:D4}-{1:D2}"; + + /// + /// A string.Format pattern to use when formatting a date. + /// + public const string FMT_YEARMONTHDAY = "{0:D4}-{1:D2}-{2:D2}"; + + public FhirDateTime(DateTimeOffset dt) : this(PrimitiveTypeConverter.ConvertTo(dt)) { - /// - /// A string.Format pattern to use when formatting a full datetime with timezone. - /// - public const string FMT_FULL = "yyyy-MM-dd'T'HH:mm:ssK"; - - /// - /// A string.Format pattern to use when formatting a year. - /// - public const string FMT_YEAR = "{0:D4}"; - - /// - /// A string.Format pattern to use when formatting a year and month. - /// - public const string FMT_YEARMONTH = "{0:D4}-{1:D2}"; - - /// - /// A string.Format pattern to use when formatting a date. - /// - public const string FMT_YEARMONTHDAY = "{0:D4}-{1:D2}-{2:D2}"; - - public FhirDateTime(DateTimeOffset dt) : this(PrimitiveTypeConverter.ConvertTo(dt)) - { - } + } - public FhirDateTime(int year, int month, int day, int hr, int min, int sec, TimeSpan offset) - : this(new DateTimeOffset(year, month, day, hr, min, sec, offset)) - { - } + public FhirDateTime(int year, int month, int day, int hr, int min, int sec, TimeSpan offset) + : this(new DateTimeOffset(year, month, day, hr, min, sec, offset)) + { + } - public FhirDateTime(int year, int month, int day) - : this(string.Format(System.Globalization.CultureInfo.InvariantCulture, FMT_YEARMONTHDAY, year, month, day)) - { - } + public FhirDateTime(int year, int month, int day) + : this(string.Format(System.Globalization.CultureInfo.InvariantCulture, FMT_YEARMONTHDAY, year, month, day)) + { + } - public FhirDateTime(int year, int month) - : this(string.Format(System.Globalization.CultureInfo.InvariantCulture, FMT_YEARMONTH, year, month)) - { - } + public FhirDateTime(int year, int month) + : this(string.Format(System.Globalization.CultureInfo.InvariantCulture, FMT_YEARMONTH, year, month)) + { + } - public FhirDateTime(int year) - : this(string.Format(System.Globalization.CultureInfo.InvariantCulture, FMT_YEAR, year)) - { - } + public FhirDateTime(int year) + : this(string.Format(System.Globalization.CultureInfo.InvariantCulture, FMT_YEAR, year)) + { + } - public static FhirDateTime Now() => new(DateTimeOffset.Now); + public static FhirDateTime Now() => new(DateTimeOffset.Now); - [NonSerialized] // To prevent binary serialization from serializing this field - private P.DateTime? _parsedValue = null; + [NonSerialized] // To prevent binary serialization from serializing this field + private P.DateTime? _parsedValue = null; - // This is a sentintel value that marks that the current string representation is - // not parseable, so we don't have to try again. It's value is never used, it's just - // checked by reference. - private static readonly P.DateTime INVALID_VALUE = P.DateTime.FromDateTimeOffset(DateTimeOffset.MinValue); + // This is a sentintel value that marks that the current string representation is + // not parseable, so we don't have to try again. It's value is never used, it's just + // checked by reference. + private static readonly P.DateTime INVALID_VALUE = P.DateTime.FromDateTimeOffset(DateTimeOffset.MinValue); - /// - /// Converts a FhirDateTime to a . - /// - /// true if the FhirDateTime contains a valid date/time string, false otherwise. - public bool TryToDateTime([NotNullWhen(true)] out P.DateTime? dateTime) + /// + /// Converts a FhirDateTime to a . + /// + /// true if the FhirDateTime contains a valid date/time string, false otherwise. + public bool TryToDateTime([NotNullWhen(true)] out P.DateTime? dateTime) + { + if (_parsedValue is null) { - if (_parsedValue is null) - { - if (Value is not null && !P.DateTime.TryParse(Value, out _parsedValue)) - _parsedValue = INVALID_VALUE; - } - - if (hasInvalidParsedValue()) - { - dateTime = null; - return false; - } - else - { - dateTime = _parsedValue!; - return true; - } - - bool hasInvalidParsedValue() => ReferenceEquals(_parsedValue, INVALID_VALUE); + if (Value is not null && !P.DateTime.TryParse(Value, out _parsedValue)) + _parsedValue = INVALID_VALUE; } - /// - /// Converts a FhirDateTime to a . - /// - /// The DateTime, or null if the is null. - /// Thrown when the Value does not contain a valid FHIR DateTime. - public P.DateTime ToDateTime() => TryToDateTime(out var dt) ? dt : throw new FormatException($"String '{Value}' was not recognized as a valid datetime."); - - protected override void OnObjectValueChanged() + if (hasInvalidParsedValue()) { - _parsedValue = null; - base.OnObjectValueChanged(); + dateTime = null; + return false; } - - /// - /// Converts this Fhir FhirDateTime to a . - /// - /// Ensures the returned DateTimeOffset uses the the specified zone. - /// In .NET the minimal value for DateTimeOffset is 1/1/0001 12:00:00 AM +00:00. That means,for example, - /// a FhirDateTime of "0001-01-01T00:00:00+01:00" could not be converted to a DateTimeOffset. In that case a - /// ArgumentOutOfRangeException will be thrown. - /// A DateTimeOffset filled out to midnight, january 1 (UTC) in case of a partial date/time. If the Fhir DateTime - /// does not specify a timezone, the UTC (Coordinated Universal Time) is assumed. Note that the zone parameter has no - /// effect on this, this merely converts the given Fhir datetime to the desired timezone - public DateTimeOffset ToDateTimeOffset(TimeSpan zone) + else { - if (Value == null) throw new InvalidOperationException("FhirDateTime's value is null."); + dateTime = _parsedValue!; + return true; + } + + bool hasInvalidParsedValue() => ReferenceEquals(_parsedValue, INVALID_VALUE); + } - // ToDateTimeOffset() will convert partial date/times by filling out to midnight/january 1 UTC - // When there's no timezone, the UTC is assumed - if (!TryToDateTime(out var dt)) - throw new FormatException($"DateTime '{Value}' was not recognized as a valid datetime."); + /// + /// Converts a FhirDateTime to a . + /// + /// The DateTime, or null if the is null. + /// Thrown when the Value does not contain a valid FHIR DateTime. + public P.DateTime ToDateTime() => TryToDateTime(out var dt) ? dt : throw new FormatException($"String '{Value}' was not recognized as a valid datetime."); - // Since Value is not null and the parsed value is valid, dto will not be null - return dt!.ToDateTimeOffset(TimeSpan.Zero).ToOffset(zone); - } + protected override void OnObjectValueChanged() + { + _parsedValue = null; + base.OnObjectValueChanged(); + } - /// - /// Convert this FhirDateTime to a . - /// - /// True if the value of the FhirDateTime is not null, can be parsed as a DateTimeOffset and has a specified timezone, false otherwise. - public bool TryToDateTimeOffset(out DateTimeOffset dto) + /// + /// Converts this Fhir FhirDateTime to a . + /// + /// Ensures the returned DateTimeOffset uses the the specified zone. + /// In .NET the minimal value for DateTimeOffset is 1/1/0001 12:00:00 AM +00:00. That means,for example, + /// a FhirDateTime of "0001-01-01T00:00:00+01:00" could not be converted to a DateTimeOffset. In that case a + /// ArgumentOutOfRangeException will be thrown. + /// A DateTimeOffset filled out to midnight, january 1 (UTC) in case of a partial date/time. If the Fhir DateTime + /// does not specify a timezone, the UTC (Coordinated Universal Time) is assumed. Note that the zone parameter has no + /// effect on this, this merely converts the given Fhir datetime to the desired timezone + public DateTimeOffset ToDateTimeOffset(TimeSpan zone) + { + if (Value == null) throw new InvalidOperationException("FhirDateTime's value is null."); + + // TryToDateTime() will convert partial date/times by filling out to midnight/january 1 UTC. + if (!TryToDateTime(out var dt)) + throw new FormatException($"DateTime '{Value}' was not recognized as a valid datetime."); + + // Since Value is not null and the parsed value is valid, dto will not be null + return dt.ToDateTimeOffset(TimeSpan.Zero).ToOffset(zone); + } + + /// + /// Convert this FhirDateTime to a . + /// + /// True if the value of the FhirDateTime is not null, can be parsed as a DateTimeOffset and has a + /// specified timezone, false otherwise. + public bool TryToDateTimeOffset(out DateTimeOffset dto) + { + if (Value is not null && TryToDateTime(out var dt) && dt.Offset is not null) { - if (Value is not null && TryToDateTime(out var dt) && dt!.Offset is not null) - { - dto = dt.ToDateTimeOffset(dt.Offset.Value); - return true; - } - else - { - dto = default; - return false; - } + dto = dt.ToDateTimeOffset(dt.Offset.Value); + return true; } - /// - /// Convert this FhirDateTime to a . - /// - /// Used when the partial FhirDateTime does not have an offset specified. - /// The converted . - /// True if the value of the FhirDateTime is not null and can be parsed as a DateTimeOffset, false otherwise. - public bool TryToDateTimeOffset(TimeSpan defaultOffset, out DateTimeOffset dto) + dto = default; + return false; + } + + /// + /// Convert this FhirDateTime to a . + /// + /// Used when the partial FhirDateTime does not have an offset specified. + /// The converted . + /// True if the value of the FhirDateTime is not null and can be parsed as a DateTimeOffset, false otherwise. + public bool TryToDateTimeOffset(TimeSpan defaultOffset, out DateTimeOffset dto) + { + if (Value is not null && TryToDateTime(out var dt)) { - if (Value is not null && TryToDateTime(out var dt)) - { - dto = dt!.ToDateTimeOffset(defaultOffset); - return true; - } - else - { - dto = default; - return false; - } + dto = dt.ToDateTimeOffset(defaultOffset); + return true; } - /// - /// Checks whether the given literal is correctly formatted. - /// - public static bool IsValidValue(string value) => P.DateTime.TryParse(value, out var parsed) && - (parsed.Precision <= P.DateTimePrecision.Day == !parsed.HasOffset); + dto = default; + return false; } -} - -#nullable restore \ No newline at end of file + /// + /// Checks whether the given literal is correctly formatted. + /// + public static bool IsValidValue(string value) => P.DateTime.TryParse(value, out var parsed) && + (parsed.Precision <= P.DateTimePrecision.Day == !parsed.HasOffset); +} \ No newline at end of file diff --git a/src/Hl7.Fhir.Base/Model/Time.cs b/src/Hl7.Fhir.Base/Model/Time.cs index 50070153b1..4cb19e8ec3 100644 --- a/src/Hl7.Fhir.Base/Model/Time.cs +++ b/src/Hl7.Fhir.Base/Model/Time.cs @@ -35,105 +35,101 @@ POSSIBILITY OF SUCH DAMAGE. using System.Globalization; using P = Hl7.Fhir.ElementModel.Types; -namespace Hl7.Fhir.Model +namespace Hl7.Fhir.Model; + +public partial class Time { + public const string FMT_HOURMINSEC = "{0:D2}:{1:D2}:{2:D2}"; - public partial class Time + public Time(int hour, int minute, int second) : this(string.Format(CultureInfo.InvariantCulture, FMT_HOURMINSEC, hour, minute, second)) { - public const string FMT_HOURMINSEC = "{0:D2}:{1:D2}:{2:D2}"; + // Nothing + } - public Time(int hour, int minute, int second) : this(string.Format(CultureInfo.InvariantCulture, FMT_HOURMINSEC, hour, minute, second)) - { - // Nothing - } + /// + /// Takes the hour, minute and second of a given in the indicated timezone, and uses this + /// to construct a new Time. + /// + public static Time FromDateTimeOffset(DateTimeOffset dto) => new(dto.Hour, dto.Minute, dto.Second); - /// - /// Takes the hour, minute and second of a given in the indicated timezone, and uses this - /// to construct a new Time. - /// - public static Time FromDateTimeOffset(DateTimeOffset dto) => new(dto.Hour, dto.Minute, dto.Second); + public static Time Now() => FromDateTimeOffset(DateTimeOffset.Now); - public static Time Now() => FromDateTimeOffset(DateTimeOffset.Now); + public static Time UtcNow() => FromDateTimeOffset(DateTimeOffset.UtcNow); - public static Time UtcNow() => FromDateTimeOffset(DateTimeOffset.UtcNow); + [NonSerialized] // To prevent binary serialization from serializing this field + private P.Time? _parsedValue = null; - [NonSerialized] // To prevent binary serialization from serializing this field - private P.Time? _parsedValue = null; + // This is a sentintel value that marks that the current string representation is + // not parseable, so we don't have to try again. It's value is never used, it's just + // checked by reference. + private static readonly P.Time INVALID_VALUE = P.Time.FromDateTimeOffset(DateTimeOffset.MinValue); - // This is a sentintel value that marks that the current string representation is - // not parseable, so we don't have to try again. It's value is never used, it's just - // checked by reference. - private static readonly P.Time INVALID_VALUE = P.Time.FromDateTimeOffset(DateTimeOffset.MinValue); + /// + /// Converts a Fhir Time to a . + /// + /// true if the Fhir Time contains a valid time string, false otherwise. + public bool TryToTime([NotNullWhen(true)] out P.Time? time) + { + if (_parsedValue is null) + { + if (Value is not null && !P.Time.TryParse(Value, out _parsedValue)) + _parsedValue = INVALID_VALUE; + } - /// - /// Converts a Fhir Time to a . - /// - /// true if the Fhir Time contains a valid time string, false otherwise. - public bool TryToTime([NotNullWhen(true)] out P.Time? time) + if (hasInvalidParsedValue()) + { + time = null; + return false; + } + else { - if (_parsedValue is null) - { - if (Value is not null && !P.Time.TryParse(Value, out _parsedValue)) - _parsedValue = INVALID_VALUE; - } - - if (hasInvalidParsedValue()) - { - time = null; - return false; - } - else - { - time = _parsedValue!; - return true; - } - - bool hasInvalidParsedValue() => ReferenceEquals(_parsedValue, INVALID_VALUE); + time = _parsedValue!; + return true; } - /// - /// Converts a Fhir Time to a . - /// - /// The Time, or null if the is null. - /// Thrown when the Value does not contain a valid FHIR Time. - public P.Time ToTime() => TryToTime(out var dt) ? dt : throw new FormatException($"String '{Value}' was not recognized as a valid time."); + bool hasInvalidParsedValue() => ReferenceEquals(_parsedValue, INVALID_VALUE); + } + + /// + /// Converts a Fhir Time to a . + /// + /// The Time, or null if the is null. + /// Thrown when the Value does not contain a valid FHIR Time. + public P.Time ToTime() => TryToTime(out var dt) ? dt : throw new FormatException($"String '{Value}' was not recognized as a valid time."); + + protected override void OnObjectValueChanged() + { + _parsedValue = null; + base.OnObjectValueChanged(); + } - protected override void OnObjectValueChanged() + /// + /// Converts this Fhir Time to a . + /// + public TimeSpan ToTimeSpan() => + TryToTimeSpan(out var dt) ? dt : + throw new FormatException($"Time '{Value}' was null or not recognized as a valid time."); + + /// + /// Convert this FhirDateTime to a . + /// + /// True if the value of the Fhir Time is not null and can be parsed as a Time without an offset, false otherwise. + public bool TryToTimeSpan(out TimeSpan dto) + { + if (Value is not null && TryToTime(out var dt) && !dt.HasOffset) { - _parsedValue = null; - base.OnObjectValueChanged(); + dto = dt.ToTimeSpan(); + return true; } - - /// - /// Converts this Fhir Time to a . - /// - public TimeSpan ToTimeSpan() => - TryToTimeSpan(out var dt) ? dt : - throw new FormatException($"Time '{Value}' was null or not recognized as a valid time."); - - /// - /// Convert this FhirDateTime to a . - /// - /// True if the value of the Fhir Time is not null and can be parsed as a Time without an offset, false otherwise. - public bool TryToTimeSpan(out TimeSpan dto) + else { - if (Value is not null && TryToTime(out var dt) && !dt.HasOffset) - { - dto = dt.ToTimeSpan(); - return true; - } - else - { - dto = default; - return false; - } + dto = default; + return false; } - - /// - /// Checks whether the given literal is correctly formatted. - /// - public static bool IsValidValue(string value) => P.Time.TryParse(value, out var parsed) && !parsed.HasOffset; } -} -#nullable restore \ No newline at end of file + /// + /// Checks whether the given literal is correctly formatted. + /// + public static bool IsValidValue(string value) => P.Time.TryParse(value, out var parsed) && !parsed.HasOffset; +} \ No newline at end of file From 3888b97285c74225af36d4e354651075c4604faa Mon Sep 17 00:00:00 2001 From: Ewout Kramer Date: Fri, 6 Dec 2024 17:09:20 +0100 Subject: [PATCH 2/3] Fixed unit test --- src/Hl7.Fhir.Support.Poco.Tests/Model/DateTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Hl7.Fhir.Support.Poco.Tests/Model/DateTests.cs b/src/Hl7.Fhir.Support.Poco.Tests/Model/DateTests.cs index 5ed71246af..d8ff131380 100644 --- a/src/Hl7.Fhir.Support.Poco.Tests/Model/DateTests.cs +++ b/src/Hl7.Fhir.Support.Poco.Tests/Model/DateTests.cs @@ -82,7 +82,7 @@ public void UpdatesCachedValue() dft.Value = null; dft.TryToDateTimeOffset(out _).Should().BeFalse(); - dft.ToDateTimeOffset().Should().BeNull(); + Assert.ThrowsException(() => dft.ToDateTimeOffset()); } [TestMethod] @@ -113,4 +113,4 @@ public void CanConvertToDateTime() dt.Should().BeNull(); } } -} +} \ No newline at end of file From 2d38059f320bf3a5e3c0e4cb6bbd573ac2bd619c Mon Sep 17 00:00:00 2001 From: Ewout Kramer Date: Mon, 9 Dec 2024 11:01:48 +0100 Subject: [PATCH 3/3] Compat suppressions. --- src/Hl7.Fhir.Base/CompatibilitySuppressions.xml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Hl7.Fhir.Base/CompatibilitySuppressions.xml b/src/Hl7.Fhir.Base/CompatibilitySuppressions.xml index e580ed4ec0..cedb839bcb 100644 --- a/src/Hl7.Fhir.Base/CompatibilitySuppressions.xml +++ b/src/Hl7.Fhir.Base/CompatibilitySuppressions.xml @@ -344,6 +344,13 @@ lib/net8.0/Hl7.Fhir.Base.dll true + + CP0002 + M:Hl7.Fhir.Model.Date.ToDateTimeOffset + lib/net8.0/Hl7.Fhir.Base.dll + lib/net8.0/Hl7.Fhir.Base.dll + true + CP0002 M:Hl7.Fhir.Model.Meta.get_ProfileElement