DateTimeHelperTest.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using System;
  2. using Masuit.Tools.DateTimeExt;
  3. using Masuit.Tools.Models;
  4. using Xunit;
  5. namespace Masuit.Tools.Abstractions.Test.DateTimeExt;
  6. public class DateTimeHelperTest
  7. {
  8. [Fact]
  9. public static void GetWeekAmount()
  10. {
  11. Assert.Equal(new DateTime(2024, 12, 1).GetWeekAmount(), 53);
  12. }
  13. [Fact]
  14. public static void WeekOfYear()
  15. {
  16. Assert.Equal(new DateTime(2024, 12, 1).WeekOfYear(), 49);
  17. Assert.Equal(new DateTime(2024, 12, 1).WeekOfYear(DayOfWeek.Friday), 49);
  18. }
  19. [Fact]
  20. public static void GetWeekTime()
  21. {
  22. Assert.Equal(new DateTime(2024, 12, 1).GetWeekTime(20).ToString(), new DateTimeRange(new DateTime(2024, 5, 13), new DateTime(2024, 5, 19, 23, 59, 59)).ToString());
  23. }
  24. [Fact]
  25. public static void GetCurrentWeek()
  26. {
  27. Assert.Equal(new DateTime(2024, 12, 1).GetCurrentWeek().ToString(), new DateTimeRange(new DateTime(2024, 11, 25), new DateTime(2024, 12, 1, 23, 59, 59)).ToString());
  28. }
  29. [Fact]
  30. public static void GetCurrentMonth()
  31. {
  32. Assert.Equal(new DateTime(2024, 12, 1).GetCurrentMonth().ToString(), new DateTimeRange(new DateTime(2024, 12, 1), new DateTime(2024, 12, 31, 23, 59, 59)).ToString());
  33. }
  34. [Fact]
  35. public static void GetCurrentLunarMonth()
  36. {
  37. Assert.Equal(new DateTime(2024, 12, 1).GetCurrentLunarMonth().ToString(), new DateTimeRange(new DateTime(2024, 12, 1), new DateTime(2024, 12, 30, 23, 59, 59)).ToString());
  38. }
  39. [Fact]
  40. public static void GetCurrentYear()
  41. {
  42. Assert.Equal(new DateTime(2024, 12, 1).GetCurrentYear().ToString(), new DateTimeRange(new DateTime(2024, 1, 1), new DateTime(2024, 12, 31, 23, 59, 59)).ToString());
  43. }
  44. [Fact]
  45. public static void GetCurrentLunarYear()
  46. {
  47. Assert.Equal(new DateTime(2024, 12, 1).GetCurrentLunarYear().ToString(), new DateTimeRange(new DateTime(2024, 2, 10), new DateTime(2025, 1, 28, 23, 59, 59)).ToString());
  48. }
  49. [Fact]
  50. public static void GetCurrentQuarter()
  51. {
  52. Assert.Equal(new DateTime(2024, 12, 1).GetCurrentQuarter().ToString(), new DateTimeRange(new DateTime(2024, 10, 1), new DateTime(2024, 12, 31, 23, 59, 59)).ToString());
  53. }
  54. [Fact]
  55. public static void GetCurrentLunarQuarter()
  56. {
  57. Assert.Equal(new DateTime(2024, 12, 1).GetCurrentLunarQuarter().ToString(), new DateTimeRange(new DateTime(2024, 11, 1), new DateTime(2025, 1, 28, 23, 59, 59)).ToString());
  58. }
  59. [Fact]
  60. public static void GetCurrentSolar()
  61. {
  62. Assert.Equal(new DateTime(2024, 12, 1).GetCurrentSolar().ToString(), new DateTimeRange(new DateTime(2024, 11, 7), new DateTime(2025, 2, 2, 23, 59, 59)).ToString());
  63. }
  64. [Fact]
  65. public static void GetCurrentRange()
  66. {
  67. Assert.Equal(new DateTime(2024, 12, 1).GetCurrentRange(DateRangeType.LunarYear).ToString(), new DateTimeRange(new DateTime(2024, 2, 10), new DateTime(2025, 1, 28, 23, 59, 59)).ToString());
  68. }
  69. [Fact]
  70. public static void GetTotalSeconds()
  71. {
  72. Assert.Equal(new DateTime(2024, 12, 1).GetTotalSeconds(), 1732982400);
  73. }
  74. [Fact]
  75. public static void GetTotalMilliseconds()
  76. {
  77. Assert.Equal(new DateTime(2024, 12, 1).GetTotalMilliseconds(), 1732982400000);
  78. }
  79. [Fact]
  80. public static void GetTotalMicroseconds()
  81. {
  82. Assert.Equal(new DateTime(2024, 12, 1).GetTotalMicroseconds(), 1732982400000000);
  83. }
  84. [Fact]
  85. public static void GetTotalNanoseconds()
  86. {
  87. Assert.True(new DateTime(2024, 12, 1).GetTotalNanoseconds() >= 1732982400000000000);
  88. }
  89. [Fact]
  90. public static void GetTotalMinutes()
  91. {
  92. Assert.Equal(new DateTime(2024, 12, 1).GetTotalMinutes(), 28883040);
  93. }
  94. [Fact]
  95. public static void GetDaysOfYear()
  96. {
  97. Assert.Equal(new DateTime(2024, 12, 1).GetDaysOfYear(), 366);
  98. }
  99. [Fact]
  100. public static void GetDaysOfMonth()
  101. {
  102. Assert.Equal(new DateTime(2024, 12, 1).GetDaysOfMonth(), 31);
  103. }
  104. [Fact]
  105. public static void InRange()
  106. {
  107. Assert.True(new DateTime(2024, 12, 1).In(new DateTime(2024, 12, 1), new DateTime(2024, 12, 31)));
  108. }
  109. [Fact]
  110. public static void GetMonthLastDate()
  111. {
  112. Assert.Equal(new DateTime(2024, 12, 1).GetMonthLastDate(), 31);
  113. }
  114. [Fact]
  115. public static void GetUnionSet()
  116. {
  117. Assert.Equal(new DateTimeRange(new DateTime(2024, 12, 10), new DateTime(2024, 12, 20)).GetMaxTimePeriod([new DateTimeRange(new DateTime(2024, 12, 15), new DateTime(2024, 12, 30))]), new DateTimeRange(new DateTime(2024, 12, 15), new DateTime(2024, 12, 30)));
  118. }
  119. }