34 lines
1.4 KiB
C#
34 lines
1.4 KiB
C#
namespace Elternbeirat.Web.Shared;
|
|
|
|
/// <summary>
|
|
/// Tells the current calendar day as people at the school see it.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// The container's clock runs in UTC, so <see cref="DateTime.Today"/> would flip to
|
|
/// the next day at 01:00 or 02:00 Berlin time, and an event shortly after midnight
|
|
/// would count as past for an hour or two. Event times are Berlin wall-clock
|
|
/// values (see <c>LocalDateTimeConverter</c>), so "today" has to be a Berlin day
|
|
/// too.
|
|
/// <para>
|
|
/// The time comes from an injected <see cref="TimeProvider"/>, not the system
|
|
/// clock, so tests can pin "today" and check what a page shows before and after
|
|
/// an event.
|
|
/// </para>
|
|
/// </remarks>
|
|
public static class BerlinClock
|
|
{
|
|
/// <summary>
|
|
/// IANA id; resolves on every platform .NET supports, Windows included.
|
|
/// </summary>
|
|
private static readonly TimeZoneInfo Berlin =
|
|
TimeZoneInfo.FindSystemTimeZoneById("Europe/Berlin");
|
|
|
|
/// <summary>
|
|
/// Returns today's date in Berlin.
|
|
/// </summary>
|
|
/// <param name="time">The clock to read, usually <see cref="TimeProvider.System"/>.</param>
|
|
/// <returns>The current Berlin calendar day.</returns>
|
|
public static DateOnly Today(TimeProvider time) =>
|
|
DateOnly.FromDateTime(TimeZoneInfo.ConvertTime(time.GetUtcNow(), Berlin).DateTime);
|
|
}
|