112 lines
4.7 KiB
C#

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(string personBirthdayFormat, Models.PersonBirthday personBirthday) => personBirthday.Value.ToString(personBirthdayFormat);
internal static string GetFileName(string personBirthdayFormat, Models.PersonBirthday personBirthday) => $"{personBirthday.Value.ToString(personBirthdayFormat)}.json";
internal static bool DoesBirthDateExits(Properties.IStorage storage, string personBirthdayFormat, Models.PersonBirthday personBirthday) => File.Exists(GetFileFullName(storage, personBirthdayFormat, personBirthday));
internal static Models.PersonBirthday GetNextBirthDate(Properties.IStorage storage) => throw new Exception(storage.ToString()); // Person.GetNextBirthDate(storage);
internal static string GetFileFullName(Properties.IStorage storage, string personBirthdayFormat, Models.PersonBirthday personBirthday) => Path.Combine(storage.PeopleRootDirectory, "{}", GetFileName(personBirthdayFormat, personBirthday));
internal static DateTime? GetDateTime(string personBirthdayFormat, string personKey) => DateTime.TryParseExact(personKey, personBirthdayFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime dateTime) ? dateTime : null;
internal static Models.PersonBirthday? GetPersonBirthday(string personBirthdayFormat, string personKey)
{
Models.PersonBirthday? result;
DateTime? dateTime = GetDateTime(personBirthdayFormat, personKey);
if (dateTime is null)
result = null;
else
result = new(dateTime.Value);
return result;
}
internal static bool IsCounterPersonBirthday(Models.PersonBirthday personBirthday)
{
bool result;
if (personBirthday.Value.Year < 1900)
result = true;
else
result = false;
return result;
}
internal static bool IsWrongYearFilterOrCounterPersonBirthday(bool? isWrongYear, Models.PersonBirthday personBirthday)
{
bool result;
if (isWrongYear is null || isWrongYear.Value || IsCounterPersonBirthday(personBirthday))
result = true;
else
result = false;
return result;
}
internal static TimeSpan? GetTimeSpan(long minimumDateTimeTicks, bool? isWrongYear, Models.PersonBirthday personBirthday)
{
TimeSpan? timeSpan;
bool isWrongYearFilterOrCounterPersonBirthday = IsWrongYearFilterOrCounterPersonBirthday(isWrongYear, personBirthday);
if (isWrongYearFilterOrCounterPersonBirthday)
timeSpan = null;
else
timeSpan = new(minimumDateTimeTicks - personBirthday.Value.Ticks);
return timeSpan;
}
internal static TimeSpan? GetTimeSpan(DateTime minimumDateTime, bool? isWrongYear, Models.PersonBirthday personBirthday)
{
TimeSpan? timeSpan = GetTimeSpan(minimumDateTime.Ticks, isWrongYear, personBirthday);
return timeSpan;
}
internal static (int, TimeSpan) GetAge(long dateTimeTicks, Models.PersonBirthday birthday)
{
TimeSpan result;
int years;
if (birthday?.Value is null)
throw new NullReferenceException(nameof(birthday.Value));
(years, result) = Age.GetAge(dateTimeTicks, birthday.Value);
return (years, result);
}
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;
}
}