タスクの時間を計算するのに役立つメソッドを書いています。各タスクは、作成から数時間以内に完了する必要があります。
望ましい動作: タスクを完了するために残っている数を表示する必要があります。正の数の残り時間 負の数 タスクが指定された時間をオーバーランしました。
時差を計算するためのこれは正しい実装ですか?
これまでのところ、私の進歩はこれであり、いくつかのテストを作成しました:
/// <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);
}