Forked from carlosmaniero/ElectricityReadingInterval.cs
Created
October 6, 2021 13:28
-
-
Save daicorrea-tw/bf16eb2f92dd1ccfaa3ebb83c530e8ca to your computer and use it in GitHub Desktop.
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 System; | |
namespace JOIEnergy.Domain | |
{ | |
public class ElectricityReadingInterval | |
{ | |
public readonly DateTime Start; | |
public readonly DateTime End; | |
public ElectricityReadingInterval(DateTime start, DateTime end) | |
{ | |
Start = start; | |
End = end; | |
} | |
public static ElectricityReadingInterval getPreviousWeekInterval() | |
{ | |
var today = DateTime.Now.Date; | |
return getPreviousWeekInterval(today); | |
} | |
public static ElectricityReadingInterval getPreviousWeekInterval(DateTime today) | |
{ | |
DateTime sunday = getSunday(today.Date); | |
DateTime sundayEarly = sunday.AddDays(-7); | |
return new ElectricityReadingInterval(sundayEarly, sunday); | |
} | |
private static DateTime getSunday(DateTime date) | |
{ | |
while (date.DayOfWeek != DayOfWeek.Sunday) | |
{ | |
date = date.AddDays(-1); | |
} | |
return 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 System; | |
using JOIEnergy.Domain; | |
using Xunit; | |
namespace JOIEnergy.Tests | |
{ | |
public class ElectricityReadingIntervalTest | |
{ | |
public ElectricityReadingIntervalTest() | |
{ | |
} | |
[Fact] | |
public void GetLastWeekFromToday() | |
{ | |
Assert.Equal(new DateTime(2021, 9, 26), ElectricityReadingInterval.getPreviousWeekInterval(new DateTime(2021, 10, 5, 12, 00, 00)).Start); | |
Assert.Equal(new DateTime(2021, 10, 3), ElectricityReadingInterval.getPreviousWeekInterval(new DateTime(2021, 10, 5, 12, 00, 00)).End); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment