Files
archived-discord-bot/CompatBot/Utils/Extensions/DateTimeEx.cs
2020-11-14 14:33:29 +05:00

66 lines
2.1 KiB
C#

using System;
namespace CompatBot.Utils
{
public static class DateTimeEx
{
public static DateTime AsUtc(this DateTime dateTime)
{
if (dateTime.Kind == DateTimeKind.Utc)
return dateTime;
return new DateTime(dateTime.Ticks, DateTimeKind.Utc);
}
public static DateTime AsUtc(this long ticks)
{
return new(ticks, DateTimeKind.Utc);
}
public static string AsShortTimespan(this TimeSpan timeSpan)
{
var totalSeconds = timeSpan.TotalSeconds;
var totalSecondsInt = (int)totalSeconds;
var totalMinutes = totalSeconds / 60;
var totalMinutesInt = (int)totalMinutes;
var totalHours = totalMinutes / 60;
var totalHoursInt = (int)totalHours;
var totalDays = totalHours / 24;
var totalDaysInt = (int)totalDays;
var totalWeeks = totalDays / 7;
var totalWeeksInt = (int)totalWeeks;
var totalMonths = totalDays / 30;
var totalMonthsInt = (int)totalMonths;
var totalYears = totalDays / 365.25;
var totalYearsInt = (int)totalYears;
var years = totalYearsInt;
var months = totalMonthsInt - years * 12;
var weeks = totalWeeksInt - years * 52 - months * 4;
var days = totalDaysInt - totalWeeksInt * 7;
var hours = totalHoursInt - totalDaysInt * 24;
var minutes = totalMinutesInt - totalHoursInt * 60;
var seconds = totalSecondsInt - totalMinutesInt * 60;
var result = "";
if (years > 0)
result += years + "y ";
if (months > 0)
result += months + "m ";
if (weeks > 0)
result += weeks + "w ";
if (days > 0)
result += days + "d ";
if (hours > 0)
result += hours + "h ";
if (minutes > 0)
result += minutes + "m ";
if (string.IsNullOrEmpty(result))
result = seconds + "s";
return result.TrimEnd();
}
}
}