Files
Elternbeirat/Elternbeirat.Web/Shared/EventFormat.cs
T

49 lines
1.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Globalization;
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.
/// </summary>
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>
/// Formats the entry's date range: a single day, a date with a time, or a
/// span across days.
/// </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);
if (ev.End is not { } end)
{
return start;
}
// 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;
}
}