Merge pull request #7 from mahalex/fix-unrepresentable-datetime-bug

Fix unrepresentable datetime bug
This commit is contained in:
Alexander Luzgarev 2019-03-04 22:28:43 +01:00 committed by GitHub
commit 6fe9eda50b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 35 additions and 7 deletions

View File

@ -449,6 +449,19 @@ namespace MatFileHandler.Tests
Assert.That(duration[2], Is.EqualTo(new TimeSpan(1, 3, 5))); Assert.That(duration[2], Is.EqualTo(new TimeSpan(1, 3, 5)));
} }
/// <summary>
/// Test unrepresentable datetime.
/// </summary>
[Test]
public void TestDatetime_Unrepresentable()
{
var matFile = GetTests("good")["datetime-unrepresentable"];
var obj = matFile["d"].Value as IMatObject;
var datetime = new DatetimeAdapter(obj);
var d0 = datetime[0];
Assert.That(d0, Is.Null);
}
private static AbstractTestDataFactory<IMatFile> GetTests(string factoryName) => private static AbstractTestDataFactory<IMatFile> GetTests(string factoryName) =>
new MatTestDataFactory(Path.Combine(TestDirectory, factoryName)); new MatTestDataFactory(Path.Combine(TestDirectory, factoryName));

View File

@ -1,7 +1,7 @@
 
Microsoft Visual Studio Solution File, Format Version 12.00 Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15 # Visual Studio Version 16
VisualStudioVersion = 15.0.28107.0 VisualStudioVersion = 16.0.28621.142
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MatFileHandler", "MatFileHandler\MatFileHandler.csproj", "{C0CD11D3-016A-4FCD-AF0B-D745F79F3749}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MatFileHandler", "MatFileHandler\MatFileHandler.csproj", "{C0CD11D3-016A-4FCD-AF0B-D745F79F3749}"
EndProject EndProject
@ -9,6 +9,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MatFileHandler.Tests", "Mat
EndProject EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{2CF58D3D-4CEC-419B-AD67-58665A888BDF}" Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{2CF58D3D-4CEC-419B-AD67-58665A888BDF}"
ProjectSection(SolutionItems) = preProject ProjectSection(SolutionItems) = preProject
LICENSE.md = LICENSE.md
MatFileHandler\Objects.md = MatFileHandler\Objects.md MatFileHandler\Objects.md = MatFileHandler\Objects.md
README.md = README.md README.md = README.md
EndProjectSection EndProjectSection

View File

@ -56,7 +56,18 @@ namespace MatFileHandler
/// Gets values of datetime object at given position in the array converted to <see cref="DateTimeOffset"/>. /// Gets values of datetime object at given position in the array converted to <see cref="DateTimeOffset"/>.
/// </summary> /// </summary>
/// <param name="list">Indices.</param> /// <param name="list">Indices.</param>
/// <returns>Value converted to <see cref="DateTimeOffset"/>.</returns> /// <returns>Value converted to <see cref="DateTimeOffset"/>; null if the resulting value is unrepresentable.</returns>
public DateTimeOffset this[params int[] list] => epoch.AddMilliseconds(data[Dimensions.DimFlatten(list)]); public DateTimeOffset? this[params int[] list]
{
get
{
var milliseconds = data[Dimensions.DimFlatten(list)];
if (milliseconds < -62_135_596_800_000.0 || milliseconds > 253_402_300_799_999.0)
{
return null;
}
return epoch.AddMilliseconds(milliseconds);
}
}
} }
} }

View File

@ -1,13 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFrameworks>netstandard2.0;net461</TargetFrameworks> <TargetFrameworks>netstandard2.0;net461</TargetFrameworks>
<PackageVersion>1.3.0-beta3</PackageVersion> <PackageVersion>1.3.0-beta4</PackageVersion>
<PackageId>MatFileHandler</PackageId> <PackageId>MatFileHandler</PackageId>
<Title>A library for reading and writing MATLAB .mat files.</Title> <Title>A library for reading and writing MATLAB .mat files.</Title>
<Authors>Alexander Luzgarev</Authors> <Authors>Alexander Luzgarev</Authors>
<Description>MatFileHandler provides a simple interface for reading and writing MATLAB .mat files (of so-called "Level 5") and extracting the contents of numerical arrays, logical arrays, sparse arrays, char arrays, cell arrays and structure arrays.</Description> <Description>MatFileHandler provides a simple interface for reading and writing MATLAB .mat files (of so-called "Level 5") and extracting the contents of numerical arrays, logical arrays, sparse arrays, char arrays, cell arrays and structure arrays.</Description>
<Copyright>Copyright 2017-2018 Alexander Luzgarev</Copyright> <Copyright>Copyright 2017-2018 Alexander Luzgarev</Copyright>
<PackageLicenseUrl>https://raw.githubusercontent.com/mahalex/MatFileHandler/master/LICENSE.md</PackageLicenseUrl> <PackageLicenseFile>LICENSE.md</PackageLicenseFile>
<PackageProjectUrl>https://github.com/mahalex/MatFileHandler</PackageProjectUrl> <PackageProjectUrl>https://github.com/mahalex/MatFileHandler</PackageProjectUrl>
<PackageReleaseNotes>First release.</PackageReleaseNotes> <PackageReleaseNotes>First release.</PackageReleaseNotes>
<PackageTags>Matlab</PackageTags> <PackageTags>Matlab</PackageTags>
@ -31,4 +31,7 @@
</PackageReference> </PackageReference>
<PackageReference Include="System.ValueTuple" Version="4.4.0" Condition="'$(TargetFramework)' == 'net461'" /> <PackageReference Include="System.ValueTuple" Version="4.4.0" Condition="'$(TargetFramework)' == 'net461'" />
</ItemGroup> </ItemGroup>
</Project> <ItemGroup>
<None Include="..\LICENSE.md" Pack="true" PackagePath=""/>
</ItemGroup>
</Project>