Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(calculation): refactor indian date calculation #40

Merged
merged 2 commits into from
Jun 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/__tests__/getDate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,27 @@ describe('getDate', () => {
);
});
});

const december162022 = new Date('2023-12-16');
describe(`Date: ${december162022}`, () => {
it(`(BD) should return 'শনিবার, ১ পৌষ, ১৪৩০'`, () => {
expect(getDate(december162022)).toEqual('শনিবার, ১ পৌষ, ১৪৩০');
});
it(`(IN) should return 'শনিবার, ২৯ অগ্রহায়ণ, ১৪৩০'`, () => {
expect(getDate(december162022, { calculationMethod: 'IN' })).toEqual(
'শনিবার, ২৯ অগ্রহায়ণ, ১৪৩০'
);
});
});
const june192023 = new Date('2023-06-19');
describe(`Date: ${june192023}`, () => {
it(`(BD) should return 'সোমবার, ৫ আষাঢ়, ১৪৩০'`, () => {
expect(getDate(june192023)).toEqual('সোমবার, ৫ আষাঢ়, ১৪৩০');
});
it(`(IN) should return 'সোমবার, ৩ আষাঢ়, ১৪৩০'`, () => {
expect(getDate(june192023, { calculationMethod: 'IN' })).toEqual(
'সোমবার, ৩ আষাঢ়, ১৪৩০'
);
});
});
});
3 changes: 2 additions & 1 deletion src/__tests__/getDay.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ describe('getDay', () => {
expect(getDay(august17, { calculationMethod: 'IN' })).toBe('৩১');
});
});
const august18 = new Date('August 18 2022 06:22:03');

const august18 = new Date('2022-08-18');
describe(`Date: ${august18}`, () => {
it(`(BD) should convert to '৩'`, () => {
expect(getDay(august18)).toBe('৩');
Expand Down
126 changes: 14 additions & 112 deletions src/__tests__/getMonth.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
import { getMonth } from '../getMonth';

// const may14 = new Date('May 14 2021 06:22:03');
// const may15 = new Date('May 15 2021 06:22:03');
// const may16 = new Date('May 16 2021 06:22:03');

// const june14 = new Date('June 14 2021 06:22:03');
// const june15 = new Date('June 15 2021 06:22:03');
// const june16 = new Date('June 16 2021 06:22:03');

// const july15 = new Date('July 15 2021 06:22:03');
// const july16 = new Date('July 16 2021 06:22:03');
// const july18 = new Date('July 18 2021 06:22:03');

describe('getMonth', () => {
const april13 = new Date('April 13 2021 06:22:03');
describe(`Date: ${april13}`, () => {
Expand Down Expand Up @@ -49,109 +37,23 @@ describe('getMonth', () => {
const invalidDate = new Date('sdfasf');
describe(`Date: ${invalidDate}`, () => {
it(`(BD) should convert to 'Invalid Date'`, () => {
expect(getMonth(invalidDate)).toBe('Invalid Date');
let error: Error | undefined;
try {
getMonth(invalidDate);
} catch (err) {
error = err;
}
expect(error).toEqual(new Error('Invalid Date'));
});

it(`(IN) should convert to 'Invalid Date'`, () => {
expect(getMonth(invalidDate, { calculationMethod: 'IN' })).toBe(
'Invalid Date'
);
let error: Error | undefined;
try {
getMonth(invalidDate, { calculationMethod: 'IN' });
} catch (err) {
error = err;
}
expect(error).toEqual(new Error('Invalid Date'));
});
});
});

// test(`converts ${may14} to 'বৈশাখ'`, () => {
// expect(getMonth(may14)).toBe('বৈশাখ');
// });

// test(`converts ${may14} to 'বৈশাখ'`, () => {
// expect(getMonth(may14, { calculationMethod: 'IN' })).toBe('বৈশাখ');
// });

// test(`converts ${may15} to 'জ্যৈষ্ঠ'`, () => {
// expect(getMonth(may15)).toBe('জ্যৈষ্ঠ');
// });

// test(`converts ${may15} to 'বৈশাখ'`, () => {
// expect(getMonth(may15, { calculationMethod: 'IN' })).toBe('বৈশাখ');
// });

// test(`converts ${may16} to 'জ্যৈষ্ঠ'`, () => {
// expect(getMonth(may16)).toBe('জ্যৈষ্ঠ');
// });

// test(`converts ${may16} to 'জ্যৈষ্ঠ'`, () => {
// expect(getMonth(may16, { calculationMethod: 'IN' })).toBe('জ্যৈষ্ঠ');
// });

// test(`converts ${may14} to 'বৈশাখ'`, () => {
// expect(getMonth(may14)).toBe('বৈশাখ');
// });

// test(`converts ${may14} to 'বৈশাখ'`, () => {
// expect(getMonth(may14, { calculationMethod: 'IN' })).toBe('বৈশাখ');
// });

// test(`converts ${may15} to 'জ্যৈষ্ঠ'`, () => {
// expect(getMonth(may15)).toBe('জ্যৈষ্ঠ');
// });

// test(`converts ${may15} to 'বৈশাখ'`, () => {
// expect(getMonth(may15, { calculationMethod: 'IN' })).toBe('বৈশাখ');
// });

// test(`converts ${may16} to 'জ্যৈষ্ঠ'`, () => {
// expect(getMonth(may16)).toBe('জ্যৈষ্ঠ');
// });

// test(`converts ${may16} to 'জ্যৈষ্ঠ'`, () => {
// expect(getMonth(may16, { calculationMethod: 'IN' })).toBe('জ্যৈষ্ঠ');
// });

// test(`converts ${june14} to 'জ্যৈষ্ঠ'`, () => {
// expect(getMonth(june14)).toBe('জ্যৈষ্ঠ');
// });

// test(`converts ${june14} to 'জ্যৈষ্ঠ'`, () => {
// expect(getMonth(june14, { calculationMethod: 'IN' })).toBe('জ্যৈষ্ঠ');
// });

// test(`converts ${june15} to 'আষাঢ়'`, () => {
// expect(getMonth(june15)).toBe('আষাঢ়');
// });

// test(`converts ${june15} to 'জ্যৈষ্ঠ'`, () => {
// expect(getMonth(june15, { calculationMethod: 'IN' })).toBe('জ্যৈষ্ঠ');
// });

// test(`converts ${june16} to 'আষাঢ়'`, () => {
// expect(getMonth(june16)).toBe('আষাঢ়');
// });

// test(`converts ${june16} to 'আষাঢ়'`, () => {
// expect(getMonth(june16, { calculationMethod: 'IN' })).toBe('আষাঢ়');
// });

// test(`converts ${july15} to 'আষাঢ়'`, () => {
// expect(getMonth(july15)).toBe('আষাঢ়');
// });

// test(`converts ${july15} to 'আষাঢ়'`, () => {
// expect(getMonth(july15, { calculationMethod: 'IN' })).toBe('আষাঢ়');
// });

// test(`converts ${july16} to 'শ্রাবণ'`, () => {
// expect(getMonth(july16)).toBe('শ্রাবণ');
// });

// test(`converts ${july16} to 'আষাঢ়'`, () => {
// expect(getMonth(july16, { calculationMethod: 'IN' })).toBe('আষাঢ়');
// });

// test(`converts ${july18} to 'শ্রাবণ'`, () => {
// expect(getMonth(july18)).toBe('শ্রাবণ');
// });

// test(`converts ${july18} to 'শ্রাবণ'`, () => {
// expect(getMonth(july18, { calculationMethod: 'IN' })).toBe('শ্রাবণ');
// });
44 changes: 40 additions & 4 deletions src/__tests__/getYear.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,49 @@ describe('getYear', () => {
const invalidDate = new Date('xyzdate');
describe(`Date: ${invalidDate}`, () => {
it(`(BD) should convert to 'Invalid Date'`, () => {
expect(getYear(invalidDate, { format: 'YY' })).toBe('Invalid Date');
let error: Error | undefined;
try {
getYear(invalidDate, { format: 'YY' });
} catch (err) {
error = err;
}
expect(error).toEqual(new Error('Invalid Date'));
});

it(`(IN) should convert to 'Invalid Date'`, () => {
expect(
getYear(invalidDate, { format: 'YY', calculationMethod: 'IN' })
).toBe('Invalid Date');
let error: Error | undefined;
try {
getYear(invalidDate, { format: 'YY', calculationMethod: 'IN' });
} catch (err) {
error = err;
}
expect(error).toEqual(new Error('Invalid Date'));
});
});

const beforeBanglaDate = new Date('593-04-13');
describe(`Date: ${beforeBanglaDate}`, () => {
it(`(BD) should convert to 'Invalid Date'`, () => {
let error: Error | undefined;
try {
getYear(beforeBanglaDate, { format: 'YY' });
} catch (err) {
error = err;
}
expect(error).toEqual(new Error('Invalid Date'));
});

it(`(IN) should convert to 'Invalid Date'`, () => {
let error: Error | undefined;
try {
getYear(beforeBanglaDate, {
format: 'YY',
calculationMethod: 'IN',
});
} catch (err) {
error = err;
}
expect(error).toEqual(new Error('Invalid Date'));
});
});
});
7 changes: 6 additions & 1 deletion src/getDate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,14 @@ export function getDate(
calculationMethod: 'BD',
}
) {
if (!isValidDate(date)) return errorMessage;
if (!isValidDate(date)) {
throw new Error(errorMessage);
}

const inputDate = new Date(date);
inputDate.setTime(
inputDate.getTime() + (inputDate.getTimezoneOffset() + 360) * 60 * 1000
);
const { format = 'eeee, D MMMM, YYYY', calculationMethod = 'BD' } = options;

let formattedDate = format.replace(/eeee|eee/gi, (fmt) => {
Expand Down
85 changes: 35 additions & 50 deletions src/getDay.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { DayOptions } from './types';
import { errorMessage, formatDay, isLeapYear, isValidDate } from './utils';
import {
errorMessage,
formatDay,
getJulianDate,
isLeapYear,
isValidDate,
monthLengthIN,
startJulianDate,
yearLength,
} from './utils';

function getDayBD(day: number, month: number, year: number): number {
let banglaDay: number;
Expand Down Expand Up @@ -49,71 +58,47 @@ function getDayBD(day: number, month: number, year: number): number {
}

function getDayIN(day: number, month: number, year: number): number {
const julianDate = getJulianDate(year, month + 1, day);

if (julianDate < startJulianDate) {
throw new Error(errorMessage);
}

const banglaYear = Math.floor((julianDate - startJulianDate) / yearLength);
const calculatedJulianDate = startJulianDate + banglaYear * yearLength;
let ps: number;
let ns: number;
let banglaDay: number;
switch (month) {
case 0:
banglaDay = day < 16 ? day + 15 : day - 15;
break;
case 1:
banglaDay = day < 14 ? day + 16 : day - 13;
break;
case 2:
if (isLeapYear(year)) {
banglaDay = day < 15 ? day + 16 : day - 14;
} else {
banglaDay = day < 16 ? day + 15 : day - 15;
}
break;
case 3:
if (isLeapYear(year)) {
banglaDay = day < 14 ? day + 17 : day - 13;
} else {
banglaDay = day < 15 ? day + 16 : day - 14;
}
break;
case 4:
banglaDay = day < 16 ? day + 16 : day - 15;
break;
case 5:
banglaDay = day < 16 ? day + 16 : day - 15;
break;
case 6:
banglaDay = day < 18 ? day + 15 : day - 17;
break;
case 7:
banglaDay = day < 18 ? day + 14 : day - 17;
break;
case 8:
banglaDay = day < 18 ? day + 14 : day - 17;
break;
case 9:
banglaDay = day < 19 ? day + 13 : day - 18;
break;
case 10:
banglaDay = day < 18 ? day + 13 : day - 17;
break;
case 11:
banglaDay = day < 17 ? day + 13 : day - 16;
break;
default:
banglaDay = day < 15 ? day + 16 : day - 14;
break;
for (let i = 0; i < 12; i += 1) {
ps = calculatedJulianDate + monthLengthIN[i];
ns = calculatedJulianDate + monthLengthIN[i + 1];

if (julianDate >= ps && julianDate <= Math.floor(ns) + 1.75) {
banglaDay = Math.floor(julianDate - ps) + 1;
}
}

return banglaDay;
}

export function getDay(
date: Date = new Date(),
options: DayOptions = { format: 'D', calculationMethod: 'BD' }
): string {
if (!isValidDate(date)) return errorMessage;
if (!isValidDate(date)) {
throw new Error(errorMessage);
}

const inputDate = new Date(date);
inputDate.setTime(
inputDate.getTime() + (inputDate.getTimezoneOffset() + 360) * 60 * 1000
);

const day = inputDate.getUTCDate();
const month = inputDate.getMonth();
const year = inputDate.getFullYear();
const { format, calculationMethod = 'BD' } = options;

const banglaDay =
calculationMethod === 'BD'
? getDayBD(day, month, year)
Expand Down
Loading
Loading