Skip to content

Commit 0e1d4e1

Browse files
authored
Merge pull request #25 from shahabfar/Development
Update Persian calendar tests and refactor conversion logic
2 parents e1c0c4c + c72feca commit 0e1d4e1

File tree

5 files changed

+170
-147
lines changed

5 files changed

+170
-147
lines changed

src/Persia.Net.Test/PersianDateOnlyTests.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,20 @@
33
public class PersianDateOnlyTests
44
{
55
[Theory]
6-
[InlineData(2025, 03, 21, 1, 1, 1404)]
7-
[InlineData(2025, 03, 20, 30, 12, 1403)]
8-
[InlineData(2025, 03, 19, 29, 12, 1403)]
96
[InlineData(1977, 02, 08, 19, 11, 1355)]
7+
[InlineData(1980, 07, 20, 29, 4, 1359)]
8+
[InlineData(1995, 12, 31, 10, 10, 1374)]
109
[InlineData(2000, 01, 01, 11, 10, 1378)]
1110
[InlineData(2010, 05, 15, 25, 2, 1389)]
12-
[InlineData(1995, 12, 31, 10, 10, 1374)]
13-
[InlineData(1980, 07, 20, 29, 4, 1359)]
1411
[InlineData(2022, 09, 23, 1, 7, 1401)]
12+
[InlineData(2025, 03, 19, 29, 12, 1403)]
13+
[InlineData(2025, 03, 20, 30, 12, 1403)]
14+
[InlineData(2025, 03, 21, 1, 1, 1404)]
15+
[InlineData(2028, 03, 20, 1, 1, 1407)] // New entry
16+
[InlineData(2029, 03, 19, 29, 12, 1407)] // New entry
17+
[InlineData(2029, 03, 20, 1, 1, 1408)] // New entry
18+
[InlineData(2030, 03, 20, 30, 12, 1408)] // New entry
19+
[InlineData(2034, 03, 20, 30, 12, 1412)] // New entry
1520
public void Test_ConvertToPersianDate_ReturnCorrectDate(int year, int month, int day, int expectedDay, int expectedMonth, int expectedYear)
1621
{
1722
// Arrange
@@ -32,3 +37,4 @@ public void Test_ConvertToPersianDate_ReturnCorrectDate(int year, int month, int
3237
Assert.Equal(expectedYear, convertedNonNullableDate.Year);
3338
}
3439
}
40+

src/Persia.Net.Test/PersianDateTimeTests.cs

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -114,23 +114,46 @@ public void Test_ConvertToPersianDateToLongPersianString_ReturnCorrectString()
114114
Assert.Equal("جمعه ۳ فروردین ۱۴۰۳", convertedDateString);
115115
}
116116

117-
[Fact]
118-
public void Test_ConvertToDateTime_ReturnCorrectDate()
117+
//[Fact]
118+
//public void Test_ConvertToDateTime_ReturnCorrectDate()
119+
//{
120+
// // Arrange
121+
// const int persianYear = 1355;
122+
// const int persianMonth = 11;
123+
// const int persianDay = 19;
124+
125+
// // Act
126+
// var date = PersianDateTime.ToDateTime(persianYear, persianMonth, persianDay).Date;
127+
// var dateOnly = PersianDateTime.ToDateOnly(persianYear, persianMonth, persianDay);
128+
129+
// // Assert
130+
// Assert.Equal(new DateTime(2025, 03, 26).Date, date);
131+
// Assert.Equal(new DateOnly(2025, 03, 26), dateOnly);
132+
//}
133+
134+
[Theory]
135+
[InlineData(1355, 11, 19, 08, 02, 1977)]
136+
[InlineData(1404, 1, 1, 21, 03, 2025)]
137+
[InlineData(1403, 12, 30, 20, 03, 2025)]
138+
[InlineData(1407, 01, 01, 20, 03, 2028)]
139+
[InlineData(1407, 12, 29, 19, 03, 2029)]
140+
[InlineData(1408, 01, 01, 20, 03, 2029)]
141+
[InlineData(1408, 12, 30, 20, 03, 2030)]
142+
[InlineData(1412, 12, 30, 20, 03, 2034)]
143+
public void Test_ConvertToDateTime_ReturnCorrectDate(int year, int month, int day, int expectedDay, int expectedMonth, int expectedYear)
119144
{
120145
// Arrange
121-
const int persianYear = 1403;
122-
const int persianMonth = 01;
123-
const int persianDay = 01;
146+
//DateOnly? nullableDate = new DateOnly(year, month, day);
147+
//DateOnly nonNullableDate = new DateOnly(year, month, day);
124148

125149
// Act
126-
var date = PersianDateTime.ToDateTime(persianYear, persianMonth, persianDay).Date;
127-
var dateOnly = PersianDateTime.ToDateOnly(persianYear, persianMonth, persianDay);
150+
var date = PersianDateTime.ToDateTime(year, month, day).Date;
151+
var dateOnly = PersianDateTime.ToDateOnly(year, month, day);
128152

129153
// Assert
130-
Assert.Equal(new DateTime(2024, 03, 20).Date, date);
131-
Assert.Equal(new DateOnly(2024, 03, 20), dateOnly);
154+
Assert.Equal(new DateTime(expectedYear, expectedMonth, expectedDay).Date, date);
155+
Assert.Equal(new DateOnly(expectedYear, expectedMonth, expectedDay), dateOnly);
132156
}
133-
134157
[Fact]
135158
public void Test_IsGivenPersianYearIsLeap_ReturnCorrectDate()
136159
{

src/Persia.Net/Constants/CalendarConstants.cs

Lines changed: 18 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@
22

33
internal class CalendarConstants
44
{
5-
/// <summary>
6-
/// The year of the start of the Common Era (Start of migration of Islam prophet), which is 622.
7-
/// </summary>
8-
internal const int CommonEra = 622;
9-
105
/// <summary>
116
/// The Julian Day number of the start of the Gregorian calendar, which is October 15, 1582.
127
/// </summary>
@@ -18,14 +13,9 @@ internal class CalendarConstants
1813
internal const double PersianEpoch = 1948320.5;
1914

2015
/// <summary>
21-
/// The Julian Day number of the start of the Persian year 475, which is March 20, 1096.
22-
/// </summary>
23-
internal const double BasePersianEpoch = 2121445.5;
24-
25-
/// <summary>
26-
/// Base year reference for the Persian calendar.
16+
/// Pivot value for the Gregorian reform adjustment (used in alpha calculation).
2717
/// </summary>
28-
internal const int BasePersianYear = 475;
18+
internal const double GregorianReformPivot = 1867216.25;
2919

3020
/// <summary>
3121
/// The Julian Day number of the start of the Islamic calendar, which is July 16, 622.
@@ -40,7 +30,7 @@ internal class CalendarConstants
4030
/// <summary>
4131
/// The number of days in a non-leap year.
4232
/// </summary>
43-
internal const double DaysInYear = 365;
33+
internal const int DaysInYear = 365;
4434

4535
/// <summary>
4636
/// The number of years in the Gregorian calendar's leap year cycle.
@@ -67,11 +57,6 @@ internal class CalendarConstants
6757
/// </summary>
6858
internal const double Century = 100;
6959

70-
/// <summary>
71-
/// The number of days in a Persian calendar cycle.
72-
/// </summary>
73-
internal const double CycleDays = 1029983;
74-
7560
/// <summary>
7661
/// The number of days at the end of a Persian calendar cycle.
7762
/// </summary>
@@ -102,20 +87,10 @@ internal class CalendarConstants
10287
/// </summary>
10388
internal const double CycleFactor = 1028522;
10489

105-
/// <summary>
106-
/// An adjustment made to the epoch in Persian calendar calculations.
107-
/// </summary>
108-
internal const double EpochAdjustment = 474;
109-
11090
/// <summary>
11191
/// The number of days in the first half of a Persian year.
11292
/// </summary>
113-
internal const double DaysInHalfYear = 186;
114-
115-
/// <summary>
116-
/// The year before the epoch in the Persian calendar.
117-
/// </summary>
118-
internal const int YearBeforeEpoch = 474;
93+
internal const int DaysInHalfYear = 186;
11994

12095
/// <summary>
12196
/// The number of months in the first half of a Persian year.
@@ -127,26 +102,11 @@ internal class CalendarConstants
127102
/// </summary>
128103
internal const int DaysInFirstHalfMonth = 31;
129104

130-
/// <summary>
131-
/// A factor used in Persian calendar leap year calculations.
132-
/// </summary>
133-
internal const int LeapYearFactor = 682;
134-
135-
/// <summary>
136-
/// A value subtracted in Persian calendar leap year calculations.
137-
/// </summary>
138-
internal const int LeapYearSubtractor = 110;
139-
140105
/// <summary>
141106
/// The number of days in the second half of a Persian month.
142107
/// </summary>
143108
internal const int DaysInSecondHalfMonth = 30;
144109

145-
/// <summary>
146-
/// The difference in days between the first half and second half of a Persian month.
147-
/// </summary>
148-
internal const int FirstHalfToSecondHalfDayDifference = 6;
149-
150110
/// <summary>
151111
/// Represents the offset used to adjust the Julian day number to start from the middle of the day.
152112
/// </summary>
@@ -198,37 +158,37 @@ internal class CalendarConstants
198158
internal const int IslamicLeapYearMultiplier = 11;
199159

200160
/// <summary>
201-
/// The total number of days in a 400-year cycle of the Gregorian calendar.
161+
/// Average number of days in a Gregorian “century” (100 × 365.2425).
202162
/// </summary>
203-
internal const int DaysIn400Years = 146097;
163+
internal const double DaysPerGregorianCentury = 36524.25;
204164

205165
/// <summary>
206-
/// The total number of days in a century (100 years) of the Gregorian calendar.
166+
/// Offset to align the intermediate count so that March = month 1 in the algorithm.
207167
/// </summary>
208-
internal const int DaysInCentury = 36524;
168+
internal const int JulianConversionBaseOffset = 1524;
209169

210170
/// <summary>
211-
/// The total number of days in a 4-year cycle of the Gregorian calendar, including one leap year.
171+
/// Companion offset for extracting the year from the day count.
212172
/// </summary>
213-
internal const int DaysIn4YearCycle = 1461;
173+
internal const double JulianYearOffset = 122.1;
214174

215175
/// <summary>
216-
/// The offset used in the calculation of the month from a Julian day number.
176+
/// Average length of a Julian year, used when breaking out the year from days.
217177
/// </summary>
218-
internal const int MonthCalculationOffset = 373;
178+
internal const double JulianYearDays = 365.25;
219179

220180
/// <summary>
221-
/// The length of a complete grand cycle in the Persian calendar, consisting of 2820 years.
181+
/// Factor for converting the remaining days into a month index.
222182
/// </summary>
223-
internal const int PersianCalendarGrandCycle = 2820;
183+
internal const double MonthExtractionFactor = 30.6001;
224184

225185
/// <summary>
226-
/// The number of years in the smaller cycles within the 2820-year grand cycle of the Persian calendar.
186+
/// Year offset for dates in March–December when computing the final year.
227187
/// </summary>
228-
internal const int YearsInSmallCycle = 120;
188+
internal const int YearOffsetAfterFebruary = 4716;
229189

230190
/// <summary>
231-
/// The index of the last year in the smaller 120-year cycle of the Persian calendar.
191+
/// Year offset for dates in January–February when computing the final year.
232192
/// </summary>
233-
internal const int LastYearInSmallCycleIndex = 119;
193+
internal const int YearOffsetBeforeMarch = 4715;
234194
}

0 commit comments

Comments
 (0)