Skip to content

Commit

Permalink
5 addworkinghours error (#7)
Browse files Browse the repository at this point in the history
resolved issue
  • Loading branch information
paonath authored Jan 19, 2023
1 parent f87a7d2 commit ccc252e
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<TargetFramework>net6.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,42 @@

namespace PH.WorkingDaysAndTimeUtility.UnitTest
{
public class WorkingDaysAndTimeUtilityUnitTest
public class Issue5Test : BaseTest
{
[Fact]
public void TestingIssue()
{
var wts1 = new WorkTimeSpan() { Start = new TimeSpan(08, 00, 0), End = new TimeSpan(12, 0, 0) };
var wts2 = new WorkTimeSpan() { Start = new TimeSpan(13, 30, 0), End = new TimeSpan(17, 30, 0) };
var wts = new List<WorkTimeSpan>() { wts1, wts2 };

var start = new DateTime(2023, 1, 11, 8, 0, 0);
var end = new DateTime(2023, 1, 11, 10, 0, 0);
var italiansHoliDays = new List<AHolyDay>() { new EasterMonday() };
var week = new WeekDaySpan()
{
WorkDays = new Dictionary<DayOfWeek, WorkDaySpan>()
{
{ DayOfWeek.Monday, new WorkDaySpan() { TimeSpans = wts } },
{ DayOfWeek.Tuesday, new WorkDaySpan() { TimeSpans = wts } },
{ DayOfWeek.Wednesday, new WorkDaySpan() { TimeSpans = wts } },
{ DayOfWeek.Thursday, new WorkDaySpan() { TimeSpans = wts } },
{ DayOfWeek.Friday, new WorkDaySpan() { TimeSpans = wts } },
{ DayOfWeek.Saturday, new WorkDaySpan() { TimeSpans = wts } }
}
};
var utility = new WorkingDaysAndTimeUtility(week, italiansHoliDays);
//var result = utility.AddWorkingDays(new DateTime(2023, 1, 13), 3);

var dateTime = new DateTime(2023, 1, 18, 12, 30, 22);
var result = utility.AddWorkingHours(dateTime, 1);

Assert.Equal(new DateTime(2023,1,18,14,30,22), result);
}

}

public class WorkingDaysAndTimeUtilityUnitTest
: BaseTest
{

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="GitInfo" Version="2.2.0">
<PackageReference Include="GitInfo" Version="2.3.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,43 +121,64 @@ public DateTime AddWorkingDays(DateTime start, int days)
/// <returns>Result DateTime</returns>
public DateTime AddWorkingHours(DateTime start, double hours)
{
CheckWorkDayStart(start);
var r = new DateTime(start.Ticks);

if (!IfWorkingMomentGettingNext(start, out DateTime nextStart))
CheckWorkDayStart(r);

while (!IfWorkingMomentGettingNext(r, out DateTime nextStart))
{
start = nextStart;
r = r.AddMinutes(1);
}

int year = start.Year;
List<DateTime> toExclude = CalculateDaysForExclusions(year);
for (int i = 0; i < 60 * hours; i++)
{
r= AddWorkingMinutes(r, 1, toExclude);
if (r.Year > year)
{
toExclude = CalculateDaysForExclusions(r.Year);
}
}

DateTime r = start;
var totMinutes = CheckWorkTimeStartandGetTotalWorkingHoursForTheDay(start);
List<DateTime> toExclude = CalculateDaysForExclusions(start.Year);
double hh = hours * 60;
return r;
//start = AddWorkingMinutes(start, 60 * hours)

if (totMinutes <= hh && WorkWeekConfiguration.Symmetrical )
{
#region Just for "Symmetrical" week
//if (!IfWorkingMomentGettingNext(start, out DateTime nextStart))
//{
// start = nextStart;
//}


var days = (int) (hh/totMinutes);
var otherMinutes = hh % totMinutes;
r = r.AddWorkingDays(days, toExclude, WorkingDaysInWeek);

if (otherMinutes > (double) 0)
{
r = AddWorkingMinutes(r, otherMinutes, toExclude);
}


#endregion
}
else
{
r = AddWorkingMinutes(r, totMinutes, toExclude);
//DateTime r = start;
//var totMinutes = CheckWorkTimeStartandGetTotalWorkingHoursForTheDay(start);
//List<DateTime> toExclude = CalculateDaysForExclusions(start.Year);
//double hh = hours * 60;

}
//if (totMinutes <= hh && WorkWeekConfiguration.Symmetrical )
//{
// #region Just for "Symmetrical" week


// var days = (int) (hh/totMinutes);
// var otherMinutes = hh % totMinutes;
// r = r.AddWorkingDays(days, toExclude, WorkingDaysInWeek);

// if (otherMinutes > (double) 0)
// {
// r = AddWorkingMinutes(r, otherMinutes, toExclude);
// }


// #endregion
//}
//else
//{
// r = AddWorkingMinutes(r, totMinutes, toExclude);

//}

return r;
// return r;
}

private DateTime AddWorkingMinutesNoCheck(DateTime start, double minutes)
Expand Down Expand Up @@ -750,7 +771,7 @@ private DateTime AddWorkingMinutes(DateTime start, double otherMinutes, List<Dat
{

var ts = nextInterval.Start;
r = new DateTime(r.Year, r.Month, r.Day, ts.Hours, ts.Minutes, ts.Seconds);
r = new DateTime(r.Year, r.Month, r.Day, ts.Hours, ts.Minutes, ts.Seconds);//.AddMinutes(1);
}
else
{
Expand All @@ -776,7 +797,13 @@ private TimeSpan GetFirstTimeSpanOfTheWorkingDay(DateTime d)
private bool CheckIfWorkTime(DateTime d, out WorkTimeSpan nextInterval)
{
var workDaySpan = WorkWeekConfiguration.WorkDays[d.DayOfWeek];
bool r = false;
var nextOutSpan = WorkWeekConfiguration.WorkDays.Where(x => x.Key > d.DayOfWeek).Select(x => x.Value).FirstOrDefault();
if (null == nextOutSpan)
{
nextOutSpan = WorkWeekConfiguration.WorkDays.FirstOrDefault().Value;
}

bool r = false;
nextInterval = null;

if (null == workDaySpan)
Expand All @@ -786,33 +813,75 @@ private bool CheckIfWorkTime(DateTime d, out WorkTimeSpan nextInterval)
}
else
{
var orderdTimes = (from t in workDaySpan.TimeSpans
orderby t.Start ascending, t.End ascending
select t
).ToArray();
int counter = -1;
foreach (var ts in orderdTimes)
{
counter++;

var s = new DateTime(d.Year, d.Month, d.Day, ts.Start.Hours, ts.Start.Minutes,
ts.Start.Seconds);
var e = new DateTime(d.Year, d.Month, d.Day, ts.End.Hours, ts.End.Minutes,
ts.End.Seconds);
nextInterval = counter == orderdTimes.Length - 1 ? null : orderdTimes[counter + 1];
if (d == e)
{
break;
}
else
{
if (s <= d && d < e)
{
r = true;
break;
}
}
}
var orderdTimes = workDaySpan.TimeSpans.OrderBy(x => x.Start).ThenBy(x => x.End).ToArray();
int c = 0;
foreach (var ts in orderdTimes)
{


var start = new DateTime(d.Year, d.Month, d.Day, ts.Start.Hours, ts.Start.Minutes, ts.Start.Seconds);
var end = new DateTime(d.Year, d.Month, d.Day, ts.End.Hours, ts.End.Minutes, ts.End.Seconds);

if (d < end)
{
if (d >= start)
{
return true;
}
else
{
nextInterval = ts;
}
}



//if (start <= d && d < end)
//{
// if (c <= orderdTimes.Length)
// {
// nextInterval = orderdTimes[c];
// }
// else
// {
// nextInterval = nextOutSpan.TimeSpans.OrderBy(x => x.Start).First();
// }

// return true;
//}
}

return false;

/*
var orderdTimes = (from t in workDaySpan.TimeSpans
orderby t.Start ascending, t.End ascending
select t
).ToArray();
int counter = -1;
foreach (var ts in orderdTimes)
{
counter++;
var s = new DateTime(d.Year, d.Month, d.Day, ts.Start.Hours, ts.Start.Minutes,
ts.Start.Seconds);
var e = new DateTime(d.Year, d.Month, d.Day, ts.End.Hours, ts.End.Minutes,
ts.End.Seconds);
nextInterval = counter == orderdTimes.Length - 1 ? null : orderdTimes[counter + 1];
if (d == e)
{
break;
}
else
{
if (s <= d && d < e)
{
r = true;
break;
}
}
}
*/
}

return r;
Expand Down

0 comments on commit ccc252e

Please sign in to comment.