Skip to content

Commit

Permalink
Merge pull request #3 from MTokarev/feature/simplify-learYearSupport
Browse files Browse the repository at this point in the history
simplify leap years support
  • Loading branch information
MTokarev authored Jun 10, 2021
2 parents 21fd5a2 + d407cbe commit 8664f78
Showing 1 changed file with 2 additions and 11 deletions.
13 changes: 2 additions & 11 deletions mailSender/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ private static List<UserPrincipalExtension> GetCelebratingUsers(List<UserPrincip
if(ex3.Dob != DateTime.MinValue)
{
// Add user to the list if he has a birthday today.
if(HasBirthday(ex3.Dob))
// Comparing day and month would respect leap years.
if(ex3.Dob.Month == DateTime.Now.Month && ex3.Dob.Day == DateTime.Now.Day)
{
celebratingUsers.Add(user);
}
Expand Down Expand Up @@ -196,7 +197,6 @@ private static async Task GenerateEmailsAsync(List<UserPrincipalExtension> sendT
// Wainting when all tasks are done.
await Task.WhenAll(listOfTasks);
}

private static async Task SendBrthAsync(SendGridMessage mail, UserPrincipalExtension user)
{
// Creating client and sending messages.
Expand All @@ -213,14 +213,5 @@ private static async Task SendBrthAsync(SendGridMessage mail, UserPrincipalExten
_logger.Information($"Mail has been sent to: '{user.EmailAddress}'.");
}
}
// Handling leap year cases
// If user was born in a leap year and now it is not a leap one then 'DayOfYear' will be incosistent
private static bool HasBirthday(DateTime userDob)
{
string DobNormalizedStr = $"{userDob.Month}/{userDob.Day}/{DateTime.Now.Year}";
return DateTime.TryParse(DobNormalizedStr, out var DobNormalized) ?
DobNormalized.DayOfYear == DateTime.Now.DayOfYear :
false;
}
}
}

0 comments on commit 8664f78

Please sign in to comment.