Skip to content
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using NodaTime.Text;
using Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.Mapping;

// ReSharper disable once CheckNamespace
Expand Down Expand Up @@ -84,7 +83,7 @@ protected override string GenerateNonNullSqlLiteral(object value)
protected override string GenerateEmbeddedNonNullSqlLiteral(object value)
{
var dateInterval = (DateInterval)value;
return $"[{LocalDatePattern.Iso.Format(dateInterval.Start)},{LocalDatePattern.Iso.Format(dateInterval.End)}]";
return $"[{DateMapping.FormatSql(dateInterval.Start)},{DateMapping.FormatSql(dateInterval.End)}]";
}

/// <summary>
Expand Down
11 changes: 8 additions & 3 deletions src/EFCore.PG.NodaTime/Storage/Internal/DateMapping.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Globalization;
using System.Text.Json;
using Microsoft.EntityFrameworkCore.Storage.Json;
using NodaTime.Text;
Expand Down Expand Up @@ -84,7 +85,7 @@ protected override string GenerateNonNullSqlLiteral(object value)
protected override string GenerateEmbeddedNonNullSqlLiteral(object value)
=> FormatLocalDate((LocalDate)value);

private static string FormatLocalDate(LocalDate date)
private static string FormatLocalDate(LocalDate date, bool json = false)
{
if (!NpgsqlNodaTimeTypeMappingSourcePlugin.DisableDateTimeInfinityConversions)
{
Expand All @@ -99,9 +100,13 @@ private static string FormatLocalDate(LocalDate date)
}
}

return LocalDatePattern.Iso.Format(date);
return json ? LocalDatePattern.Iso.Format(date) : FormatSql(date);
}

// JSON uses ISO astronomical years (0 = 1 BCE); PostgreSQL date literals require year-of-era and a BC suffix.
internal static string FormatSql(LocalDate date)
=> date.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture) + (date.Year <= 0 ? " BC" : "");

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
Expand Down Expand Up @@ -139,7 +144,7 @@ public override LocalDate FromJsonTyped(ref Utf8JsonReaderManager manager, objec
}

public override void ToJsonTyped(Utf8JsonWriter writer, LocalDate value)
=> writer.WriteStringValue(FormatLocalDate(value));
=> writer.WriteStringValue(FormatLocalDate(value, json: true));

/// <inheritdoc />
public override Expression ConstructorExpression => Expression.Property(null, InstanceProperty);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@


using System.Text;
using NodaTime.Text;
// ReSharper disable once CheckNamespace
using Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.Mapping;

Expand Down Expand Up @@ -93,14 +92,14 @@ protected override string GenerateEmbeddedNonNullSqlLiteral(object value)

if (interval.HasStart)
{
stringBuilder.Append(InstantPattern.ExtendedIso.Format(interval.Start));
stringBuilder.Append(TimestampTzInstantMapping.FormatSql(interval.Start));
}

stringBuilder.Append(',');

if (interval.HasEnd)
{
stringBuilder.Append(InstantPattern.ExtendedIso.Format(interval.End));
stringBuilder.Append(TimestampTzInstantMapping.FormatSql(interval.End));
}

stringBuilder.Append(')');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using NodaTime.Text;
using Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.Mapping;

// ReSharper disable once CheckNamespace
Expand Down Expand Up @@ -109,7 +108,7 @@ private string GenerateLiteralCore(object value)
}
}

return InstantPattern.ExtendedIso.Format(instant);
return TimestampTzInstantMapping.FormatSql(instant);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Globalization;
using System.Text.Json;
using Microsoft.EntityFrameworkCore.Storage.Json;
using NodaTime.Text;
Expand Down Expand Up @@ -101,7 +102,7 @@ protected override string GenerateEmbeddedNonNullSqlLiteral(object value)
"{Format((LocalDateTime)value)}"
""";

private static string Format(LocalDateTime localDateTime)
private static string Format(LocalDateTime localDateTime, bool json = false)
{
if (!NpgsqlNodaTimeTypeMappingSourcePlugin.DisableDateTimeInfinityConversions)
{
Expand All @@ -116,7 +117,9 @@ private static string Format(LocalDateTime localDateTime)
}
}

return LocalDateTimePattern.ExtendedIso.Format(localDateTime);
return json || localDateTime.Year > 0
? LocalDateTimePattern.ExtendedIso.Format(localDateTime)
: localDateTime.ToString("yyyy-MM-dd'T'HH:mm:ss.FFFFFFFFF", CultureInfo.InvariantCulture) + " BC";
}

/// <summary>
Expand Down Expand Up @@ -168,7 +171,7 @@ public override LocalDateTime FromJsonTyped(ref Utf8JsonReaderManager manager, o
}

public override void ToJsonTyped(Utf8JsonWriter writer, LocalDateTime value)
=> writer.WriteStringValue(Format(value));
=> writer.WriteStringValue(Format(value, json: true));

/// <inheritdoc />
public override Expression ConstructorExpression => Expression.Property(null, InstanceProperty);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Globalization;
using System.Text.Json;
using Microsoft.EntityFrameworkCore.Storage.Json;
using NodaTime.Text;
Expand Down Expand Up @@ -91,7 +92,7 @@ protected override string GenerateEmbeddedNonNullSqlLiteral(object value)
"{Format((Instant)value)}"
""";

private static string Format(Instant instant)
private static string Format(Instant instant, bool json = false)
{
if (!NpgsqlNodaTimeTypeMappingSourcePlugin.DisableDateTimeInfinityConversions)
{
Expand All @@ -106,9 +107,14 @@ private static string Format(Instant instant)
}
}

return InstantPattern.ExtendedIso.Format(instant);
return json ? InstantPattern.ExtendedIso.Format(instant) : FormatSql(instant);
}

internal static string FormatSql(Instant instant)
=> instant.InUtc().Year > 0
? InstantPattern.ExtendedIso.Format(instant)
: instant.ToString("yyyy-MM-dd'T'HH:mm:ss.FFFFFFFFF'Z'", CultureInfo.InvariantCulture) + " BC";

internal static Expression GenerateCodeLiteral(Instant instant)
=> Expression.Call(_fromUnixTimeTicks, Expression.Constant(instant.ToUnixTimeTicks()));

Expand Down Expand Up @@ -149,7 +155,7 @@ public override Instant FromJsonTyped(ref Utf8JsonReaderManager manager, object?
}

public override void ToJsonTyped(Utf8JsonWriter writer, Instant value)
=> writer.WriteStringValue(Format(value));
=> writer.WriteStringValue(Format(value, json: true));

/// <inheritdoc />
public override Expression ConstructorExpression => Expression.Property(null, InstanceProperty);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Globalization;
using System.Text.Json;
using Microsoft.EntityFrameworkCore.Storage.Json;
using NodaTime.Text;
Expand Down Expand Up @@ -101,8 +102,10 @@ protected override string GenerateEmbeddedNonNullSqlLiteral(object value)
"{Format((OffsetDateTime)value)}"
""";

private static string Format(OffsetDateTime offsetDateTime)
=> OffsetDateTimePattern.ExtendedIso.Format(offsetDateTime);
private static string Format(OffsetDateTime offsetDateTime, bool json = false)
=> json || offsetDateTime.Year > 0
? OffsetDateTimePattern.ExtendedIso.Format(offsetDateTime)
: offsetDateTime.ToString("yyyy-MM-dd'T'HH:mm:ss.FFFFFFFFFo<G>", CultureInfo.InvariantCulture) + " BC";

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down Expand Up @@ -133,7 +136,7 @@ public override OffsetDateTime FromJsonTyped(ref Utf8JsonReaderManager manager,
=> OffsetDateTimePattern.ExtendedIso.Parse(manager.CurrentReader.GetString()!).GetValueOrThrow();

public override void ToJsonTyped(Utf8JsonWriter writer, OffsetDateTime value)
=> writer.WriteStringValue(Format(value));
=> writer.WriteStringValue(Format(value, json: true));

/// <inheritdoc />
public override Expression ConstructorExpression => Expression.Property(null, InstanceProperty);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Globalization;
using System.Text.Json;
using Microsoft.EntityFrameworkCore.Storage.Json;
using NodaTime.Text;
Expand Down Expand Up @@ -84,7 +85,7 @@ protected override string ProcessStoreType(RelationalTypeMappingParameters param
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override string GenerateNonNullSqlLiteral(object value)
=> $"TIMESTAMPTZ '{Pattern.Format((ZonedDateTime)value)}'";
=> $"TIMESTAMPTZ '{Format((ZonedDateTime)value)}'";

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand All @@ -94,9 +95,14 @@ protected override string GenerateNonNullSqlLiteral(object value)
/// </summary>
protected override string GenerateEmbeddedNonNullSqlLiteral(object value)
=> $"""
"{Pattern.Format((ZonedDateTime)value)}"
"{Format((ZonedDateTime)value)}"
""";

private static string Format(ZonedDateTime zonedDateTime)
=> zonedDateTime.Year > 0
? Pattern.Format(zonedDateTime)
: zonedDateTime.ToString("yyyy-MM-dd'T'HH:mm:ss.FFFFFFo<G>", CultureInfo.InvariantCulture) + " BC";

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
Expand Down
Loading
Loading