Skip to content

Commit

Permalink
feat: ✨ add getDayOfYear functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
thkruz committed Aug 24, 2022
1 parent cbfd7e4 commit 46ffa20
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,32 @@ class Utils {
);
};

public static getDayOfYear(date: Date): number {
date = date || new Date();

const dayCount = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334];
const mn = date.getMonth();
const dn = date.getUTCDate();
let dayOfYear = dayCount[mn] + dn;

if (mn > 1 && Utils.isLeapYear(date)) {
dayOfYear++;
}

return dayOfYear;
}

private static isLeapYear(dateIn: Date) {
const year = dateIn.getUTCFullYear();

// eslint-disable-next-line no-bitwise
if ((year & 3) !== 0) {
return false;
}

return year % 100 !== 0 || year % 400 === 0;
}

public static roundToNDecimalPlaces(value: number, places: number): number {
return Math.round(value * 10 ** places) / 10 ** places;
}
Expand Down
32 changes: 32 additions & 0 deletions test/utils/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,35 @@ describe('Create Vector Function', () => {
expect(Utils.createVec(1, 10, 2)).toEqual([1, 3, 5, 7, 9]);
});
});

describe('doy Functions', () => {
test('if doy is correct', () => {
expect(Utils.getDayOfYear(new Date(2022, 0, 1))).toEqual(1);
expect(Utils.getDayOfYear(new Date(2022, 1, 1))).toEqual(32);
expect(Utils.getDayOfYear(new Date(2022, 2, 1))).toEqual(60);
expect(Utils.getDayOfYear(new Date(2022, 3, 1))).toEqual(91);
expect(Utils.getDayOfYear(new Date(2022, 4, 1))).toEqual(121);
expect(Utils.getDayOfYear(new Date(2022, 5, 1))).toEqual(152);
expect(Utils.getDayOfYear(new Date(2022, 6, 1))).toEqual(182);
expect(Utils.getDayOfYear(new Date(2022, 7, 1))).toEqual(213);
expect(Utils.getDayOfYear(new Date(2022, 8, 1))).toEqual(244);
expect(Utils.getDayOfYear(new Date(2022, 9, 1))).toEqual(274);
expect(Utils.getDayOfYear(new Date(2022, 10, 1))).toEqual(305);
expect(Utils.getDayOfYear(new Date(2022, 11, 1))).toEqual(335);
});

test('if getDayOfYear is correct in leap year', () => {
expect(Utils.getDayOfYear(new Date(2020, 0, 1, 0))).toEqual(1);
expect(Utils.getDayOfYear(new Date(2020, 1, 1, 0))).toEqual(32);
expect(Utils.getDayOfYear(new Date(2020, 2, 1, 0))).toEqual(61);
expect(Utils.getDayOfYear(new Date(2020, 3, 1, 0))).toEqual(92);
expect(Utils.getDayOfYear(new Date(2020, 4, 1, 0))).toEqual(122);
expect(Utils.getDayOfYear(new Date(2020, 5, 1, 0))).toEqual(153);
expect(Utils.getDayOfYear(new Date(2020, 6, 1, 0))).toEqual(183);
expect(Utils.getDayOfYear(new Date(2020, 7, 1, 0))).toEqual(214);
expect(Utils.getDayOfYear(new Date(2020, 8, 1, 0))).toEqual(245);
expect(Utils.getDayOfYear(new Date(2020, 9, 1, 0))).toEqual(275);
expect(Utils.getDayOfYear(new Date(2020, 10, 1, 0))).toEqual(306);
expect(Utils.getDayOfYear(new Date(2020, 11, 1, 0))).toEqual(336);
});
});

0 comments on commit 46ffa20

Please sign in to comment.