diff --git a/CHANGELOG.md b/CHANGELOG.md index 36b01e2..91b319e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +- Single pass non-copying date format guessing + ## [1.2.6] - 2024-07-16 - Throw exceptions on invalid conversion attempts diff --git a/TypeGuesser/Deciders/DateTimeTypeDecider.cs b/TypeGuesser/Deciders/DateTimeTypeDecider.cs index 80817ab..2731164 100644 --- a/TypeGuesser/Deciders/DateTimeTypeDecider.cs +++ b/TypeGuesser/Deciders/DateTimeTypeDecider.cs @@ -180,21 +180,32 @@ protected override object ParseImpl(ReadOnlySpan value) /// public void GuessDateFormat(IEnumerable samples) { - if (!AllowCultureGuessing) - return; - - samples = samples.Where(static s => !string.IsNullOrWhiteSpace(s)).ToList(); + var total = 0; + var simple = 0; + var m = 0; + var d = 0; - //if they are all valid anyway - if (samples.All(s => DateTime.TryParse(s, Culture, DateTimeStyles.None, out _))) + if (!AllowCultureGuessing) return; - _dateFormatToUse = DateFormatsDM; - var countDm = samples.Count(s => TryBruteParse(s, out _)); - _dateFormatToUse = DateFormatsMD; - var countMd = samples.Count(s => TryBruteParse(s, out _)); + foreach (var sSample in samples.Where(static sSample => !string.IsNullOrWhiteSpace(sSample))) + { + var sample = sSample.AsSpan(); + total++; + if (DateTime.TryParse(sample, Culture, DateTimeStyles.None, out _)) + simple++; + else + { + _dateFormatToUse = DateFormatsDM; + if (TryBruteParse(sample, out _)) + d++; + _dateFormatToUse = DateFormatsMD; + if (TryBruteParse(sample, out _)) + m++; + } + } - if (countDm >= countMd) + if (simple < total && d > m) _dateFormatToUse = DateFormatsDM; }