Created
April 7, 2023 20:30
-
-
Save ismael3s/2b9f8b349e4f2aa99b2b07b3217e5f0c to your computer and use it in GitHub Desktop.
Calculate Productive Time Between Two Date
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using WorkingTime; | |
| namespace WorkingTimeTest; | |
| public class WorkingTimeServiceUnitTest | |
| { | |
| public static IEnumerable<object[]> WorkingTimeParameters() | |
| { | |
| yield return new object[] { | |
| new WorkingTimeParams( | |
| WorkingHourStartAt: new TimeSpan(9, 0, 0), | |
| WorkingHoursEndAt: new TimeSpan(18, 0, 0), | |
| InitialDate: new DateTime(2023, 4, 3, 5, 0, 0), | |
| EndDate: new DateTime(2023, 4, 3, 22, 0, 0), | |
| Holidays: new List<DateTime>() | |
| ), | |
| 540 | |
| }; | |
| yield return new object[] { | |
| new WorkingTimeParams( | |
| WorkingHourStartAt: new TimeSpan(9, 0, 0), | |
| WorkingHoursEndAt: new TimeSpan(18, 0, 0), | |
| InitialDate: new DateTime(2023, 4, 3, 5, 0, 0), | |
| EndDate: new DateTime(2023, 4, 4, 22, 0, 0), | |
| Holidays: new List<DateTime>() { | |
| new DateTime(2023, 4, 4, 0, 0, 0), | |
| } | |
| ), | |
| 540 | |
| }; | |
| yield return new object[] { | |
| new WorkingTimeParams( | |
| WorkingHourStartAt: new TimeSpan(9, 0, 0), | |
| WorkingHoursEndAt: new TimeSpan(18, 0, 0), | |
| InitialDate: new DateTime(2023, 4, 3, 5, 0, 0), | |
| EndDate: new DateTime(2023, 4, 7, 22, 0, 0), | |
| Holidays: new List<DateTime>() | |
| ), | |
| 2700 | |
| }; | |
| yield return new object[] { | |
| new WorkingTimeParams( | |
| WorkingHourStartAt: new TimeSpan(9, 0, 0), | |
| WorkingHoursEndAt: new TimeSpan(18, 0, 0), | |
| InitialDate: new DateTime(2023, 4, 3, 5, 0, 0), | |
| EndDate: new DateTime(2023, 4, 7, 22, 0, 0), | |
| Holidays: new List<DateTime>() { | |
| new DateTime(2023, 4, 7, 0, 0, 0), | |
| } | |
| ), | |
| 2160 | |
| }; | |
| } | |
| [Theory] | |
| [MemberData(nameof(WorkingTimeParameters))] | |
| public void GivenAnWorkingTimeParams_ShouldReturnTheCorrectTimeSpan(WorkingTimeParams workingTimeParams, int expectedMinutes) | |
| { | |
| var result = WorkingTimeService.CalculateProductiveHours(workingTimeParams); | |
| result.Should().Be(TimeSpan.FromMinutes(expectedMinutes)); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| namespace WorkingTime; | |
| public class WorkingTimeService | |
| { | |
| public static TimeSpan CalculateProductiveHours(WorkingTimeParams workingTimeParams) | |
| { | |
| TimeSpan workingTime = TimeSpan.Zero; | |
| workingTimeParams = workingTimeParams with | |
| { | |
| WorkingHoursEndAt = workingTimeParams.WorkingHoursEndAt.Subtract(TimeSpan.FromMinutes(1)), | |
| }; | |
| for (DateTime current = workingTimeParams.InitialDate; current < workingTimeParams.EndDate; current = current.AddMinutes(1)) | |
| { | |
| TimeSpan currentTime = current.TimeOfDay; // Get the time of day for the current minute | |
| bool isCurrentTimeBetweenWorkingHours = currentTime >= workingTimeParams.WorkingHourStartAt && currentTime <= workingTimeParams.WorkingHoursEndAt; | |
| bool isWeekend = current.DayOfWeek == DayOfWeek.Saturday || current.DayOfWeek == DayOfWeek.Sunday; | |
| bool isHoliday = workingTimeParams.Holidays.Contains(current.Date); | |
| if (isCurrentTimeBetweenWorkingHours && !isWeekend && !isHoliday) | |
| { | |
| workingTime = workingTime.Add(TimeSpan.FromMinutes(1)); // Add one minute to the working time | |
| } | |
| } | |
| return workingTime; | |
| } | |
| } | |
| public record WorkingTimeParams(TimeSpan WorkingHourStartAt, TimeSpan WorkingHoursEndAt, DateTime InitialDate, DateTime EndDate, List<DateTime> Holidays) { } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment