Skip to content

Commit

Permalink
Simplified DateUtils and added more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jstedfast committed Sep 1, 2021
1 parent f850f2f commit 74d32ea
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 45 deletions.
36 changes: 3 additions & 33 deletions MimeKit/Utils/DateUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -587,18 +587,7 @@ public static bool TryParse (byte[] buffer, int startIndex, out DateTimeOffset d
if (startIndex < 0 || startIndex > buffer.Length)
throw new ArgumentOutOfRangeException (nameof (startIndex));

int length = buffer.Length - startIndex;
var tokens = new List<DateToken> (TokenizeDate (buffer, startIndex, length));

if (TryParseStandardDateFormat (tokens, buffer, out date))
return true;

if (TryParseUnknownDateFormat (tokens, buffer, out date))
return true;

date = new DateTimeOffset ();

return false;
return TryParse (buffer, startIndex, buffer.Length - startIndex, out date);
}

/// <summary>
Expand All @@ -618,17 +607,7 @@ public static bool TryParse (byte[] buffer, out DateTimeOffset date)
if (buffer == null)
throw new ArgumentNullException (nameof (buffer));

var tokens = new List<DateToken> (TokenizeDate (buffer, 0, buffer.Length));

if (TryParseStandardDateFormat (tokens, buffer, out date))
return true;

if (TryParseUnknownDateFormat (tokens, buffer, out date))
return true;

date = new DateTimeOffset ();

return false;
return TryParse (buffer, 0, buffer.Length, out date);
}

/// <summary>
Expand All @@ -649,17 +628,8 @@ public static bool TryParse (string text, out DateTimeOffset date)
throw new ArgumentNullException (nameof (text));

var buffer = Encoding.UTF8.GetBytes (text);
var tokens = new List<DateToken> (TokenizeDate (buffer, 0, buffer.Length));

if (TryParseStandardDateFormat (tokens, buffer, out date))
return true;

if (TryParseUnknownDateFormat (tokens, buffer, out date))
return true;

date = new DateTimeOffset ();

return false;
return TryParse (buffer, 0, buffer.Length, out date);
}

// Note: this method exists because BouncyCastle's DerUtcTime.ParseDateString() fails
Expand Down
56 changes: 44 additions & 12 deletions UnitTests/Utils/DateParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,17 @@ public class DateParserTests
"Tue, 21 Apr 15 14:44:51 GMT",
"Tue, 21 April 15 14:44:51 GMT",
"Thu, 1 Oct 2015 14:40:57 +0200 (Mitteleuropäische Sommerzeit)",
"Tue, 12 Jun 2012 19:22:28 0200"
"Tue, 12 Jun 2012 19:22:28 0200",
"Fri, 8 May 2015",
"Fri, 8 May 2015 12",
"Fri, 8 May 2015 12:05",
"Fri, 8 May 2015 12:05:01",
"Fri, 8 May 2015 12:05:01 400",
"Sat, 9 May 2015 24:00:00 -0400",
"Sat, 9 May 2015 25:00:00 -0400",
"May 9 2015 25:00:00 -0400",
"2015 May 9 25:00:00 -0400",
"2015 May 9 25:99:78 -0400",
};

static readonly string[] expected = {
Expand All @@ -73,18 +83,26 @@ public class DateParserTests
"Tue, 21 Apr 2015 14:44:51 +0000",
"Tue, 21 Apr 2015 14:44:51 +0000",
"Thu, 01 Oct 2015 14:40:57 +0200",
"Tue, 12 Jun 2012 19:22:28 +0200"
"Tue, 12 Jun 2012 19:22:28 +0200",
"Fri, 08 May 2015 00:00:00 +0000",
"Fri, 08 May 2015 00:00:00 +0000",
"Fri, 08 May 2015 12:05:00 +0000",
"Fri, 08 May 2015 12:05:01 +0000",
"Fri, 08 May 2015 12:05:01 +0400",
"Sat, 09 May 2015 00:00:00 -0400",
"Sat, 09 May 2015 00:00:00 -0400",
"Sat, 09 May 2015 00:00:00 -0400",
"Sat, 09 May 2015 00:00:00 -0400",
"Sat, 09 May 2015 00:00:00 -0400",
};

[Test]
public void TestDateParser ()
{
DateTimeOffset date;
string parsed;
byte[] text;

for (int i = 0; i < dates.Length; i++) {
text = Encoding.UTF8.GetBytes (dates[i]);
var text = Encoding.UTF8.GetBytes (dates[i]);
DateTimeOffset date;
string parsed;

Assert.IsTrue (DateUtils.TryParse (text, 0, text.Length, out date), "Failed to parse date: {0}", dates[i]);
parsed = DateUtils.FormatDate (date);
Expand All @@ -102,13 +120,27 @@ public void TestDateParser ()
parsed = DateUtils.FormatDate (date);
Assert.AreEqual (expected[i], parsed, "Parsed date does not match: '{0}' vs '{1}'", parsed, expected[i]);
}
}

text = Encoding.ASCII.GetBytes ("this is pure junk");
static readonly string[] invalidDates = {
"this is pure junk",
"Sunday is the day of our Lord",
"Sun is so bright, I gotta wear shades",
"Sat, 8 dogs did while 8 cats hid",
"Sat, 9 May flies bit my arms"
};

Assert.IsFalse (DateUtils.TryParse (text, 0, text.Length, out date), "Should not have parsed junk.");
Assert.IsFalse (DateUtils.TryParse (text, 0, out date), "Should not have parsed junk.");
Assert.IsFalse (DateUtils.TryParse (text, out date), "Should not have parsed junk.");
Assert.IsFalse (DateUtils.TryParse ("this is pure junk", out date), "Should not have parsed junk.");
[Test]
public void TestParseInvalidDates ()
{
for (int i = 0; i < invalidDates.Length; i++) {
var text = Encoding.UTF8.GetBytes (invalidDates[i]);

Assert.IsFalse (DateUtils.TryParse (text, 0, text.Length, out _), "Should not have parsed '{0}'", invalidDates[i]);
Assert.IsFalse (DateUtils.TryParse (text, 0, out _), "Should not have parsed '{0}'", invalidDates[i]);
Assert.IsFalse (DateUtils.TryParse (text, out _), "Should not have parsed '{0}'", invalidDates[i]);
Assert.IsFalse (DateUtils.TryParse (invalidDates[i], out _), "Should not have parsed '{0}'", invalidDates[i]);
}
}
}
}

0 comments on commit 74d32ea

Please sign in to comment.