From 61e5930f83303ff67eb21416f868c73f5e17d6ce Mon Sep 17 00:00:00 2001 From: Paolo Innocenti Date: Wed, 14 Jul 2021 06:53:59 +0200 Subject: [PATCH] OK --- PH.WorkingDaysAndTime/Directory.Build.props | 6 +- .../SplitTimeInFactorsUnitTest.cs | 115 +++++++----------- .../Configuration/WorkTimeSpan.cs | 37 ++++-- .../Extensions/WorkTimeSpanExtension.cs | 8 ++ .../IWorkingDaysAndTimeUtility.cs | 40 ++++++ .../PH.WorkingDaysAndTimeUtility.csproj | 5 +- .../WorkingDaysAndTimeUtility.cs | 75 ++++++++++-- 7 files changed, 191 insertions(+), 95 deletions(-) diff --git a/PH.WorkingDaysAndTime/Directory.Build.props b/PH.WorkingDaysAndTime/Directory.Build.props index a35f994..e7000c9 100644 --- a/PH.WorkingDaysAndTime/Directory.Build.props +++ b/PH.WorkingDaysAndTime/Directory.Build.props @@ -2,9 +2,9 @@ Paolo Innocenti Copyright 2021 (c) Paolo Innocenti - paonath@gmail.com - 2.0.7 - 2.0.7 - 2.0.7 + 2.0.8 + 2.0.8 + 2.0.8 diff --git a/PH.WorkingDaysAndTime/PH.WorkingDaysAndTimeUtility.UnitTest/SplitTimeInFactorsUnitTest.cs b/PH.WorkingDaysAndTime/PH.WorkingDaysAndTimeUtility.UnitTest/SplitTimeInFactorsUnitTest.cs index 68ae365..ef9d250 100644 --- a/PH.WorkingDaysAndTime/PH.WorkingDaysAndTimeUtility.UnitTest/SplitTimeInFactorsUnitTest.cs +++ b/PH.WorkingDaysAndTime/PH.WorkingDaysAndTimeUtility.UnitTest/SplitTimeInFactorsUnitTest.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.Linq; using PH.WorkingDaysAndTimeUtility.Configuration; using Xunit; @@ -17,76 +18,42 @@ public static TimeSlotConfig GetTimeSlotConfig() TimesDictionary = new Dictionary>() }; - - - cfg.TimesDictionary.Add(DayOfWeek.Monday, new List() + var l = new List() { - 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() - { - 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() - { - 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() { - 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() - { - 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() - { - 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() - { - 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() { - 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; @@ -168,11 +135,11 @@ public void CheckValues() var slots = new List() { - 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); @@ -183,16 +150,16 @@ public void CheckValues() timecfg.TimesDictionary.Add(DayOfWeek.Saturday, new List() { - 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() { - 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); @@ -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); } } } \ No newline at end of file diff --git a/PH.WorkingDaysAndTime/PH.WorkingDaysAndTimeUtility/Configuration/WorkTimeSpan.cs b/PH.WorkingDaysAndTime/PH.WorkingDaysAndTimeUtility/Configuration/WorkTimeSpan.cs index f3371de..cb13e5e 100644 --- a/PH.WorkingDaysAndTime/PH.WorkingDaysAndTimeUtility/Configuration/WorkTimeSpan.cs +++ b/PH.WorkingDaysAndTime/PH.WorkingDaysAndTimeUtility/Configuration/WorkTimeSpan.cs @@ -73,7 +73,7 @@ public static DateTimeSlot Build(TimeSlot slot, DateTime d) End = slot.End, Factor = slot.Factor, Key = slot.Key, - HolyDayFactor = slot.HolyDayFactor + }; } /// If Datetime in the slot. @@ -103,9 +103,9 @@ public class TimeSlot /// The factor. public double Factor { get; set; } - /// Gets or sets the holy day factor if work on this time slice. - /// The holy day factor. - public double HolyDayFactor { get; set; } + ///// Gets or sets the holy day factor if work on this time slice. + ///// The holy day factor. + //public double HolyDayFactor { get; set; } /// /// Starting Time for No-Work time slice @@ -122,15 +122,15 @@ public TimeSlot() { } + /// /// Initializes a new instance of the class. /// /// The factor. - /// The holy day factor. /// The start. /// The end. - 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) { } @@ -140,14 +140,13 @@ public TimeSlot(double factor, double holyDayFactor, TimeSpan start, TimeSpan en /// /// The key. /// The factor. - /// The holy day factor. /// The start. /// The end. - 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; } @@ -185,6 +184,24 @@ public override string ToString() } } + /// + /// Initializes a new instance of the class: is a with fixed to 1.0. + /// + /// + 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; } diff --git a/PH.WorkingDaysAndTime/PH.WorkingDaysAndTimeUtility/Extensions/WorkTimeSpanExtension.cs b/PH.WorkingDaysAndTime/PH.WorkingDaysAndTimeUtility/Extensions/WorkTimeSpanExtension.cs index 9d01e5d..62d28ab 100644 --- a/PH.WorkingDaysAndTime/PH.WorkingDaysAndTimeUtility/Extensions/WorkTimeSpanExtension.cs +++ b/PH.WorkingDaysAndTime/PH.WorkingDaysAndTimeUtility/Extensions/WorkTimeSpanExtension.cs @@ -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; + } } } diff --git a/PH.WorkingDaysAndTime/PH.WorkingDaysAndTimeUtility/IWorkingDaysAndTimeUtility.cs b/PH.WorkingDaysAndTime/PH.WorkingDaysAndTimeUtility/IWorkingDaysAndTimeUtility.cs index eb36876..21ed096 100644 --- a/PH.WorkingDaysAndTime/PH.WorkingDaysAndTimeUtility/IWorkingDaysAndTimeUtility.cs +++ b/PH.WorkingDaysAndTime/PH.WorkingDaysAndTimeUtility/IWorkingDaysAndTimeUtility.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; +using PH.WorkingDaysAndTimeUtility.Configuration; namespace PH.WorkingDaysAndTimeUtility { @@ -121,5 +122,44 @@ List GetWorkingDaysBetweenTwoWorkingDateTimes(DateTime start, DateTime /// bool IsAWorkDay(DateTime day); + /// + /// Determines whether if given is holy day(do not check for Hours/Minutes, jut Day).. + /// + /// The day. + /// + /// true if given is holy day; otherwise, false. + /// + bool IsHolyDay(DateTime day); + + } + + /// + /// A tiny utility for split a into worked-slices, based on ordinary or extraordinary time table. + /// + /// Based on this utility do not use the same work-day config and algorithm. + /// + public interface ISplitTimes + { + /// Sets the time slot configuration. + /// The configuration. + void SetTimeSlotConfig(TimeSlotConfig config); + + /// Splits the worked time in factors. + /// The start. + /// The end. + /// + /// + /// start - End Time '{end:O}' must be greather than Start Time '{start:O}' + /// or + /// end - End must be on same Date of Start + /// + /// + /// WorkWeekConfiguration - WorkWeekConfiguration Config mandatory + /// or + /// WorkWeekConfiguration - TimeSlotConfig Config mandatory + /// or + /// WorkWeekConfiguration - TimeSlotConfig Config not found for '{start.DayOfWeek}' + /// + WorkedTimeSliceResult SplitWorkedTimeInFactors(DateTime start, DateTime end); } } diff --git a/PH.WorkingDaysAndTime/PH.WorkingDaysAndTimeUtility/PH.WorkingDaysAndTimeUtility.csproj b/PH.WorkingDaysAndTime/PH.WorkingDaysAndTimeUtility/PH.WorkingDaysAndTimeUtility.csproj index 143a5b1..98e57fc 100644 --- a/PH.WorkingDaysAndTime/PH.WorkingDaysAndTimeUtility/PH.WorkingDaysAndTimeUtility.csproj +++ b/PH.WorkingDaysAndTime/PH.WorkingDaysAndTimeUtility/PH.WorkingDaysAndTimeUtility.csproj @@ -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. - 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 work-days,DateTime,work-hours,work-minutes,work-DateTime,holiday,timespan, time-slices - 2.0.7 + 2.0.8 Paolo Innocenti Copyright PH 2021 (c) paonath@gmail.com. All rights reserved. https://github.com/paonath/PH.WorkingDaysAndTime diff --git a/PH.WorkingDaysAndTime/PH.WorkingDaysAndTimeUtility/WorkingDaysAndTimeUtility.cs b/PH.WorkingDaysAndTime/PH.WorkingDaysAndTimeUtility/WorkingDaysAndTimeUtility.cs index 47a44e6..0e9b3cf 100644 --- a/PH.WorkingDaysAndTime/PH.WorkingDaysAndTimeUtility/WorkingDaysAndTimeUtility.cs +++ b/PH.WorkingDaysAndTime/PH.WorkingDaysAndTimeUtility/WorkingDaysAndTimeUtility.cs @@ -7,7 +7,12 @@ namespace PH.WorkingDaysAndTimeUtility { - public class WorkingDaysAndTimeUtility : IWorkingDaysAndTimeUtility + /// + /// + /// + /// + /// + public class WorkingDaysAndTimeUtility : IWorkingDaysAndTimeUtility, ISplitTimes { public TimeSlotConfig TimeSlotConfig; @@ -358,6 +363,21 @@ public bool IsAWorkDay(DateTime day) return true; } + /// + /// Determines whether if given is holy day(do not check for Hours/Minutes, jut Day).. + /// + /// The day. + /// + /// true if given is holy day; otherwise, false. + /// + public bool IsHolyDay(DateTime day) + { + var holyDays = this.CalculateDaysForExclusions(day.Year); + return holyDays.Any(x => x.Year == day.Year && x.Month == day.Month && x.Day == day.Day); + + + } + public static bool TryGetFromConfig(WorkingDaysConfig cfg, out WorkingDaysAndTimeUtility u) { @@ -420,6 +440,46 @@ private List BuildDateTimeSlot(List slots, DateTime star .ThenBy(x => x.DateTimeEnd).ToList(); } + + private bool IfWorkingDateTimeForSplit(DateTime d) + { + var b = WorkWeekConfiguration.WorkDays.ContainsKey(d.DayOfWeek); + if (!b) + { + return false; + } + + var workDaySpan = WorkWeekConfiguration.WorkDays[d.DayOfWeek]; + bool final = false; + foreach (var workTimeSpan in workDaySpan.TimeSpans) + { + if (workTimeSpan.IsStrictWorkInstant(d.Hour, d.Minute,d.Second)) + { + final = true; + break; + + } + } + + return final; + } + + /// Splits the worked time in factors. + /// The start. + /// The end. + /// + /// + /// start - End Time '{end:O}' must be greather than Start Time '{start:O}' + /// or + /// end - End must be on same Date of Start + /// + /// + /// WorkWeekConfiguration - WorkWeekConfiguration Config mandatory + /// or + /// WorkWeekConfiguration - TimeSlotConfig Config mandatory + /// or + /// WorkWeekConfiguration - TimeSlotConfig Config not found for '{start.DayOfWeek}' + /// public WorkedTimeSliceResult SplitWorkedTimeInFactors(DateTime start, DateTime end) { if (start >= end) @@ -460,10 +520,10 @@ public WorkedTimeSliceResult SplitWorkedTimeInFactors(DateTime start, DateTime e List slotsi = TimeSlotConfig.TimesDictionary[start.DayOfWeek]; - - bool isAWorkDay = this.IsAWorkDay(start); - if (!isAWorkDay && null == TimeSlotConfig.HolyDaySlots && TimeSlotConfig.HolyDaySlots.Count > 0) + bool isAWorkDay = !this.IsHolyDay(start.Date); + + if (!isAWorkDay && null != TimeSlotConfig.HolyDaySlots && TimeSlotConfig.HolyDaySlots.Any()) { slotsi = TimeSlotConfig.HolyDaySlots; } @@ -494,7 +554,7 @@ public WorkedTimeSliceResult SplitWorkedTimeInFactors(DateTime start, DateTime e bool ifWorkTime = false; if (isAWorkDay) { - ifWorkTime = this.IfWorkingMoment(s, out var endNotUset0, out var endNotUset1); + ifWorkTime = IfWorkingDateTimeForSplit(s); } InternalElapsedFactor ifa = new InternalElapsedFactor() {Begin = s, SecondsAmount = 1, IfWorkingTime = ifWorkTime}; secsPerSlots.Add(timeSlot, ifa); @@ -535,14 +595,13 @@ public WorkedTimeSliceResult SplitWorkedTimeInFactors(DateTime start, DateTime e foreach (var keyValuePair in secsPerSlots.OrderBy(x => x.Value.Begin)) { - - double f = isAWorkDay ? keyValuePair.Key.Factor : keyValuePair.Key.HolyDayFactor; + sliceResults.Add(new WorkeTimeSlice() { Start = keyValuePair.Value.Begin, Duration = TimeSpan.FromSeconds(keyValuePair.Value.SecondsAmount), - Factor = f, + Factor = keyValuePair.Key.Factor, OnHolyDay = !isAWorkDay, OnWorkTime = keyValuePair.Value.IfWorkingTime, TimeSlot = keyValuePair.Key