我正在编写一种方法来帮助我计算任务的时间。 每项任务都必须在创建后的几小时内完成。
所需行为: 我需要显示我们左边的号码才能完成任务。 正数还有剩余时间 负数表示任务超出指定的时间。
这是计算时差的正确实现吗?
到目前为止,我的进展是这样的,我已经创建了几个测试:
/// <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);
}
答案 0 :(得分:0)
我会把它缩短一点
public static TimeSpan GetNumberOfHoursLeftToResolution(DateTime createdTime,
DateTime? closedDateTime, long resolutionTime)
{
var currentPoint = closedDateTime ?? DateTime.Now;
return new TimeSpan(resolutionTime - (currentPoint - createdTime).Ticks);
}