Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve date parsing by utilizing MimeKit logic #1434

Merged
merged 1 commit into from Feb 16, 2023

Conversation

lahma
Copy link
Collaborator

@lahma lahma commented Feb 15, 2023

Utilizing extracted code from MimeKit, removed all things not needed. No dependency on library, but attribution given in both source and CREDITS.txt.

A fast turnaround for soon three year old issue.

fixes #766

@sebastienros
Copy link
Owner

Saw this and looks good, was wondering how better it is from the logic in Fluid which I improved a lot recently for different reasons, and maybe there should be a separate library ;)

@lahma
Copy link
Collaborator Author

lahma commented Feb 16, 2023

I tried the same test cases with Fluid using following test code, 7/8 test cases failed so I think I'll merge this now and we can revisit when yet another library emerges 😉

[Theory]
[InlineData("Thu, 30 Jan 2020 08:00:00 PST", 1580400000000)]
[InlineData("Thursday January 01 1970 00:00:25 UTC", 25000)]
[InlineData("Wednesday 31 December 1969 18:01:26 MDT", 86000)]
[InlineData("Wednesday 31 December 1969 19:00:08 EST", 8000)]
[InlineData("Wednesday 31 December 1969 17:01:59 PDT", 119000)]
[InlineData("December 31 1969 17:01:14 MST", 74000)]
[InlineData("January 01 1970 01:46:06 +0145", 66000)]
[InlineData("December 31 1969 17:00:50 PDT", 50000)]
public void CanParseLocaleString(string dateTime, long expected)
{
    var options = new TemplateOptions() { CultureInfo = new CultureInfo("en-US", useUserOverride: false), TimeZone = TimeZoneInfo.Utc };
    new StringValue(dateTime).TryGetDateTimeInput(new TemplateContext(), out var customDateTime);
    Assert.Equal(expected, FromDateTimeOffset(customDateTime));
}

internal static readonly DateTime Epoch = new(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

private static long FromDateTimeOffset(DateTimeOffset dt, bool negative = false)
{
    var dateAsUtc = dt.ToUniversalTime();

    double result;
    if (negative)
    {
        var zero = (Epoch - DateTime.MinValue).TotalMilliseconds;
        result = zero - TimeSpan.FromTicks(dateAsUtc.Ticks).TotalMilliseconds;
        result *= -1;
    }
    else
    {
        result = (dateAsUtc - Epoch).TotalMilliseconds;
    }

    return (long) System.Math.Floor(result);
}

@lahma lahma merged commit 83206f4 into sebastienros:main Feb 16, 2023
@lahma lahma deleted the improve-date-paring branch February 16, 2023 06:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

NaN Date created from what seems to be a valid Date string
2 participants