Files

56 lines
2.1 KiB
C#

using System.Globalization;
using Elternbeirat.Web.Shared;
namespace Elternbeirat.Web.Features.Posts;
/// <summary>
/// Formats a post's calendar day for display and for a machine-readable
/// <c>datetime</c> attribute.
/// </summary>
/// <remarks>
/// Posts carry a <see cref="DateOnly"/> (a calendar day, no time of day). Their
/// <c>&lt;time&gt;</c> element needs the day twice: once in German for the
/// visitor (<see cref="Display"/>) and once as ISO 8601 for the <c>datetime</c>
/// attribute (<see cref="Iso"/>). Keeping both here means the two never drift
/// apart and the markup no longer repeats the same <c>ToString</c> pair.
/// <para>
/// This is the post counterpart to
/// <see cref="Events.EventDateFormat"/>, which formats a
/// <see cref="System.DateTime"/> range instead.
/// </para>
/// </remarks>
public static class PostDateFormat
{
/// <summary>
/// Display format for a calendar day, e.g. <c>8. Oktober 2026</c>. The
/// weekday is omitted, unlike the event format.
/// </summary>
private const string LongDate = "d. MMMM yyyy";
/// <summary>
/// Formats a calendar day in German for a visitor to read.
/// </summary>
/// <param name="value">
/// The calendar day to format.
/// </param>
/// <returns>
/// The day as e.g. <c>8. Oktober 2026</c>, using German month names.
/// </returns>
public static string Display(DateOnly value)
=> value.ToString(LongDate, Cultures.German);
/// <summary>
/// Formats a calendar day as ISO 8601 (<c>yyyy-MM-dd</c>) for a
/// machine-readable <c>datetime</c> attribute.
/// </summary>
/// <param name="value">
/// The calendar day to format.
/// </param>
/// <returns>
/// The day as e.g. <c>2026-10-08</c>, culture-invariant so the value is
/// stable regardless of the request's culture.
/// </returns>
public static string Iso(DateOnly value)
=> value.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);
}