Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the Unix epoch as a default for non-nullable date time #59

Merged
merged 1 commit into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion Jinaga.Test/Facts/VersioningTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,36 @@ public void CanUpgradeWithNullableDateTimeField()
deserialized.createdAt.Should().BeNull();
}

[Fact]
public void CanSerializeNullableDateTimeFieldWithValue()
{
var original = new BlogV4(new DateTime(2021, 1, 1).ToUniversalTime());
var factGraph = Serialize(original);
var deserialized = Deserialize<BlogV4>(factGraph, factGraph.Last);

deserialized.createdAt.Should().Be(new DateTime(2021, 1, 1).ToUniversalTime());
}

[Fact]
public void CanSerializeNullableDateTimeFieldWithNull()
{
var original = new BlogV4(null);
var factGraph = Serialize(original);
var deserialized = Deserialize<BlogV4>(factGraph, factGraph.Last);

deserialized.createdAt.Should().BeNull();
}

[Fact]
public void CanUpgradeNullableDateTimeToNonNullable()
{
var original = new BlogV4(null);
var factGraph = Serialize(original);
var deserialized = Deserialize<BlogV5>(factGraph, factGraph.Last);

deserialized.createdAt.Should().Be(DateTime.UnixEpoch);
}

[Fact]
public void CanDowngrade()
{
Expand Down Expand Up @@ -100,4 +130,7 @@ public record BlogV2(string domain, string title) {}
public record BlogV3(string domain, string title, int stars) {}

[FactType("Blog")]
public record BlogV4(DateTime? createdAt) {}
public record BlogV4(DateTime? createdAt) {}

[FactType("Blog")]
public record BlogV5(DateTime createdAt) {}
8 changes: 6 additions & 2 deletions Jinaga/Facts/FieldValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,18 @@ public static string ToNullableIso8601String(DateTime? dateTime)

public static DateTime FromIso8601String(string str)
{
return DateTime.Parse(str).ToUniversalTime();
return DateTime.TryParse(str, out var dateTime)
? dateTime.ToUniversalTime()
: DateTime.UnixEpoch;
}

public static DateTime? FromNullableIso8601String(string str)
{
return string.IsNullOrEmpty(str)
? (DateTime?)null
: FromIso8601String(str);
: DateTime.TryParse(str, out var dateTime)
? dateTime.ToUniversalTime()
: (DateTime?)null;
}

public abstract string StringValue { get; }
Expand Down
Loading