Handle unrepresentable datetimes

This commit is contained in:
Alexander Luzgarev 2019-03-04 22:04:06 +01:00
parent 2c514c62f1
commit 2fbd45d643
3 changed files with 25 additions and 1 deletions

View File

@ -449,6 +449,19 @@ namespace MatFileHandler.Tests
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) =>
new MatTestDataFactory(Path.Combine(TestDirectory, factoryName));

View File

@ -57,6 +57,17 @@ namespace MatFileHandler
/// </summary>
/// <param name="list">Indices.</param>
/// <returns>Value converted to <see cref="DateTimeOffset"/>.</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);
}
}
}
}