Define the all-day rule once as Event.HasTime

This commit is contained in:
tleininger committed 2026-09-24 10:08:04 +02:00
1 parent 31b114982e
commit 66bc76af1c
4 files changed
+87 -57

No files matched your search

+67 -32
View File
@@ -4,45 +4,80 @@ using Elternbeirat.Contracts;
namespace Elternbeirat.Web.Shared;
/// <summary>
/// Formats an <see cref="Event"/>'s date range for display in German. Shared by
/// the full event list and the home-page events embed so both render dates the
/// same way.
/// Formats the date range of an <see cref="Event"/> for display in German.
/// </summary>
/// <remarks>
/// Shared by the full event list and the home-page events embed, so both render
/// dates the same way.
/// </remarks>
public static class EventFormat
{
private static readonly CultureInfo German = CultureInfo.GetCultureInfo("de-DE");
// An event counts as all-day when its start carries no wall-clock time
// (00:00); PocketBase has no separate flag.
private static bool HasTime(Event ev) => ev.Start.TimeOfDay != TimeSpan.Zero;
/// <summary>
/// Date format for the start, and for a single-day event.
/// </summary>
private const string LongDate = "dddd, d. MMMM yyyy";
/// <summary>
/// Formats the entry's date range: a single day, a date with a time, or a
/// span across days.
/// Date format for the end of a multi-day event; the weekday is omitted.
/// </summary>
public static string Range(Event ev)
{
var start = HasTime(ev)
? ev.Start.ToString("dddd, d. MMMM yyyy, HH:mm", German) + " Uhr"
: ev.Start.ToString("dddd, d. MMMM yyyy", German);
private const string ShortDate = "d. MMMM yyyy";
if (ev.End is not { } end)
/// <summary>
/// The culture used for month and weekday names.
/// </summary>
private static readonly CultureInfo German = CultureInfo.GetCultureInfo("de-DE");
/// <summary>
/// Formats the date range of an event.
/// </summary>
/// <param name="event">
/// The event to format.
/// </param>
/// <returns>
/// The formatted range, depending on <see cref="Event.End"/> and <see cref="Event.HasTime"/>:
/// <list type="bullet">
/// <item>
/// <description>No end: <c>Donnerstag, 8. Oktober 2026, 19:30 Uhr</c></description>
/// </item>
/// <item>
/// <description>Same day: <c>Donnerstag, 8. Oktober 2026, 19:30–21:00 Uhr</c></description>
/// </item>
/// <item>
/// <description>Several days: <c>Freitag, 9. Oktober 2026 – 11. Oktober 2026</c></description>
/// </item>
/// </list>
/// Without a time, the time parts are omitted.
/// </returns>
public static string Range(Event @event)
=> (@event.Start, @event.End, @event.HasTime) switch
{
return start;
}
(var start, null, var hasTime)
=> Stamp(start, LongDate, hasTime),
(var start, { } end, true) when end.Date == start.Date
=> $"{start.ToString(LongDate, German)}, {start.ToString("HH:mm", German)}–{end.ToString("HH:mm", German)} Uhr",
(var start, { } end, false) when end.Date == start.Date
=> Stamp(start, LongDate, withTime: false),
(var start, { } end, var hasTime)
=> $"{Stamp(start, LongDate, hasTime)} – {Stamp(end, ShortDate, hasTime)}",
};
// Same day: append just the end time. Different days: append the full
// end date.
if (end.Date == ev.Start.Date)
{
return HasTime(ev)
? start + "–" + end.ToString("HH:mm", German) + " Uhr"
: start;
}
var endText = HasTime(ev)
? end.ToString("d. MMMM yyyy, HH:mm", German) + " Uhr"
: end.ToString("d. MMMM yyyy", German);
return start + " – " + endText;
}
/// <summary>
/// Formats a single point in time as a date with an optional time.
/// </summary>
/// <param name="value">
/// The date and time to format.
/// </param>
/// <param name="dateFormat">
/// The date format, e.g. <see cref="LongDate"/>.
/// </param>
/// <param name="withTime">
/// <see langword="true"/> to append the time, e.g. <c>, 19:30 Uhr</c>.
/// </param>
/// <returns>
/// The formatted date, with the time if requested.
/// </returns>
private static string Stamp(DateTime value, string dateFormat, bool withTime)
=> withTime
? value.ToString(dateFormat + ", HH:mm", German) + " Uhr"
: value.ToString(dateFormat, German);
}