using System.Globalization; namespace View_by_Distance.Shared.Models.Stateless.Methods; internal abstract class PersonBirthday { internal static DateTime GetDefaultValue() => DateTime.MinValue; // {{1}}SingletonValue // ... internal static string GetFormatted(Models.PersonBirthday personBirthday) => personBirthday.Value.ToString(Stateless.IPersonBirthday.Format); internal static string GetFileName(Models.PersonBirthday personBirthday) => $"{personBirthday.Value.ToString(Stateless.IPersonBirthday.Format)}.json"; internal static bool DoesBirthDateExits(Properties.IStorage storage, Models.PersonBirthday personBirthday) => File.Exists(GetFileFullName(storage, personBirthday)); internal static Models.PersonBirthday GetNextBirthDate(Properties.IStorage storage) => throw new Exception(storage.ToString()); // Person.GetNextBirthDate(storage); internal static string GetFileFullName(Properties.IStorage storage, Models.PersonBirthday personBirthday) => Path.Combine(storage.PeopleRootDirectory, "{}", GetFileName(personBirthday)); internal static DateTime? GetDateTime(string personKey) => DateTime.TryParseExact(personKey, Stateless.IPersonBirthday.Format, CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime dateTime) ? dateTime : null; internal static Models.PersonBirthday? GetPersonBirthday(string personKey) { Models.PersonBirthday? result; DateTime? dateTime = GetDateTime(personKey); if (dateTime is null) result = null; else result = new(dateTime.Value); return result; } internal static TimeSpan? GetTimeSpan(DateTime minimumDateTime, bool? isWrongYear, Models.PersonBirthday personBirthday) { TimeSpan? timeSpan; if (isWrongYear is null || isWrongYear.Value || personBirthday.Value.Year < 1900) timeSpan = null; else timeSpan = new(minimumDateTime.Ticks - personBirthday.Value.Ticks); return timeSpan; } internal static (int, TimeSpan) GetAge(DateTime dateTime, Models.PersonBirthday birthday) { TimeSpan result; int years; if (birthday?.Value is null) throw new NullReferenceException(nameof(birthday.Value)); (years, result) = Age.GetAge(dateTime, birthday.Value); return (years, result); } internal static (int, double) GetAge(DateTime dateTime, DateTime dayBeforeLeapDate, Models.PersonBirthday birthday) { double result; (int years, TimeSpan timeSpan) = GetAge(dateTime, birthday); if (!DateTime.IsLeapYear(dateTime.Year) || dateTime < dayBeforeLeapDate.AddDays(1)) result = timeSpan.TotalDays / 365; else result = timeSpan.TotalDays / 366; return (years, result); } internal static double? GetAge(Models.PersonBirthday birthday) { double? result; if (birthday is null) result = null; else { DateTime dateTime = DateTime.Now; DateTime dayBeforeLeapDate = new(dateTime.Year, 2, 28); (int years, double r) = GetAge(dateTime, dayBeforeLeapDate, birthday); result = years + r; } return result; } }