我正在编写一种方法来帮助我计算任务时间。每项任务都必须在创建后的数小时内完成。
期望的行为:我需要显示完成任务的剩余数量。正数 剩余时间 负数 任务超出指定时间。
这是计算时差的正确实现吗?
到目前为止,我的进展是这样的,我已经创建了几个测试:
/// <summary>
/// Displays number of hours left for resolution as positive and negative number for missed targets.
/// </summary>
/// <param name="createdTime"></param>
/// <param name="closedDateTime"></param>
/// <param name="resolutionTime"></param>
/// <returns>Timespan</returns>
public static TimeSpan GetNumberOfHoursLeftToResolution(DateTime createdTime, DateTime? closedDateTime, long resolutionTime)
{
var now = DateTime.Now;
var diff = new TimeSpan(0);
bool isNegative = false;
var startWithResolutionHours = createdTime.AddTicks(resolutionTime);
// task is closed
if (closedDateTime.HasValue)
{
diff = (startWithResolutionHours - closedDateTime.Value);
isNegative = closedDateTime.Value >= startWithResolutionHours;
}
else
{
// task is still open
diff = (startWithResolutionHours - now );
isNegative = now.AddTicks(resolutionTime) >= startWithResolutionHours;
}
var result = new TimeSpan(diff.Days, diff.Hours, diff.Minutes + ((diff.Seconds > 0) ? 1 : 0), 0);
if (isNegative)
{
return -result;
}
//count of all difference between times
return result.Add(-new TimeSpan(resolutionTime));
}
我写了几个测试
[TestMethod]
public void TaskHasNotBeenResolvedsAndHasAllTimeLeftForResolution()
{
var d = DateTime.Now;
var resolutionTime = new TimeSpan(2, 0, 0).Ticks;
var expectedResult = -new TimeSpan(2, 0, 0);
var actualResult = TimeCalculation.GetNumberOfHoursLeftToResolution(d, null, resolutionTime);
Assert.AreEqual(expectedResult, actualResult);
}
[TestMethod]
public void TaskHasNotBeenResolvedAndHasFrom8Hours6Left()
{
var d = DateTime.Now.AddHours(-2);
var resolutionTime = new TimeSpan(8, 0, 0).Ticks;
var expectedResult = -new TimeSpan(6, 0, 0); // 6 hours
var actualResult = TimeCalculation.GetNumberOfHoursLeftToResolution(d, null, resolutionTime);
Assert.AreEqual(expectedResult, actualResult);
}
[TestMethod]
public void TaskHasNotBeenResolvedAndHas20MinLeft()
{
var d = DateTime.Now.AddHours(-4).AddMinutes(20);
var resolutionTime = new TimeSpan(4, 0, 0).Ticks;
var expectedResult = -new TimeSpan(0, 20, 0); // -20min
var actualResult = TimeCalculation.GetNumberOfHoursLeftToResolution(d, null, resolutionTime);
Assert.AreEqual(expectedResult, actualResult);
}
[TestMethod]
public void TaskHasNotBeenResolvedAndHas1MinLeft()
{
var d = DateTime.Now.AddHours(-4).AddMinutes(1);
var resolutionTime = new TimeSpan(4, 0, 0).Ticks;
var expectedResult = - new TimeSpan(0, 1, 0); // - 1min
var actualResult = TimeCalculation.GetNumberOfHoursLeftToResolution(d, null, resolutionTime);
Assert.AreEqual(expectedResult, actualResult);
}
[TestMethod]
public void TaskHasNotBeenResolvedAndHasNoMinutesLeft()
{
var d = DateTime.Now.AddHours(-4);
var resolutionTime = new TimeSpan(4, 0, 0).Ticks;
var expectedResult = new TimeSpan(0, 0, 0); // 0 min
var actualResult = TimeCalculation.GetNumberOfHoursLeftToResolution(d, null, resolutionTime);
Assert.AreEqual(expectedResult, actualResult);
}
[TestMethod]
public void TaskHasNotBeenResolvedInTimeOneMinOverdue()
{
var d = DateTime.Now.AddMinutes(1);
var resolutionTime = new TimeSpan(4, 0, 0).Ticks;
var expectedResult = new TimeSpan(0, 1, 0); // 1 min
var actualResult = TimeCalculation.GetNumberOfHoursLeftToResolution(d, null, resolutionTime);
Assert.AreEqual(expectedResult, actualResult);
}