Files
Elternbeirat/Elternbeirat.PocketBase/LocalDateTimeConverter.cs
T

132 lines
5.0 KiB
C#

using System.Globalization;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Elternbeirat.PocketBase;
/// <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>
/// <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>
/// <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))));
}