Removed Methods Namespace
This commit is contained in:
187
Shared/Models/Stateless/PersonBirthday.cs
Normal file
187
Shared/Models/Stateless/PersonBirthday.cs
Normal file
@ -0,0 +1,187 @@
|
||||
using System.Globalization;
|
||||
|
||||
namespace View_by_Distance.Shared.Models.Stateless;
|
||||
|
||||
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 TimeSpan? GetTimeSpan(long minimumDateTimeTicks, bool? isWrongYear, Models.PersonBirthday personBirthday)
|
||||
{
|
||||
TimeSpan? timeSpan;
|
||||
bool isWrongYearFilterOrCounterPersonBirthday = IPersonBirthday.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<PersonKeyFormattedAndPersonBirthday> GetPersonBirthdays(string personBirthdayFormat, string[] personKeyDirectories, string personDisplayDirectoryName)
|
||||
{
|
||||
List<PersonKeyFormattedAndPersonBirthday> results = [];
|
||||
string personKeyFormatted;
|
||||
Models.PersonBirthday? personBirthday;
|
||||
PersonKeyFormattedAndPersonBirthday personKeyFormattedAndPersonBirthday;
|
||||
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);
|
||||
continue;
|
||||
if (personBirthday is null)
|
||||
continue;
|
||||
if (!IPersonBirthday.IsCounterPersonBirthday(personBirthday) && ((!personKeyDirectory.Contains('#') && (personDisplayDirectoryName.Contains('~') || personDisplayDirectoryName.Contains('#'))) || (personKeyDirectory.Contains('#') && !personDisplayDirectoryName.Contains('#'))))
|
||||
throw new NotSupportedException();
|
||||
personKeyFormattedAndPersonBirthday = new(personKeyFormatted, personBirthday);
|
||||
results.Add(personKeyFormattedAndPersonBirthday);
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
private static string? GetMonthShortForm(string month)
|
||||
{
|
||||
string? result = month.ToLower()[0] switch
|
||||
{
|
||||
// 'j' => "jan",
|
||||
'f' => "feb",
|
||||
// 'm' => "mar",
|
||||
// 'a' => "apr",
|
||||
// 'm' => "may",
|
||||
// 'j' => "jun",
|
||||
// 'j' => "jul",
|
||||
// 'a' => "aug",
|
||||
's' => "sep",
|
||||
'o' => "oct",
|
||||
'n' => "nov",
|
||||
'd' => "dec",
|
||||
_ => null
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
internal static DateTime? GetDate(string month, string day, string year)
|
||||
{
|
||||
DateTime? result;
|
||||
DateTime dayDateTime;
|
||||
DateTime yearDateTime;
|
||||
DateTime monthDateTime;
|
||||
string? monthShortHand = string.IsNullOrEmpty(month) ? "x" : GetMonthShortForm(month);
|
||||
if (month.Length > 3)
|
||||
{
|
||||
if (!DateTime.TryParseExact($"{month},1,1500", "MMMM,d,yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out monthDateTime))
|
||||
monthDateTime = DateTime.MinValue;
|
||||
}
|
||||
else if (month.Length == 3)
|
||||
{
|
||||
if (!DateTime.TryParseExact($"{month},1,1500", "MMM,d,yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out monthDateTime))
|
||||
monthDateTime = DateTime.MinValue;
|
||||
}
|
||||
else if (month.Length == 1 && monthShortHand is not null)
|
||||
{
|
||||
if (!DateTime.TryParseExact($"{monthShortHand},1,1500", "MMM,d,yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out monthDateTime))
|
||||
monthDateTime = DateTime.MinValue;
|
||||
}
|
||||
else if (int.TryParse(month, out int _))
|
||||
{
|
||||
if (!DateTime.TryParseExact($"{month.PadLeft(2, '0')[..2]},1,1500", "MM,d,yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out monthDateTime))
|
||||
monthDateTime = DateTime.MinValue;
|
||||
}
|
||||
else
|
||||
monthDateTime = DateTime.MinValue;
|
||||
if (!int.TryParse(day, out int _))
|
||||
dayDateTime = DateTime.MinValue;
|
||||
else
|
||||
{
|
||||
if (!DateTime.TryParseExact($"01,{day.PadLeft(2, '0')[..2]},1500", "MM,d,yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out dayDateTime))
|
||||
dayDateTime = DateTime.MinValue;
|
||||
}
|
||||
if (year.Length == 2 && int.TryParse(year, out int _))
|
||||
{
|
||||
if (!DateTime.TryParseExact($"01,01,{year.PadLeft(4, '0')[..4]}", "MM,dd,yy", CultureInfo.InvariantCulture, DateTimeStyles.None, out yearDateTime))
|
||||
yearDateTime = DateTime.MinValue;
|
||||
}
|
||||
else if (year.Length == 4 && int.TryParse(year, out int _))
|
||||
{
|
||||
if (!DateTime.TryParseExact($"01,01,{year.PadLeft(4, '0')[..4]}", "MM,dd,yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out yearDateTime))
|
||||
yearDateTime = DateTime.MinValue;
|
||||
}
|
||||
else
|
||||
yearDateTime = DateTime.MinValue;
|
||||
result = monthDateTime == DateTime.MinValue ? null : dayDateTime == DateTime.MinValue ? null : yearDateTime == DateTime.MinValue ? null : new(yearDateTime.Year, monthDateTime.Month, dayDateTime.Day);
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user