| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 | using System;using InABox.Core;namespace InABox.Avalonia.Converters;public class DateTimeToAgeConverter : AbstractConverter<DateTime, String>{            public static string FormatTime(DateTime date)    {        DateTime now = DateTime.Now;        String prefix = date > now ? "In" : "";        String suffix = date <= now ? "ago" : "";        TimeSpan span = date > now ? date - now : now - date;                    if (span.Days > 365)        {            int years = (span.Days / 365);            if (span.Days % 365 != 0)                years += 1;            return String.Format("{0} {1} {2} {3}",                 prefix,                years,                 years == 1 ? "year" : "years",                suffix).Trim();        }                    if (span.Days > 30)        {            int months = (span.Days / 30);            if (span.Days % 31 != 0)                months += 1;            return String.Format("{0} {1} {2} {3}",                 prefix,                months,                months == 1 ? "month" : "months",                suffix).Trim();        }                    if (span.Days > 0)            return String.Format("{0} {1} {2} {3}",                 prefix,                span.Days,                 span.Days == 1 ? "day" : "days",                suffix).Trim();                    if (span.Hours > 0)            return String.Format("{0} about {1} {2} {3}",                 prefix,                span.Hours,                 span.Hours == 1 ? "hour" : "hours",                suffix);                    if (span.Minutes > 0)            return String.Format("{0} {1} {2} {3}",                prefix,                span.Minutes,                 span.Minutes == 1 ? "minute" : "minutes",                suffix).Trim();                    if (span.Seconds > 5)            return String.Format("{0} {1} seconds {2}",                 prefix,                span.Seconds,                suffix).Trim();                    if (span.Seconds <= 5)            return date > now                ? "Imminently"                 : "Just now";                    return string.Empty;    }            public string EmptyValue { get; set; } = String.Empty;    public string Prefix { get; set; } = string.Empty;        protected override string Convert(DateTime value, object? parameter = null)    {        var result = value.IsEmpty()            ? EmptyValue            : string.IsNullOrWhiteSpace(Prefix)                ? FormatTime(value)                : $"{Prefix} {FormatTime(value)}";        return result;    }}
 |