Files
Elternbeirat/Elternbeirat.Web/Features/Layout/FooterYear.cs
T

36 lines
1.3 KiB
C#

namespace Elternbeirat.Web.Features.Layout;
/// <summary>
/// Works out the year shown in the footer's copyright line.
/// </summary>
/// <remarks>
/// The container runs on UTC, but visitors live in Germany. Taking the year
/// straight from the server clock would show the old year for the first hour
/// (two in summer time) of every new year, so the instant is converted to
/// Europe/Berlin first.
/// </remarks>
public static class FooterYear
{
/// <summary>
/// IANA id; resolves on every platform .NET supports, Windows included.
/// </summary>
private static readonly TimeZoneInfo Berlin =
TimeZoneInfo.FindSystemTimeZoneById("Europe/Berlin");
/// <summary>
/// Returns the calendar year in Berlin at the given instant.
/// </summary>
/// <param name="now">
/// The current instant, in any offset; usually
/// <see cref="TimeProvider.GetUtcNow"/>.
/// </param>
/// <returns>The year a visitor in Germany would see on their calendar.</returns>
/// <example>
/// <code>
/// FooterYear.For(new DateTimeOffset(2026, 12, 31, 23, 30, 0, TimeSpan.Zero)); // 2027
/// </code>
/// </example>
public static int For(DateTimeOffset now) =>
TimeZoneInfo.ConvertTime(now, Berlin).Year;
}