Advertisement
Guest User

Untitled

a guest
Jul 30th, 2015
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. public static class DateTimeAssert
  2. {
  3. public static void AreEqual(DateTime? expectedDate, DateTime? actualDate, DateTimeDeltaType deltaType, int count)
  4. {
  5. if (expectedDate == null && actualDate == null)
  6. {
  7. return;
  8. }
  9. else if (expectedDate == null)
  10. {
  11. throw new NullReferenceException("The expected date was null");
  12. }
  13. else if (actualDate == null)
  14. {
  15. throw new NullReferenceException("The actual date was null");
  16. }
  17. TimeSpan expectedDelta = GetTimeSpanDeltaByType(deltaType, count);
  18. double totalSecondsDifference = Math.Abs(((DateTime)actualDate - (DateTime)expectedDate).TotalSeconds);
  19.  
  20. if (totalSecondsDifference > expectedDelta.TotalSeconds)
  21. {
  22. throw new Exception(string.Format("Expected Date: {0}, Actual Date: {1} \nExpected Delta: {2}, Actual Delta in seconds- {3} (Delta Type: {4})",
  23. expectedDate,
  24. actualDate,
  25. expectedDelta,
  26. totalSecondsDifference,
  27. deltaType));
  28. }
  29. }
  30.  
  31. private static TimeSpan GetTimeSpanDeltaByType(DateTimeDeltaType type, int count)
  32. {
  33. TimeSpan result = default(TimeSpan);
  34.  
  35. switch (type)
  36. {
  37. case DateTimeDeltaType.Days:
  38. result = new TimeSpan(count, 0, 0, 0);
  39. break;
  40. case DateTimeDeltaType.Minutes:
  41. result = new TimeSpan(0, count, 0);
  42. break;
  43. default: throw new NotImplementedException("The delta type is not implemented.");
  44. }
  45.  
  46. return result;
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement