Skip to content

Commit

Permalink
Merge pull request #120 from HicServices/feature/singlepassdate
Browse files Browse the repository at this point in the history
Feature/singlepassdate
  • Loading branch information
JFriel authored Sep 2, 2024
2 parents 64bec72 + 3554205 commit b78ef54
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 22 additions & 11 deletions TypeGuesser/Deciders/DateTimeTypeDecider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,21 +180,32 @@ protected override object ParseImpl(ReadOnlySpan<char> value)
/// </summary>
public void GuessDateFormat(IEnumerable<string> 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;
}

Expand Down

0 comments on commit b78ef54

Please sign in to comment.