Skip to content

Commit

Permalink
Merge pull request #28 from Toumash/feature/holidays-calendar
Browse files Browse the repository at this point in the history
adds full calendar holidays computing
  • Loading branch information
Toumash authored Oct 5, 2019
2 parents 70077d8 + 5be0a72 commit ca098c0
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 12 deletions.
8 changes: 4 additions & 4 deletions DailyStatus.Common/BLL/WorkDaysCalculator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ namespace DailyStatus.Common.BLL
{
public class WorkDaysCalculator
{
public TimeSpan ExpectedWorkedDays(DateTime todayWithHours, TimeSpan workDayStartHour, double numberOfWorkingHoursPerDay, params DateTime[] holidaysDuringWeek)
public TimeSpan MonthExpectedHours(DateTime todayWithHours, TimeSpan workDayStartHour, double numberOfWorkingHoursPerDay, params DateTime[] holidays)
{
var today = todayWithHours.Date;
var first = new DateTime(today.Year, today.Month, 1);
var workDayStart = today + workDayStartHour;

var worktime = TimeSpan.FromHours(first.BusinessDaysUntil(today, holidaysDuringWeek) * numberOfWorkingHoursPerDay);
var worktime = TimeSpan.FromHours(first.BusinessDaysUntil(today, holidays) * numberOfWorkingHoursPerDay);

if (today.DayOfWeek != DayOfWeek.Saturday && today.DayOfWeek != DayOfWeek.Sunday && !holidaysDuringWeek.Contains(today))
if (today.DayOfWeek != DayOfWeek.Saturday && today.DayOfWeek != DayOfWeek.Sunday && !holidays.Contains(today))
{
worktime -= TimeSpan.FromHours(numberOfWorkingHoursPerDay);
var diff = (todayWithHours - workDayStart).TotalHours;
Expand All @@ -30,7 +30,7 @@ public TimeSpan ExpectedWorkedDays(DateTime todayWithHours, TimeSpan workDayStar

public TimeSpan ExpectedWorkedDays(TimeSpan workDayStartHour, double numberOfWorkingHoursPerDay, params DateTime[] holidaysDuringWeek)
{
return ExpectedWorkedDays(DateTime.Now, workDayStartHour, numberOfWorkingHoursPerDay, holidaysDuringWeek);
return MonthExpectedHours(DateTime.Now, workDayStartHour, numberOfWorkingHoursPerDay, holidaysDuringWeek);
}
}

Expand Down
3 changes: 3 additions & 0 deletions DailyStatus.Common/Configuration/DailyStatusConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using DailyStatus.Common.Model;
using DailyStatus.Configuration;
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;

Expand All @@ -19,5 +21,6 @@ public class DailyStatusConfiguration
public int WorkDayStartHour { get; set; }
public int HoursADay { get; set; }
public long WorkspaceId { get; set; }
public List<DateTime> Holidays { get; set; }
}
}
38 changes: 31 additions & 7 deletions DailyStatus.CommonTests/BLL/WorkDaysCalculatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,56 +14,80 @@ public class WorkDaysCalculatorTests
[TestMethod()]
public void Given24OfMarch_ExpWorkDaysShoudReturn17Days_136Hours()
{
var actual = new WorkDaysCalculator().ExpectedWorkedDays(new DateTime(2018, 03, 24, 5, 5, 5), TimeSpan.FromHours(10), 8);
var actual = new WorkDaysCalculator().MonthExpectedHours(new DateTime(2018, 03, 24, 5, 5, 5), TimeSpan.FromHours(10), 8);
var expected = TimeSpan.FromHours(136);
Assert.AreEqual(expected.TotalHours / 8, actual.TotalHours / 8);
}

[TestMethod()]
public void Given23OfMarchEndOfDay_ExpWorkDaysShoudReturn17Days_136Hours()
{
var actual = new WorkDaysCalculator().ExpectedWorkedDays(new DateTime(2018, 03, 23, 20, 0, 0), TimeSpan.FromHours(8), 8);
var actual = new WorkDaysCalculator().MonthExpectedHours(new DateTime(2018, 03, 23, 20, 0, 0), TimeSpan.FromHours(8), 8);
var expected = TimeSpan.FromHours(136);
Assert.AreEqual(expected, actual);
}

[TestMethod()]
public void Given23OfMarchStartOdDay_ExpWorkDaysShoudReturn17Days_128Hours()
{
var actual = new WorkDaysCalculator().ExpectedWorkedDays(new DateTime(2018, 03, 23, 02, 0, 0), TimeSpan.FromHours(10), 8);
var actual = new WorkDaysCalculator().MonthExpectedHours(new DateTime(2018, 03, 23, 02, 0, 0), TimeSpan.FromHours(10), 8);
var expected = TimeSpan.FromHours(128);
Assert.AreEqual(expected, actual);
}

[TestMethod()]
public void Given23OfMarchStartOdDay_ExpWorkDaysShoudReturn17Days_On26March18()
{
var actual = new WorkDaysCalculator().ExpectedWorkedDays(new DateTime(2018, 03, 26, 8, 0, 0), TimeSpan.FromHours(10), 8);
var actual = new WorkDaysCalculator().MonthExpectedHours(new DateTime(2018, 03, 26, 8, 0, 0), TimeSpan.FromHours(10), 8);
var expected = TimeSpan.FromHours(17 * 8);
Assert.AreEqual(expected, actual);
}
[TestMethod()]
public void Given23OfMarchStartOdDay_ExpWorkDaysShoudReturn17AndAHalfDays_On26March18()
{
var actual = new WorkDaysCalculator().ExpectedWorkedDays(new DateTime(2018, 03, 26, 14, 0, 0), TimeSpan.FromHours(10), 8);
var actual = new WorkDaysCalculator().MonthExpectedHours(new DateTime(2018, 03, 26, 14, 0, 0), TimeSpan.FromHours(10), 8);
var expected = TimeSpan.FromHours(17 * 8 + 4);
Assert.AreEqual(expected, actual);
}

[TestMethod()]
public void Given23OfMarchStartOdDay_ExpWorkDaysShoudReturn17AndMinutes_On26March18()
{
var actual = new WorkDaysCalculator().ExpectedWorkedDays(new DateTime(2018, 03, 26, 11, 0, 0), TimeSpan.FromHours(10), 8);
var actual = new WorkDaysCalculator().MonthExpectedHours(new DateTime(2018, 03, 26, 11, 0, 0), TimeSpan.FromHours(10), 8);
var expected = TimeSpan.FromHours(17 * 8 + 1);
Assert.AreEqual(expected, actual);
}

[TestMethod()]
public void Given8OfMarchStartOdDay_ExpWorkDaysShoudReturn5Days5Hours_On8March15()
{
var actual = new WorkDaysCalculator().ExpectedWorkedDays(new DateTime(2019, 03, 8, 15, 0, 0), TimeSpan.FromHours(10), 8);
var actual = new WorkDaysCalculator().MonthExpectedHours(new DateTime(2019, 03, 8, 15, 0, 0), TimeSpan.FromHours(10), 8);
var expected = TimeSpan.FromHours(5 * 8 + 5);
Assert.AreEqual(expected, actual);
}

[TestMethod]
public void Given12_11_2019_ShouldReturn6Days_BecauseThereIsOneHolidayOn11_11()
{
var actual = new WorkDaysCalculator().MonthExpectedHours(
todayWithHours: new DateTime(2019, 11, 12),
workDayStartHour: TimeSpan.FromHours(10),
numberOfWorkingHoursPerDay: 8,
new DateTime(2019, 11, 11));
var expected = TimeSpan.FromHours(6 * 8);
Assert.AreEqual(expected, actual);
}

[TestMethod]
public void Given7_01_2019_ShouldReturn3Days()
{
var actual = new WorkDaysCalculator().MonthExpectedHours(
todayWithHours: new DateTime(2019, 1, 7),
workDayStartHour: TimeSpan.FromHours(10),
numberOfWorkingHoursPerDay: 8,
new DateTime(2019, 1, 1), new DateTime(2019, 1, 6));
var expected = TimeSpan.FromHours(3 * 8);
Assert.AreEqual(expected, actual);
}
}
}
3 changes: 3 additions & 0 deletions DailyStatus.UI/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
<setting name="hours_a_day" serializeAs="String">
<value>8</value>
</setting>
<setting name="holidays" serializeAs="String">
<value>1/01/2019,01/06/2019,04/21/2019,04/22/2019,05/01/2019,05/03/2019,06/09/2019,06/20/2019,08/15/2019,11/01/2019,11/11/2019,12/25/2019,12/26/2019</value>
</setting>
</DailyStatus.UI.Properties.Settings>
</userSettings>
</configuration>
15 changes: 14 additions & 1 deletion DailyStatus.UI/Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions DailyStatus.UI/Properties/Settings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,8 @@
<Setting Name="hours_a_day" Type="System.Int32" Scope="User">
<Value Profile="(Default)">8</Value>
</Setting>
<Setting Name="holidays" Type="System.String" Scope="User">
<Value Profile="(Default)">1/01/2019,01/06/2019,04/21/2019,04/22/2019,05/01/2019,05/03/2019,06/09/2019,06/20/2019,08/15/2019,11/01/2019,11/11/2019,12/25/2019,12/26/2019</Value>
</Setting>
</Settings>
</SettingsFile>
3 changes: 3 additions & 0 deletions DailyStatus.UI/SettingsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using DailyStatus.UI.Properties;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
Expand All @@ -20,6 +21,7 @@ public static DailyStatusConfiguration LoadSettings()
cfg.WorkDayStartHour = stg.start_hour;
cfg.HoursADay = stg.hours_a_day;
cfg.WorkspaceId = stg.api_workspaceid;
cfg.Holidays = stg.holidays.Split(',').Select(str => DateTime.Parse(str, CultureInfo.InvariantCulture)).ToList();
return cfg;
}

Expand All @@ -31,6 +33,7 @@ public static void SaveSettings(DailyStatusConfiguration cfg)
stg.hour_rate = cfg.HourRate;
stg.start_hour = cfg.WorkDayStartHour;
stg.hours_a_day = cfg.HoursADay;
stg.holidays = string.Join(",", cfg.Holidays.Select(d => d.ToString(CultureInfo.InvariantCulture)));
stg.Save();
}
}
Expand Down

0 comments on commit ca098c0

Please sign in to comment.