Skip to content

Commit 38ee2df

Browse files
committed
ADD day of year
1 parent bfa3c78 commit 38ee2df

File tree

6 files changed

+66
-0
lines changed

6 files changed

+66
-0
lines changed

lib/src/date.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ abstract class Date implements Comparable<Date> {
5252
/// Milliseconds [0..999]
5353
int get millisecond;
5454

55+
/// day of year.
56+
///
57+
/// calculated, not stored.
58+
int get dayOfYear;
59+
5560
/// Returns the time part as [Duration].
5661
Duration get time {
5762
return Duration(

lib/src/gregorian/gregorian_calculation.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,4 +172,17 @@ class _Algo {
172172
millisecond,
173173
);
174174
}
175+
176+
/// get day of year
177+
static int getDayOfYear(bool isLeap, int month, int day) {
178+
// day accumulation in regular years:
179+
const r = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365];
180+
// day accumulation in leap years:
181+
const l = [0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366];
182+
if (isLeap) {
183+
return l[month - 1] + day;
184+
} else {
185+
return r[month - 1] + day;
186+
}
187+
}
175188
}

lib/src/gregorian/gregorian_date.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ class Gregorian extends Date {
7575
@override
7676
final int millisecond;
7777

78+
@override
79+
int get dayOfYear {
80+
return _Algo.getDayOfYear(isLeapYear(), month, day);
81+
}
82+
7883
/// Week day number
7984
/// [monday] = 1
8085
/// [sunday] = 7

lib/src/jalali/jalali_calculation.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,16 @@ class _Algo {
268268
isLeap,
269269
);
270270
}
271+
272+
/// get day of year
273+
static int getDayOfYear(bool isLeap, int month, int day) {
274+
// day accumulation in regular years:
275+
const r = [0, 31, 62, 93, 124, 155, 186, 216, 246, 276, 306, 336, 365];
276+
// day accumulation in leap years:
277+
// no need, since the last day is the leap day.
278+
// const l = [0, 31, 62, 93, 124, 155, 186, 216, 246, 276, 306, 336, 366];
279+
return r[month - 1] + day;
280+
}
271281
}
272282

273283
/// Internal used for Jalali calculation

lib/src/jalali/jalali_date.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ class Jalali extends Date {
7676
@override
7777
final int millisecond;
7878

79+
@override
80+
int get dayOfYear {
81+
return _Algo.getDayOfYear(isLeapYear(), month, day);
82+
}
83+
7984
/// Whether is leap year.
8085
///
8186
/// It is cached since we always calculate it

test/shamsi_date_test.dart

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1696,6 +1696,34 @@ void main() {
16961696
expect(gdt.millisecond, 219);
16971697
});
16981698

1699+
test('Jalali.dayOfYear', () {
1700+
expect(Jalali(1404).isLeapYear(), false);
1701+
expect(Jalali(1404, 1, 1).dayOfYear, 1);
1702+
expect(Jalali(1404, 12, 29).dayOfYear, 365);
1703+
1704+
expect(Jalali(1403).isLeapYear(), true);
1705+
expect(Jalali(1403, 1, 1).dayOfYear, 1);
1706+
expect(Jalali(1403, 12, 30).dayOfYear, 366);
1707+
1708+
expect(Jalali(1404, 2, 2).dayOfYear, 33);
1709+
expect(Jalali(1404, 4, 16).dayOfYear, 109);
1710+
expect(Jalali(1404, 12, 25).dayOfYear, 361);
1711+
});
1712+
1713+
test('Gregorian.dayOfYear', () {
1714+
expect(Gregorian(2025).isLeapYear(), false);
1715+
expect(Gregorian(2025, 1, 1).dayOfYear, 1);
1716+
expect(Gregorian(2025, 12, 31).dayOfYear, 365);
1717+
1718+
expect(Gregorian(2028).isLeapYear(), true);
1719+
expect(Gregorian(2028, 1, 1).dayOfYear, 1);
1720+
expect(Gregorian(2028, 12, 31).dayOfYear, 366);
1721+
1722+
expect(Gregorian(2025, 10, 31).dayOfYear, 304);
1723+
expect(Gregorian(2025, 4, 22).dayOfYear, 112);
1724+
expect(Gregorian(2028, 4, 22).dayOfYear, 113);
1725+
});
1726+
16991727
test('Iranian Calendar Authority Test Cases', () {
17001728
// Persian leap year data provided by the Iranian calendar authority at:
17011729
// https://calendar.ut.ac.ir/Fa/News/Data/Doc/KabiseShamsi1206-1498-new.pdf

0 commit comments

Comments
 (0)