Move date converters onto the contracts
This commit is contained in:
1 parent
6d3260db49
commit
31b114982e
6 files changed
+29
-23
No files matched your search
@@ -0,0 +1,47 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Elternbeirat.Contracts;
|
||||
|
||||
/// <summary>
|
||||
/// JSON converter that reads and writes a PocketBase date as a calendar date
|
||||
/// (<see cref="DateOnly"/>), keeping the day and dropping the time-of-day.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// PocketBase has no date-only field type: it stores every date as a UTC
|
||||
/// instant with a time-of-day (e.g. an editor's <c>2 Jan 00:00</c> Berlin is
|
||||
/// stored as <c>"2026-01-01 22:00:00.000Z"</c>). The day the editor meant is
|
||||
/// therefore the day <em>in Berlin</em>, not the day of the raw UTC string. This
|
||||
/// converter reuses the same UTC->Berlin conversion as
|
||||
/// <see cref="LocalDateTimeConverter"/> and then keeps only the date, so the day
|
||||
/// shown is the day that was entered -- with no time-of-day and no off-by-one.
|
||||
/// </remarks>
|
||||
/// <example>
|
||||
/// Apply it to a calendar-day field of a contract:
|
||||
/// <code>
|
||||
/// [JsonConverter(typeof(DateOnlyConverter))]
|
||||
/// public DateOnly Date { get; init; }
|
||||
/// </code>
|
||||
/// </example>
|
||||
/// <seealso cref="WallClock"/>
|
||||
public sealed class DateOnlyConverter : JsonConverter<DateOnly>
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
/// <exception cref="FormatException">
|
||||
/// The stored value is not a valid date string.
|
||||
/// </exception>
|
||||
public override DateOnly Read(
|
||||
ref Utf8JsonReader reader,
|
||||
Type typeToConvert,
|
||||
JsonSerializerOptions options)
|
||||
=> WallClock.Parse(reader.GetString()) is { } berlin
|
||||
? DateOnly.FromDateTime(berlin)
|
||||
: default;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void Write(
|
||||
Utf8JsonWriter writer,
|
||||
DateOnly value,
|
||||
JsonSerializerOptions options)
|
||||
=> writer.WriteStringValue(WallClock.Format(value.ToDateTime(TimeOnly.MinValue)));
|
||||
}
|
||||
@@ -26,6 +26,7 @@ public record Event
|
||||
/// as the time.
|
||||
/// </remarks>
|
||||
[JsonPropertyName("start")]
|
||||
[JsonConverter(typeof(LocalDateTimeConverter))]
|
||||
public DateTime Start { get; init; }
|
||||
|
||||
/// <summary>
|
||||
@@ -35,6 +36,7 @@ public record Event
|
||||
/// The end time, or <see langword="null"/> if the event has no end.
|
||||
/// </value>
|
||||
[JsonPropertyName("end")]
|
||||
[JsonConverter(typeof(NullableLocalDateTimeConverter))]
|
||||
public DateTime? End { get; init; }
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -0,0 +1,133 @@
|
||||
using System.Globalization;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Elternbeirat.Contracts;
|
||||
|
||||
/// <summary>
|
||||
/// Converts between PocketBase date strings and Europe/Berlin wall-clock time.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// PocketBase stores every date in UTC and serializes it with a trailing <c>Z</c>
|
||||
/// (e.g. <c>"2026-10-08 17:30:00.000Z"</c>). The values produced here are local
|
||||
/// wall-clock numbers: an editor who typed 19:30 in the admin form gets 19:30 back,
|
||||
/// with daylight saving applied by the time zone. They carry
|
||||
/// <see cref="DateTimeKind.Unspecified"/> so that no later formatting shifts them again.
|
||||
/// </remarks>
|
||||
internal static class WallClock
|
||||
{
|
||||
/// <summary>
|
||||
/// IANA id; resolves on every platform .NET supports, Windows included.
|
||||
/// </summary>
|
||||
private static readonly TimeZoneInfo Berlin =
|
||||
TimeZoneInfo.FindSystemTimeZoneById("Europe/Berlin");
|
||||
|
||||
/// <summary>
|
||||
/// Parses a PocketBase UTC date string into Berlin wall-clock time.
|
||||
/// </summary>
|
||||
/// <param name="raw">
|
||||
/// The raw PocketBase value; may be <see langword="null"/> or empty.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// The Berlin wall-clock time with <see cref="DateTimeKind.Unspecified"/>, or
|
||||
/// <see langword="null"/> if <paramref name="raw"/> is <see langword="null"/> or empty
|
||||
/// (PocketBase's representation of an unset date).
|
||||
/// </returns>
|
||||
/// <exception cref="FormatException">
|
||||
/// <paramref name="raw"/> is not a valid date string.
|
||||
/// </exception>
|
||||
public static DateTime? Parse(string? raw) =>
|
||||
string.IsNullOrEmpty(raw)
|
||||
? null
|
||||
: TimeZoneInfo.ConvertTime(
|
||||
DateTimeOffset.Parse(raw, CultureInfo.InvariantCulture), Berlin).DateTime;
|
||||
|
||||
/// <summary>
|
||||
/// Formats a Berlin wall-clock time as the UTC string PocketBase stores.
|
||||
/// Inverse of <see cref="Parse"/>.
|
||||
/// </summary>
|
||||
/// <param name="berlin">
|
||||
/// A Berlin wall-clock time. Its <see cref="DateTime.Kind"/> is ignored, and the
|
||||
/// value is always interpreted as Berlin local time.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// The UTC value in PocketBase format, e.g. <c>"2026-10-08 17:30:00.000Z"</c>.
|
||||
/// </returns>
|
||||
/// <exception cref="ArgumentException">
|
||||
/// <paramref name="berlin"/> does not exist in Berlin, because it falls in the
|
||||
/// daylight-saving gap in spring.
|
||||
/// </exception>
|
||||
public static string Format(DateTime berlin) =>
|
||||
TimeZoneInfo.ConvertTimeToUtc(DateTime.SpecifyKind(berlin, DateTimeKind.Unspecified), Berlin)
|
||||
.ToString("yyyy-MM-dd HH:mm:ss.fff'Z'", CultureInfo.InvariantCulture);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// JSON converter that reads and writes PocketBase dates as Europe/Berlin
|
||||
/// wall-clock <see cref="DateTime"/> values.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// An empty string (unset date) is read as <see langword="default"/>(<see cref="DateTime"/>).
|
||||
/// For optional dates, use <see cref="NullableLocalDateTimeConverter"/> instead.
|
||||
/// </remarks>
|
||||
/// <example>
|
||||
/// Apply it to a required date field of a contract:
|
||||
/// <code>
|
||||
/// [JsonConverter(typeof(LocalDateTimeConverter))]
|
||||
/// public DateTime Start { get; init; }
|
||||
/// </code>
|
||||
/// </example>
|
||||
/// <seealso cref="WallClock"/>
|
||||
public sealed class LocalDateTimeConverter : JsonConverter<DateTime>
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public override DateTime Read(
|
||||
ref Utf8JsonReader reader,
|
||||
Type typeToConvert,
|
||||
JsonSerializerOptions options)
|
||||
=> WallClock.Parse(reader.GetString()) ?? default;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void Write(
|
||||
Utf8JsonWriter writer,
|
||||
DateTime value,
|
||||
JsonSerializerOptions options)
|
||||
=> writer.WriteStringValue(WallClock.Format(value));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// JSON converter that reads and writes optional PocketBase dates as Europe/Berlin
|
||||
/// wall-clock <see cref="DateTime"/> values.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// PocketBase sends an empty string for an unset optional date (e.g. an event
|
||||
/// without an end). This value, like JSON <c>null</c>, is read as <see langword="null"/>.
|
||||
/// </remarks>
|
||||
/// <example>
|
||||
/// Apply it to an optional date field of a contract:
|
||||
/// <code>
|
||||
/// [JsonConverter(typeof(NullableLocalDateTimeConverter))]
|
||||
/// public DateTime? End { get; init; }
|
||||
/// </code>
|
||||
/// </example>
|
||||
/// <seealso cref="WallClock"/>
|
||||
public sealed class NullableLocalDateTimeConverter : JsonConverter<DateTime?>
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public override DateTime? Read(
|
||||
ref Utf8JsonReader reader,
|
||||
Type typeToConvert,
|
||||
JsonSerializerOptions options)
|
||||
=> WallClock.Parse(reader.GetString());
|
||||
|
||||
/// <inheritdoc/>
|
||||
/// <remarks>
|
||||
/// Only called for non-null values: <see cref="JsonConverter{T}.HandleNull"/> is
|
||||
/// <see langword="false"/>, so the serializer writes <see langword="null"/> itself.
|
||||
/// </remarks>
|
||||
public override void Write(
|
||||
Utf8JsonWriter writer,
|
||||
DateTime? value,
|
||||
JsonSerializerOptions options)
|
||||
=> writer.WriteStringValue(WallClock.Format(value ?? throw new ArgumentNullException(nameof(value))));
|
||||
}
|
||||
@@ -1,4 +1,3 @@
|
||||
using System.ComponentModel;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Elternbeirat.Contracts;
|
||||
@@ -27,6 +26,7 @@ public record Post
|
||||
/// time-of-day, no off-by-one at midnight. See <see cref="DateOnlyConverter"/>.
|
||||
/// </remarks>
|
||||
[JsonPropertyName("date")]
|
||||
[JsonConverter(typeof(DateOnlyConverter))]
|
||||
public DateOnly Date { get; init; }
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in new issue
Block a user