Skip to content

Commit

Permalink
FormatInternalDate outputs wrong formatted dates with timezones havin…
Browse files Browse the repository at this point in the history
…g negative offsets (#1753)

Fixed ImapUtils.FormatInternalDate() to properly handle negative timezone offsets with non-zero minutes

---------

Co-authored-by: Jeffrey Stedfast <jestedfa@microsoft.com>
  • Loading branch information
elkoiko and jstedfast authored May 16, 2024
1 parent 55977aa commit 8414973
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
2 changes: 1 addition & 1 deletion MailKit/Net/Imap/ImapUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static string FormatInternalDate (DateTimeOffset date)
{
return string.Format (CultureInfo.InvariantCulture, "{0:D2}-{1}-{2:D4} {3:D2}:{4:D2}:{5:D2} {6:+00;-00}{7:00}",
date.Day, Months[date.Month - 1], date.Year, date.Hour, date.Minute, date.Second,
date.Offset.Hours, date.Offset.Minutes);
date.Offset.Hours, Math.Abs (date.Offset.Minutes));
}

static bool TryGetInt32 (string text, ref int index, out int value)
Expand Down
51 changes: 51 additions & 0 deletions UnitTests/Net/Imap/ImapUtilsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4282,5 +4282,56 @@ public async Task TestParseFolderListWithFolderNameContainingUnquotedTabsAsync (
}
}
}

[Test]
[TestCase (2023, 5, 14, 12, 30, 45, -4, -30, "14-May-2023 12:30:45 -0430")] // Sunday
[TestCase (2023, 5, 15, 12, 30, 45, -3, -30, "15-May-2023 12:30:45 -0330")] // Monday
[TestCase (2023, 5, 16, 12, 30, 45, -2, 0, "16-May-2023 12:30:45 -0200")] // Tuesday
[TestCase (2023, 5, 17, 12, 30, 45, -1, 0, "17-May-2023 12:30:45 -0100")] // Wednesday
public void TestFormatInternalDateNegativeOffsets (int year, int month, int day, int hour, int minute, int second, int offsetHours, int offsetMinutes, string expected)
{
var date = new DateTimeOffset (year, month, day, hour, minute, second, new TimeSpan (offsetHours, offsetMinutes, 0));

string formattedInternalDate = ImapUtils.FormatInternalDate (date);

Assert.That (formattedInternalDate, Is.EqualTo (expected), $"Expected {expected} but got {formattedInternalDate} for date {date}.");
}

[Test]
[TestCase (2023, 5, 18, 12, 30, 45, 1, 0, "18-May-2023 12:30:45 +0100")] // Thursday
[TestCase (2023, 5, 19, 12, 30, 45, 4, 30, "19-May-2023 12:30:45 +0430")] // Friday
[TestCase (2023, 5, 20, 12, 30, 45, 9, 30, "20-May-2023 12:30:45 +0930")] // Saturday
[TestCase (2023, 5, 21, 12, 30, 45, 12, 0, "21-May-2023 12:30:45 +1200")] // Sunday
public void TestFormatInternalDatePositiveOffsets (int year, int month, int day, int hour, int minute, int second, int offsetHours, int offsetMinutes, string expected)
{
var date = new DateTimeOffset (year, month, day, hour, minute, second, new TimeSpan (offsetHours, offsetMinutes, 0));

string formattedInternalDate = ImapUtils.FormatInternalDate (date);

Assert.That (formattedInternalDate, Is.EqualTo (expected), $"Expected {expected} but got {formattedInternalDate} for date {date}.");
}

[Test]
[TestCase (2023, 5, 22, 12, 30, 45, 0, 0, "22-May-2023 12:30:45 +0000")] // Monday
public void TestFormatInternalDateZeroOffset (int year, int month, int day, int hour, int minute, int second, int offsetHours, int offsetMinutes, string expected)
{
var date = new DateTimeOffset (year, month, day, hour, minute, second, new TimeSpan (offsetHours, offsetMinutes, 0));

string formattedInternalDate = ImapUtils.FormatInternalDate (date);

Assert.That (formattedInternalDate, Is.EqualTo (expected), $"Expected {expected} but got {formattedInternalDate} for date {date}.");
}

[Test]
[TestCase (2023, 5, 23, 23, 59, 59, 2, 30, "23-May-2023 23:59:59 +0230")] // Tuesday
[TestCase (2023, 5, 24, 0, 0, 0, -4, -30, "24-May-2023 00:00:00 -0430")] // Wednesday
public void TestFormatInternalDateEdgeCases (int year, int month, int day, int hour, int minute, int second, int offsetHours, int offsetMinutes, string expected)
{
var date = new DateTimeOffset (year, month, day, hour, minute, second, new TimeSpan (offsetHours, offsetMinutes, 0));

string formattedInternalDate = ImapUtils.FormatInternalDate (date);

Assert.That (formattedInternalDate, Is.EqualTo (expected), $"Expected {expected} but got {formattedInternalDate} for date {date}.");
}
}
}

0 comments on commit 8414973

Please sign in to comment.