Skip to content

Commit

Permalink
Merge branch 'hotfix/time_splitting_bug'
Browse files Browse the repository at this point in the history
  • Loading branch information
paonath committed Jul 14, 2021
2 parents 3e11d14 + 61e5930 commit 1c89359
Show file tree
Hide file tree
Showing 7 changed files with 191 additions and 95 deletions.
6 changes: 3 additions & 3 deletions PH.WorkingDaysAndTime/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
<PropertyGroup>
<Authors>Paolo Innocenti</Authors>
<Copyright>Copyright 2021 (c) Paolo Innocenti - [email protected] </Copyright>
<AssemblyVersion>2.0.7</AssemblyVersion>
<FileVersion>2.0.7</FileVersion>
<Version>2.0.7</Version>
<AssemblyVersion>2.0.8</AssemblyVersion>
<FileVersion>2.0.8</FileVersion>
<Version>2.0.8</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)'=='Debug'">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using PH.WorkingDaysAndTimeUtility.Configuration;
using Xunit;
Expand All @@ -17,76 +18,42 @@ public static TimeSlotConfig GetTimeSlotConfig()
TimesDictionary = new Dictionary<DayOfWeek, List<TimeSlot>>()
};



cfg.TimesDictionary.Add(DayOfWeek.Monday, new List<TimeSlot>()
var l = new List<TimeSlot>()
{
new TimeSlot("Straordinario feriale notturno ", 1.1, 2.1, new TimeSpan(0, 0, 0), new TimeSpan(6, 0, 0)),
new TimeSlot("Straordinario feriale diurno ", 1.3, 2.3, new TimeSpan(6, 01, 0), new TimeSpan(8, 59, 59)),
new TimeSlot("Ordinario ", 1.0, 2.0, new TimeSpan(9, 0, 0), new TimeSpan(18, 0, 0)),
new TimeSlot("Straordinario feriale diurno 2", 1.3, 2.3, new TimeSpan(18, 01, 0), new TimeSpan(20, 0, 0)),
new TimeSlot("Straordinario feriale notturno 2", 1.1, 2.1, new TimeSpan(20, 01, 0), new TimeSpan(23, 59, 59)),
});
new TimeSlot("Straordinario feriale notturno ", 1.1, new TimeSpan(0, 0, 0), new TimeSpan(6, 0, 0)),
new TimeSlot("Straordinario feriale diurno ", 1.3, new TimeSpan(6, 01, 0), new TimeSpan(8, 59, 59)),
new OrdinaryTimeSlot("Ordinario ", new TimeSpan(9, 0, 0), new TimeSpan(18, 0, 0)),
new TimeSlot("Straordinario feriale diurno 2", 1.3, new TimeSpan(18, 01, 0), new TimeSpan(20, 0, 0)),
new TimeSlot("Straordinario feriale notturno 2", 1.1, new TimeSpan(20, 01, 0),
new TimeSpan(23, 59, 59)),
};

cfg.TimesDictionary.Add(DayOfWeek.Tuesday, new List<TimeSlot>()
{
new TimeSlot("Straordinario feriale notturno ", 1.1, 2.1, new TimeSpan(0, 0, 0), new TimeSpan(6, 0, 0)),
new TimeSlot("Straordinario feriale diurno ", 1.3, 2.3, new TimeSpan(6, 01, 0), new TimeSpan(8, 59, 59)),
new TimeSlot("Ordinario ", 1.0, 2.0, new TimeSpan(9, 0, 0), new TimeSpan(18, 0, 0)),
new TimeSlot("Straordinario feriale diurno 2", 1.3, 2.3, new TimeSpan(18, 01, 0), new TimeSpan(20, 0, 0)),
new TimeSlot("Straordinario feriale notturno 2", 1.1, 2.1, new TimeSpan(0, 0, 0), new TimeSpan(6, 0, 0)),
});
cfg.TimesDictionary.Add(DayOfWeek.Monday, l);

cfg.TimesDictionary.Add(DayOfWeek.Wednesday, new List<TimeSlot>()
{
new TimeSlot("Straordinario feriale notturno ", 1.1, 2.1, new TimeSpan(0, 0, 0), new TimeSpan(6, 0, 0)),
new TimeSlot("Straordinario feriale diurno ", 1.3, 2.3, new TimeSpan(6, 01, 0), new TimeSpan(8, 59, 59)),
new TimeSlot("Ordinario ", 1.0, 2.0, new TimeSpan(9, 0, 0), new TimeSpan(18, 0, 0)),
new TimeSlot("Straordinario feriale diurno 2", 1.3, 2.3, new TimeSpan(18, 01, 0), new TimeSpan(20, 0, 0)),
new TimeSlot("Straordinario feriale notturno 2", 1.1, 2.1, new TimeSpan(20, 01, 0), new TimeSpan(23, 59, 59)),
});
cfg.TimesDictionary.Add(DayOfWeek.Tuesday, l);

cfg.TimesDictionary.Add(DayOfWeek.Wednesday,l);

cfg.TimesDictionary.Add(DayOfWeek.Thursday, new List<TimeSlot>()
{
new TimeSlot("Straordinario feriale notturno ", 1.1, 2.1, new TimeSpan(0, 0, 0), new TimeSpan(6, 0, 0)),
new TimeSlot("Straordinario feriale diurno ", 1.3, 2.3, new TimeSpan(6, 01, 0), new TimeSpan(8, 59, 59)),
new TimeSlot("Ordinario ", 1.0, 2.0, new TimeSpan(9, 0, 0), new TimeSpan(18, 0, 0)),
new TimeSlot("Straordinario feriale diurno 2", 1.3, 2.3, new TimeSpan(18, 01, 0), new TimeSpan(20, 0, 0)),
new TimeSlot("Straordinario feriale notturno 2", 1.1, 2.1, new TimeSpan(20, 01, 0), new TimeSpan(23, 59, 59)),
new TimeSlot("Straordinario feriale notturno ", 1.1, new TimeSpan(0, 0, 0), new TimeSpan(6, 0, 0)),
new TimeSlot("Straordinario feriale diurno ", 1.3, new TimeSpan(6, 01, 0), new TimeSpan(8, 59, 59)),
new TimeSlot("Ordinario ", 1.0, new TimeSpan(9, 0, 0), new TimeSpan(18, 0, 0)),
new TimeSlot("Straordinario feriale diurno 2", 1.3, new TimeSpan(18, 01, 0), new TimeSpan(20, 0, 0)),
new TimeSlot("Straordinario feriale notturno 2", 1.1, new TimeSpan(20, 01, 0), new TimeSpan(23, 59, 59)),
});

cfg.TimesDictionary.Add(DayOfWeek.Friday, new List<TimeSlot>()
{
new TimeSlot("Straordinario feriale notturno ", 1.1, 2.1, new TimeSpan(0, 0, 0), new TimeSpan(6, 0, 0)),
new TimeSlot("Straordinario feriale diurno ", 1.3, 2.3, new TimeSpan(6, 01, 0), new TimeSpan(8, 59, 59)),
new TimeSlot("Ordinario ", 1.0, 2.0, new TimeSpan(9, 0, 0), new TimeSpan(18, 0, 0)),
new TimeSlot("Straordinario feriale diurno 2", 1.3, 2.3, new TimeSpan(18, 01, 0), new TimeSpan(20, 0, 0)),
new TimeSlot("Straordinario feriale notturno 2", 1.1, 2.1, new TimeSpan(20, 01, 0), new TimeSpan(23, 59, 59)),
});
cfg.TimesDictionary.Add(DayOfWeek.Saturday, new List<TimeSlot>()
{
new TimeSlot("Straordinario feriale notturno ", 1.1, 2.1, new TimeSpan(0, 0, 0), new TimeSpan(6, 0, 0)),
new TimeSlot("Straordinario feriale diurno ", 1.3, 2.3, new TimeSpan(6, 01, 0), new TimeSpan(8, 59, 59)),
new TimeSlot("Ordinario ", 1.0, 2.0, new TimeSpan(9, 0, 0), new TimeSpan(18, 0, 0)),
new TimeSlot("Straordinario feriale diurno 2", 1.3, 2.3, new TimeSpan(18, 01, 0), new TimeSpan(20, 0, 0)),
new TimeSlot("Straordinario feriale notturno 2", 1.1, 2.1, new TimeSpan(20, 01, 0), new TimeSpan(23, 59, 59)),
});
cfg.TimesDictionary.Add(DayOfWeek.Sunday, new List<TimeSlot>()
{
new TimeSlot("Straordinario feriale notturno ", 1.1, 2.1, new TimeSpan(0, 0, 0), new TimeSpan(6, 0, 0)),
new TimeSlot("Straordinario feriale diurno ", 1.3, 2.3, new TimeSpan(6, 01, 0), new TimeSpan(8, 59, 59)),
new TimeSlot("Ordinario ", 1.0, 2.0, new TimeSpan(9, 0, 0), new TimeSpan(18, 0, 0)),
new TimeSlot("Straordinario feriale diurno 2", 1.3, 2.3, new TimeSpan(18, 01, 0), new TimeSpan(20, 0, 0)),
new TimeSlot("Straordinario feriale notturno 2", 1.1, 2.1, new TimeSpan(20, 01, 0), new TimeSpan(23, 59, 59)),
});
cfg.TimesDictionary.Add(DayOfWeek.Friday,l);
cfg.TimesDictionary.Add(DayOfWeek.Saturday, l);
cfg.TimesDictionary.Add(DayOfWeek.Sunday, l);

cfg.HolyDaySlots = new List<TimeSlot>()
{
new TimeSlot("Straordinario feriale notturno ", 1.1, 2.1, new TimeSpan(0, 0, 0), new TimeSpan(6, 0, 0)),
new TimeSlot("Straordinario feriale diurno ", 1.3, 2.3, new TimeSpan(6, 01, 0), new TimeSpan(8, 59, 59)),
new TimeSlot("Ordinario ", 1.0, 2.0, new TimeSpan(9, 0, 0), new TimeSpan(18, 0, 0)),
new TimeSlot("Straordinario feriale diurno 2", 1.3, 2.3, new TimeSpan(18, 01, 0), new TimeSpan(20, 0, 0)),
new TimeSlot("Straordinario feriale notturno 2", 1.1, 2.1, new TimeSpan(20, 01, 0), new TimeSpan(23, 59, 59)),
new TimeSlot("Straordinario feriale notturno ", 1.3, new TimeSpan(0, 0, 0), new TimeSpan(6, 0, 0)),
new TimeSlot("Straordinario feriale diurno ", 1.3, new TimeSpan(6, 01, 0), new TimeSpan(8, 59, 59)),
new TimeSlot("Ordinario ", 1.3, new TimeSpan(9, 0, 0), new TimeSpan(18, 0, 0)),
new TimeSlot("Straordinario feriale diurno 2", 1.3, new TimeSpan(18, 01, 0), new TimeSpan(20, 0, 0)),
new TimeSlot("Straordinario feriale notturno 2", 1.3, new TimeSpan(20, 01, 0), new TimeSpan(23, 59, 59)),
};

return cfg;
Expand Down Expand Up @@ -168,11 +135,11 @@ public void CheckValues()

var slots = new List<TimeSlot>()
{
new TimeSlot($"STRAORD_FERIALE_NOTTURNO" , 2.0, 2.0, new TimeSpan(0, 0, 0), new TimeSpan(5, 59, 59)),
new TimeSlot($"STRAORD_FERIALE_DIURNO" , 1.4, 1.4, new TimeSpan(6, 00, 0), new TimeSpan(8, 59, 59)),
new TimeSlot($"ORDINARIO" , 1.0, 1.4, new TimeSpan(9, 0, 0), new TimeSpan(17, 59, 59)),
new TimeSlot($"STRAORD_FERIALE_DIURNO" , 1.4, 1.4, new TimeSpan(18, 00, 0), new TimeSpan(19,59, 59)),
new TimeSlot($"STRAORD_FERIALE_NOTTURNO" , 2.0, 2.0, new TimeSpan(20, 00, 0), new TimeSpan(23, 59, 59))
new TimeSlot($"STRAORD_FERIALE_NOTTURNO" , 2.0, new TimeSpan(0, 0, 0), new TimeSpan(5, 59, 59)),
new TimeSlot($"STRAORD_FERIALE_DIURNO" , 1.4, new TimeSpan(6, 00, 0), new TimeSpan(8, 59, 59)),
new OrdinaryTimeSlot($"ORDINARIO" , new TimeSpan(9, 0, 0), new TimeSpan(17, 59, 59)),
new TimeSlot($"STRAORD_FERIALE_DIURNO" , 1.4, new TimeSpan(18, 00, 0), new TimeSpan(19,59, 59)),
new TimeSlot($"STRAORD_FERIALE_NOTTURNO" , 2.0, new TimeSpan(20, 00, 0), new TimeSpan(23, 59, 59))
};
timecfg.TimesDictionary.Add(DayOfWeek.Monday,slots);
timecfg.TimesDictionary.Add(DayOfWeek.Tuesday,slots);
Expand All @@ -183,16 +150,16 @@ public void CheckValues()

timecfg.TimesDictionary.Add(DayOfWeek.Saturday, new List<TimeSlot>()
{
new TimeSlot($"STRAORD_FERIALE_NOTTURNO" , 2.0, 2.0, new TimeSpan(0, 0, 0), new TimeSpan(5, 59, 59)),
new TimeSlot($"STRAORD_FERIALE_DIURNO" , 1.4, 1.4, new TimeSpan(6, 00, 0), new TimeSpan(19, 59, 59)),
new TimeSlot($"STRAORD_FERIALE_NOTTURNO" , 2.0, 2.0, new TimeSpan(20, 00, 0), new TimeSpan(23, 59, 59))
new TimeSlot($"STRAORD_FERIALE_NOTTURNO" , 2.0, new TimeSpan(0, 0, 0), new TimeSpan(5, 59, 59)),
new TimeSlot($"STRAORD_FERIALE_DIURNO" , 1.4, new TimeSpan(6, 00, 0), new TimeSpan(19, 59, 59)),
new TimeSlot($"STRAORD_FERIALE_NOTTURNO" , 2.0, new TimeSpan(20, 00, 0), new TimeSpan(23, 59, 59))
});

var sunDay = new List<TimeSlot>()
{
new TimeSlot($"STRAORD_FESTIVO_NOTTURNO" , 2.0, 2.0, new TimeSpan(0, 0, 0), new TimeSpan(6, 0, 0)),
new TimeSlot($"STRAORD_FESTIVO_DIURNO" , 1.4, 1.4, new TimeSpan(6, 00, 0), new TimeSpan(19, 59, 59)),
new TimeSlot($"STRAORD_FESTIVO_NOTTURNO" , 2.0,2.0, new TimeSpan(20, 00, 01), new TimeSpan(23, 59, 59))
new TimeSlot($"STRAORD_FESTIVO_NOTTURNO" , 2.0, new TimeSpan(0, 0, 0), new TimeSpan(6, 0, 0)),
new TimeSlot($"STRAORD_FESTIVO_DIURNO" , 1.4, new TimeSpan(6, 00, 0), new TimeSpan(19, 59, 59)),
new TimeSlot($"STRAORD_FESTIVO_NOTTURNO" , 2.0, new TimeSpan(20, 00, 01), new TimeSpan(23, 59, 59))
};

timecfg.TimesDictionary.Add(DayOfWeek.Sunday, sunDay);
Expand Down Expand Up @@ -239,12 +206,16 @@ public void CheckValues()
.Select(x => x.Duration.TotalHours).Sum();


//var aHoly = u.SplitWorkedTimeInFactors(new DateTime(2021, 6, 2, 0, 0, 0),
// new DateTime(2021, 6, 2, 23, 59, 59));

var aHoly = u.SplitWorkedTimeInFactors(new DateTime(2021, 6, 2, 0, 0, 0),
new DateTime(2021, 6, 2, 23, 59, 59));
new DateTime(2021, 6, 2, 0, 0, 2));


Assert.Equal((double)9, ordinario);
Assert.Equal((double)9, ordinario);
Debug.Assert(aHoly.WorkSlices != null, "aHoly.WorkSlices != null");
Assert.Equal(true, aHoly.WorkSlices.FirstOrDefault().OnHolyDay);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public static DateTimeSlot Build(TimeSlot slot, DateTime d)
End = slot.End,
Factor = slot.Factor,
Key = slot.Key,
HolyDayFactor = slot.HolyDayFactor

};
}
/// <summary>If Datetime in the slot.</summary>
Expand Down Expand Up @@ -103,9 +103,9 @@ public class TimeSlot
/// <value>The factor.</value>
public double Factor { get; set; }

/// <summary>Gets or sets the holy day factor if work on this time slice.</summary>
/// <value>The holy day factor.</value>
public double HolyDayFactor { get; set; }
///// <summary>Gets or sets the holy day factor if work on this time slice.</summary>
///// <value>The holy day factor.</value>
//public double HolyDayFactor { get; set; }

/// <summary>
/// Starting Time for No-Work time slice
Expand All @@ -122,15 +122,15 @@ public TimeSlot()
{

}

/// <summary>
/// Initializes a new instance of the <see cref="TimeSlot"/> class.
/// </summary>
/// <param name="factor">The factor.</param>
/// <param name="holyDayFactor">The holy day factor.</param>
/// <param name="start">The start.</param>
/// <param name="end">The end.</param>
public TimeSlot(double factor, double holyDayFactor, TimeSpan start, TimeSpan end)
:this(null,factor,holyDayFactor,start,end)
public TimeSlot(double factor, TimeSpan start, TimeSpan end)
:this(null,factor,start,end)
{

}
Expand All @@ -140,14 +140,13 @@ public TimeSlot(double factor, double holyDayFactor, TimeSpan start, TimeSpan en
/// </summary>
/// <param name="key">The key.</param>
/// <param name="factor">The factor.</param>
/// <param name="holyDayFactor">The holy day factor.</param>
/// <param name="start">The start.</param>
/// <param name="end">The end.</param>
public TimeSlot(string key,double factor, double holyDayFactor, TimeSpan start, TimeSpan end)
public TimeSlot(string key,double factor, TimeSpan start, TimeSpan end)
{
Key = key;
Factor = factor;
HolyDayFactor = holyDayFactor;
//HolyDayFactor = holyDayFactor;
Start = start;
End = end;
}
Expand Down Expand Up @@ -185,6 +184,24 @@ public override string ToString()
}
}

/// <summary>
/// Initializes a new instance of the <see cref="OrdinaryTimeSlot"/> class: is a <see cref="TimeSlot"/> with <see cref="TimeSlot.Factor"/> fixed to 1.0.
/// </summary>
/// <seealso cref="PH.WorkingDaysAndTimeUtility.Configuration.TimeSlot" />
public class OrdinaryTimeSlot : TimeSlot
{
public OrdinaryTimeSlot()
{
Factor = (double)1.0;
}

public OrdinaryTimeSlot(string key, TimeSpan start, TimeSpan end) : base(key,1.0,start, end)
{

}

}

public class WorkeTimeSlice
{
public DateTime Start { get; internal set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,13 @@ public static bool IsWorkInstant(this WorkTimeSpan span, int hours, int minutes)

return d0 <= d2 && d2 <= d1;
}
public static bool IsStrictWorkInstant(this WorkTimeSpan span, int hours, int minutes,int seconds)
{
DateTime d0 = new DateTime(2000,1,3,span.Start.Hours,span.Start.Minutes,0);
DateTime d1 = new DateTime(2000,1,3,span.End.Hours,span.End.Minutes,0);
DateTime d2 = new DateTime(2000,1,3,hours,minutes,seconds);

return d0 <= d2 && d2 < d1;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PH.WorkingDaysAndTimeUtility.Configuration;

namespace PH.WorkingDaysAndTimeUtility
{
Expand Down Expand Up @@ -121,5 +122,44 @@ List<DateTime> GetWorkingDaysBetweenTwoWorkingDateTimes(DateTime start, DateTime
/// </returns>
bool IsAWorkDay(DateTime day);

/// <summary>
/// Determines whether if given <see cref="DateTime"/> is holy day(do not check for Hours/Minutes, jut Day)..
/// </summary>
/// <param name="day">The day.</param>
/// <returns>
/// <c>true</c> if given <see cref="DateTime"/> is holy day; otherwise, <c>false</c>.
/// </returns>
bool IsHolyDay(DateTime day);

}

/// <summary>
/// A tiny utility for split a <see cref="TimeSpan"/> into worked-slices, based on ordinary or extraordinary time table.
///
/// Based on <see cref="IWorkingDaysAndTimeUtility"/> this utility do not use the same work-day config and algorithm.
/// </summary>
public interface ISplitTimes
{
/// <summary>Sets the time slot configuration.</summary>
/// <param name="config">The configuration.</param>
void SetTimeSlotConfig(TimeSlotConfig config);

/// <summary>Splits the worked time in factors.</summary>
/// <param name="start">The start.</param>
/// <param name="end">The end.</param>
/// <returns></returns>
/// <exception cref="ArgumentOutOfRangeException">
/// start - End Time '{end:O}' must be greather than Start Time '{start:O}'
/// or
/// end - End must be on same Date of Start
/// </exception>
/// <exception cref="ArgumentNullException">
/// WorkWeekConfiguration - WorkWeekConfiguration Config mandatory
/// or
/// WorkWeekConfiguration - TimeSlotConfig Config mandatory
/// or
/// WorkWeekConfiguration - TimeSlotConfig Config not found for '{start.DayOfWeek}'
/// </exception>
WorkedTimeSliceResult SplitWorkedTimeInFactors(DateTime start, DateTime end);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@

The application works only counting the dates forward and it is assumed that the date entered as the first parameter is a working day.</Description>
<PackageReleaseNotes>
Now able to split a Worked times into slices (ordinary, extraordinary, etc.)
Fix time splitting bugs.
Now able to determine if given date is holyday
</PackageReleaseNotes>


<PackageTags>work-days,DateTime,work-hours,work-minutes,work-DateTime,holiday,timespan, time-slices</PackageTags>

<PackageVersion>2.0.7</PackageVersion>
<PackageVersion>2.0.8</PackageVersion>
<Authors>Paolo Innocenti</Authors>
<Copyright>Copyright PH 2021 (c) [email protected]. All rights reserved.</Copyright>
<RepositoryUrl>https://github.com/paonath/PH.WorkingDaysAndTime</RepositoryUrl>
Expand Down
Loading

0 comments on commit 1c89359

Please sign in to comment.