using System;
namespace Jellyfin.Plugin.SRFPlay.Utilities;
///
/// Converts times into the time zone the plugin writes into text (item names, file names).
/// SRG sends times with an offset, and DateTime deserialisation turns them into server-local
/// values, so without this the text follows whatever zone the server happens to run in.
///
public static class DisplayTime
{
///
/// Converts a time to the configured display time zone (the server's if none is set).
///
/// The time. Unspecified values are treated as server-local, like deserialised ones.
/// The same instant expressed in the display time zone.
public static DateTime From(DateTime value)
{
return TimeZoneInfo.ConvertTimeFromUtc(value.ToUniversalTime(), GetZone());
}
///
/// Gets the current time in the display time zone.
///
/// The current time in the display time zone.
public static DateTime Now()
{
return TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, GetZone());
}
private static TimeZoneInfo GetZone()
{
var id = Plugin.Instance?.Configuration.DisplayTimeZone;
if (string.IsNullOrWhiteSpace(id))
{
return TimeZoneInfo.Local;
}
// An unknown id falls back to the server's zone rather than breaking listings.
return TimeZoneInfo.TryFindSystemTimeZoneById(id.Trim(), out var zone) ? zone : TimeZoneInfo.Local;
}
}