TimeConversionsHelper.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. namespace AipGateway.API.Domain.Common.Extensions
  2. {
  3. public static class TimeConversionsHelper
  4. {
  5. public static long GetDifferenceInMonths(DateTime StartDate, DateTime EndDate)
  6. {
  7. var days = from day in StartDate.DaysInRangeUntil(EndDate)
  8. let start = Max(day.AddHours(7), StartDate)
  9. let end = Min(day.AddHours(19), EndDate)
  10. select (end - start).TotalDays;
  11. return (long)Math.Round(days.Sum() / 30);
  12. }
  13. public static IEnumerable<DateTime> DaysInRangeUntil(this DateTime start, DateTime end)
  14. {
  15. return Enumerable.Range(0, 1 + (int)(end.Date - start.Date).TotalDays)
  16. .Select(dt => start.Date.AddDays(dt));
  17. }
  18. public static bool IsWeekendDay(this DateTime dt)
  19. {
  20. return dt.DayOfWeek == DayOfWeek.Saturday
  21. || dt.DayOfWeek == DayOfWeek.Sunday;
  22. }
  23. public static DateTime Max(DateTime start, DateTime end)
  24. {
  25. return new DateTime(Math.Max(start.Ticks, end.Ticks));
  26. }
  27. public static DateTime Min(DateTime start, DateTime end)
  28. {
  29. return new DateTime(Math.Min(start.Ticks, end.Ticks));
  30. }
  31. public static int ToSeconds(this int value, TimeType valueType)
  32. {
  33. switch (valueType)
  34. {
  35. case TimeType.Minutes:
  36. return value * 60;
  37. case TimeType.Hours:
  38. return value * 60 * 60;
  39. case TimeType.Days:
  40. return value * 24 * 60 * 60;
  41. default:
  42. return value;
  43. }
  44. }
  45. public static int ToMinutes(this int value, TimeType valueType)
  46. {
  47. switch (valueType)
  48. {
  49. case TimeType.Seconds:
  50. return value / 60;
  51. case TimeType.Hours:
  52. return value * 60;
  53. case TimeType.Days:
  54. return value * 24 * 60;
  55. default:
  56. return value;
  57. }
  58. }
  59. public static int ToDays(this int value, TimeType valueType)
  60. {
  61. switch (valueType)
  62. {
  63. case TimeType.Seconds:
  64. return value / 60 / 60 / 24;
  65. case TimeType.Minutes:
  66. return value / 60 / 24;
  67. case TimeType.Hours:
  68. return value / 24;
  69. default:
  70. return value;
  71. }
  72. }
  73. public static int ToHours(this int value, TimeType valueType)
  74. {
  75. switch (valueType)
  76. {
  77. case TimeType.Seconds:
  78. return value / 60 / 60;
  79. case TimeType.Minutes:
  80. return value / 60;
  81. case TimeType.Days:
  82. return value * 24;
  83. default:
  84. return value;
  85. }
  86. }
  87. public static long ToMinutes(this long value, TimeType valueType)
  88. {
  89. switch (valueType)
  90. {
  91. case TimeType.Seconds:
  92. return value / 60;
  93. case TimeType.Hours:
  94. return value * 60;
  95. case TimeType.Days:
  96. return value * 24 * 60;
  97. default:
  98. return value;
  99. }
  100. }
  101. public static long ToDays(this long value, TimeType valueType)
  102. {
  103. switch (valueType)
  104. {
  105. case TimeType.Seconds:
  106. return value / 60 / 60 / 24;
  107. case TimeType.Minutes:
  108. return value / 60 / 24;
  109. case TimeType.Hours:
  110. return value / 24;
  111. default:
  112. return value;
  113. }
  114. }
  115. public static long ToHours(this long value, TimeType valueType)
  116. {
  117. switch (valueType)
  118. {
  119. case TimeType.Seconds:
  120. return value / 60 / 60;
  121. case TimeType.Minutes:
  122. return value / 60;
  123. case TimeType.Days:
  124. return value * 24;
  125. default:
  126. return value;
  127. }
  128. }
  129. }
  130. public enum TimeType
  131. {
  132. Days = 1,
  133. Hours = 2,
  134. Minutes = 3,
  135. Seconds = 4,
  136. }
  137. }