Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save daicorrea-tw/bf16eb2f92dd1ccfaa3ebb83c530e8ca to your computer and use it in GitHub Desktop.
Save daicorrea-tw/bf16eb2f92dd1ccfaa3ebb83c530e8ca to your computer and use it in GitHub Desktop.
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;
}
}
}
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