46 lines
1 KiB
C#
46 lines
1 KiB
C#
using System;
|
|
|
|
namespace Questionable.Controller.Utils;
|
|
|
|
internal static class ExpiryUtils
|
|
{
|
|
private static readonly TimeOnly ResetTime = new TimeOnly(14, 59, 59);
|
|
|
|
private static readonly TimeSpan EndOfDaySentinel = new TimeSpan(23, 59, 59);
|
|
|
|
public static DateTime AtDailyReset(DateOnly date)
|
|
{
|
|
return new DateTime(date, ResetTime, DateTimeKind.Utc);
|
|
}
|
|
|
|
public static DateTime NormalizeExpiry(DateTime expiry)
|
|
{
|
|
TimeSpan timeOfDay = expiry.TimeOfDay;
|
|
if (timeOfDay == TimeSpan.Zero || timeOfDay == EndOfDaySentinel)
|
|
{
|
|
return AtDailyReset(DateOnly.FromDateTime(expiry));
|
|
}
|
|
if (expiry.Kind != DateTimeKind.Utc)
|
|
{
|
|
return expiry.ToUniversalTime();
|
|
}
|
|
return expiry;
|
|
}
|
|
|
|
public static string FormatTimeSpan(TimeSpan timeSpan)
|
|
{
|
|
if (timeSpan.TotalMinutes < 1.0)
|
|
{
|
|
return "< 1m";
|
|
}
|
|
if (!(timeSpan.TotalHours < 1.0))
|
|
{
|
|
if (!(timeSpan.TotalDays < 1.0))
|
|
{
|
|
return $"{(int)timeSpan.TotalDays}d {timeSpan.Hours}h";
|
|
}
|
|
return $"{timeSpan.Hours}h {timeSpan.Minutes}m";
|
|
}
|
|
return $"{timeSpan.Minutes}m";
|
|
}
|
|
}
|