Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update DayInYear.cs #227

Closed
wants to merge 3 commits into from
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 9 additions & 19 deletions src/DateTimeExtensions/WorkingDays/DayInYear.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,19 @@
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace DateTimeExtensions.WorkingDays
{
public class DayInYear
{
public DayInYear(int month, int day)
: this(month, day, new GregorianCalendar())
{
}
public DayInYear(int month, int day) : this(month, day, new GregorianCalendar()) { }

public DayInYear(int month, int day, Calendar calendar)
{
this.Month = month;
this.Day = day;
this.Calendar = calendar;
Month = month;
Day = day;
Calendar = calendar ?? throw new ArgumentNullException(nameof(calendar));
}

public int Day { get; private set; }
Expand All @@ -46,13 +40,10 @@ public DayInYear(int month, int day, Calendar calendar)

public DateTime GetDayOnYear(int year)
{
var firstDayOnGregoryanCalendar = new DateTime(year, 1, 1);
var dayInstance = Calendar.ToDateTime(Calendar.GetYear(firstDayOnGregoryanCalendar), Month, Day, 0, 0, 0, 0);
DateTime dayInstance = Calendar.ToDateTime(year, Month, Day, 0, 0, 0, 0);

//check if the instance day falls on previous year on Gregorian calendar.
// the instance should fall between year and year + 1.
// TODO: This smells a bit. Ensure this is true for all types of calendars.
if (dayInstance.Year < firstDayOnGregoryanCalendar.Year)
// Ensure the calculated day falls in the correct year
if (dayInstance.Year < year)
{
dayInstance = Calendar.AddYears(dayInstance, 1);
}
Expand All @@ -62,8 +53,7 @@ public DateTime GetDayOnYear(int year)

public bool IsTheSameDay(DateTime day)
{
var thisDayInYear = Calendar.ToDateTime(Calendar.GetYear(day), Month, Day, 0, 0, 0, 0);
return thisDayInYear == day.Date;
return Calendar.GetMonth(day) == Month && Calendar.GetDayOfMonth(day) == Day;
}
}
}
}
Loading