Replies: 2 comments 5 replies
-
I assume you're meaning the output from .ToDelimitedString() Unfortunately as it stands, you cannot. You'll have to do some post processing of the output to truncate it down the 8 characters..... alternatively, fork the project and change the appropriate line; DateTimeOfBirth.HasValue ? DateTimeOfBirth.Value.ToString(Consts.DateTimeFormatPrecisionSecond, culture) : null,
to
DateTimeOfBirth.HasValue ? DateTimeOfBirth.Value.ToString(Consts.DateFormatPrecisionDay, culture) : null, |
Beta Was this translation helpful? Give feedback.
5 replies
-
Ideally we could do something like this. Please feel free to implement this and submit back to the project :) Type-Safe DateTime Precision Configuration for HL7 SerializationGlobal Config Classusing System;
using System.Collections.Generic;
using System.Linq.Expressions;
public static class Hl7DateTimeFormatConfig
{
public static string DefaultDateTimeFormat { get; set; } = Consts.DateTimeFormatPrecisionSecond;
// Use (segmentType, propertyName) as the key for per-field precision
private static readonly Dictionary<(Type, string), string> FieldPrecisions = new();
// Type-safe registration method
public static void SetPrecision<TSegment>(
Expression<Func<TSegment, object?>> property,
string format)
{
if (property.Body is MemberExpression member)
{
FieldPrecisions[(typeof(TSegment), member.Member.Name)] = format;
}
else if (property.Body is UnaryExpression unary && unary.Operand is MemberExpression unaryMember)
{
FieldPrecisions[(typeof(TSegment), unaryMember.Member.Name)] = format;
}
else
{
throw new ArgumentException("Expression is not a property", nameof(property));
}
}
public static string GetFormatForField(Type segmentType, string propertyName)
{
if (FieldPrecisions.TryGetValue((segmentType, propertyName), out var format))
return format;
return DefaultDateTimeFormat;
}
} Registering Per-Field Precision (Type Safe)// Set global default (affects all DateTimes)
Hl7DateTimeFormatConfig.DefaultDateTimeFormat = Consts.DateTimeFormatPrecisionSecond;
// Set specific field (PID.DateTimeOfBirth) to day precision
Hl7DateTimeFormatConfig.SetPrecision<PID>(x => x.DateTimeOfBirth, Consts.DateFormatPrecisionDay); Using the Config in SerializationDateTimeOfBirth.HasValue
? DateTimeOfBirth.Value.ToString(
Hl7DateTimeFormatConfig.GetFormatForField(this.GetType(), nameof(DateTimeOfBirth)), culture)
: null |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
in PID segments, as well as in some others, we have a demand to remove time part of datetime
example : 20160823222400 -> "should be" 20160823
how can i achieve this when adding the datetime value to the DateTimeOfBirth property ?
is there a way to apply a format like "yyyyMMdd" ?
Beta Was this translation helpful? Give feedback.
All reactions