136 lines
5.3 KiB
C#
136 lines
5.3 KiB
C#
using System.Globalization;
|
|
|
|
namespace View_by_Distance.Shared.Models.Stateless.Methods;
|
|
|
|
internal abstract class PersonBirthday
|
|
{
|
|
|
|
// ...
|
|
|
|
internal static Models.PersonBirthday? GetPersonBirthday(string personBirthdayFormat, string personKeyFormatted)
|
|
{
|
|
Models.PersonBirthday? result;
|
|
DateTime? dateTime;
|
|
if (personKeyFormatted.Length != personBirthdayFormat.Length)
|
|
result = null;
|
|
else
|
|
{
|
|
dateTime = IPersonBirthday.GetDateTime(personBirthdayFormat, personKeyFormatted);
|
|
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 < 1809)
|
|
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;
|
|
}
|
|
|
|
internal static List<(string, Models.PersonBirthday)> GetPersonBirthdays(string personBirthdayFormat, string[] personKeyDirectories, string personDisplayDirectory, string personDisplayDirectoryName)
|
|
{
|
|
List<(string, Models.PersonBirthday)> results = new();
|
|
string[] files;
|
|
string personKeyFormatted;
|
|
Models.PersonBirthday? personBirthday;
|
|
foreach (string personKeyDirectory in personKeyDirectories)
|
|
{
|
|
personKeyFormatted = Path.GetFileName(personKeyDirectory);
|
|
if (!DateTime.TryParseExact(personKeyFormatted, "MM.dd.yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime birthday))
|
|
personBirthday = IPersonBirthday.GetPersonBirthday(personBirthdayFormat, personKeyFormatted);
|
|
else
|
|
(personBirthday, personKeyFormatted) = Person.Get(personBirthdayFormat, personDisplayDirectory, personKeyDirectory, birthday);
|
|
if (personBirthday is null)
|
|
continue;
|
|
if (!IPersonBirthday.IsCounterPersonBirthday(personBirthday) && ((!personKeyDirectory.Contains('#') && (personDisplayDirectoryName.Contains('~') || personDisplayDirectoryName.Contains('#'))) || (personKeyDirectory.Contains('#') && !personDisplayDirectoryName.Contains('#'))))
|
|
throw new NotSupportedException();
|
|
results.Add(new(personKeyFormatted, personBirthday));
|
|
files = Directory.GetFiles(personKeyDirectory, "*", SearchOption.TopDirectoryOnly);
|
|
if (files.Any())
|
|
continue;
|
|
File.WriteAllText(Path.Combine(personKeyDirectory, $"{personKeyFormatted}.txt"), string.Empty);
|
|
}
|
|
return results;
|
|
}
|
|
|
|
} |