Skip to content

Commit

Permalink
Add support for time formats like 15.45.33
Browse files Browse the repository at this point in the history
  • Loading branch information
Kees van Spelde committed Oct 22, 2024
1 parent 4ee32d2 commit c618d50
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
19 changes: 17 additions & 2 deletions MsgReaderCore/Mime/Decode/Rfc2822DateTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ private static DateTime ExtractDateTime(string dateInput)
const string year = @"(\d\d\d\d|\d\d)";

// Time with one or two digits for hour and minute and optional seconds (06:04:06 or 6:4:6 or 06:04 or 6:4)
const string time = @"\d?\d:\d?\d(:\d?\d)?";
const string time = @"\d?\d[:.]\d?\d([:.]\d?\d)?";

// Correct format is 21 Nov 1997 09:55:06
const string correctFormat = $@"\d\d? [^\d]+ {year} {time}";
Expand All @@ -323,11 +323,26 @@ private static DateTime ExtractDateTime(string dateInput)
// We allow both correct and incorrect format
const string joinedFormat = $@"({correctFormat})|({incorrectFormat})|({correctFormatButWithDashes})";

var wrongTime = string.Empty;
var correctTime = string.Empty;

var timeMatch = Regex.Match(dateInput, time);
if (timeMatch.Success && timeMatch.Value.Contains("."))
{
wrongTime = timeMatch.Value;
correctTime = timeMatch.Value.Replace(".", ":");
}

var match = Regex.Match(dateInput, joinedFormat);

if (match.Success)
try
{
return Convert.ToDateTime(match.Value, CultureInfo.InvariantCulture);
var value = match.Value;
if (!string.IsNullOrEmpty(wrongTime) && !string.IsNullOrEmpty(correctTime))
value = value.Replace(wrongTime, correctTime);

return Convert.ToDateTime(value, CultureInfo.InvariantCulture);
}
catch (FormatException)
{
Expand Down
6 changes: 3 additions & 3 deletions MsgReaderCore/MsgReader.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@ The EML reader supports MIME 1.0 encoded files.</Description>

<ItemGroup>
<PackageReference Include="Microsoft.IO.RecyclableMemoryStream" Version="3.0.1" />
<PackageReference Include="Microsoft.Maui.Graphics" Version="8.0.82" />
<PackageReference Include="Microsoft.Maui.Graphics" Version="8.0.92" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="OpenMcdf" Version="2.3.1" />
<PackageReference Include="RtfPipe" Version="2.0.7677.4303" />
<PackageReference Include="System.Security.Cryptography.Pkcs" Version="8.0.0" />
<PackageReference Include="System.Security.Cryptography.Pkcs" Version="8.0.1" />
<PackageReference Include="UTF.Unknown" Version="2.5.1" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net462' ">
<PackageReference Include="System.Drawing.Common" Version="8.0.8" />
<PackageReference Include="System.Drawing.Common" Version="8.0.10" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net5' or '$(TargetFramework)' == 'net6' ">
Expand Down
1 change: 1 addition & 0 deletions MsgReaderTests/LoadTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MsgReader;
using System.IO;
using MsgReader.Mime.Decode;

namespace MsgReaderTests
{
Expand Down
8 changes: 4 additions & 4 deletions MsgReaderTests/MsgReaderTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.5.2" />
<PackageReference Include="MSTest.TestFramework" Version="3.5.2" />
<PackageReference Include="System.Drawing.Common" Version="8.0.8" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="MSTest.TestAdapter" Version="3.6.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.6.1" />
<PackageReference Include="System.Drawing.Common" Version="8.0.10" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="8.0.0" />
</ItemGroup>

Expand Down

0 comments on commit c618d50

Please sign in to comment.