fix: properly floor the partial results (#1629)

This commit is contained in:
Piotr Idzik
2024-03-04 23:06:50 +01:00
committed by GitHub
parent 2fe0dfde23
commit f13eec1bbb
2 changed files with 48 additions and 36 deletions

View File

@ -14,21 +14,27 @@ const isLeap = (year) => {
else return false else return false
} }
const DateToDay = (dd, mm, yyyy) => { const DateToDay = (dd, mm, yyyy) => {
return Math.floor( return (
365 * (yyyy - 1) + 365 * (yyyy - 1) +
(yyyy - 1) / 4 - Math.floor((yyyy - 1) / 4) -
(yyyy - 1) / 100 + Math.floor((yyyy - 1) / 100) +
(yyyy - 1) / 400 + Math.floor((yyyy - 1) / 400) +
dd + dd +
(367 * mm - 362) / 12 + Math.floor((367 * mm - 362) / 12) +
(mm <= 2 ? 0 : isLeap(yyyy) ? -1 : -2) (mm <= 2 ? 0 : isLeap(yyyy) ? -1 : -2)
) )
} }
const CheckDayAndMonth = (inDay, inMonth) => {
if (inDay <= 0 || inDay > 31 || inMonth <= 0 || inMonth > 12) {
throw new TypeError('Date is not valid.')
}
}
const DateDayDifference = (date1, date2) => { const DateDayDifference = (date1, date2) => {
// firstly, check that both input are string or not. // firstly, check that both input are string or not.
if (typeof date1 !== 'string' || typeof date2 !== 'string') { if (typeof date1 !== 'string' || typeof date2 !== 'string') {
return new TypeError('Argument is not a string.') throw new TypeError('Argument is not a string.')
} }
// extract the first date // extract the first date
const [firstDateDay, firstDateMonth, firstDateYear] = date1 const [firstDateDay, firstDateMonth, firstDateYear] = date1
@ -39,18 +45,9 @@ const DateDayDifference = (date1, date2) => {
.split('/') .split('/')
.map((ele) => Number(ele)) .map((ele) => Number(ele))
// check the both data are valid or not. // check the both data are valid or not.
if ( CheckDayAndMonth(firstDateDay, firstDateMonth)
firstDateDay < 0 || CheckDayAndMonth(secondDateDay, secondDateMonth)
firstDateDay > 31 ||
firstDateMonth > 12 ||
firstDateMonth < 0 ||
secondDateDay < 0 ||
secondDateDay > 31 ||
secondDateMonth > 12 ||
secondDateMonth < 0
) {
return new TypeError('Date is not valid.')
}
return Math.abs( return Math.abs(
DateToDay(secondDateDay, secondDateMonth, secondDateYear) - DateToDay(secondDateDay, secondDateMonth, secondDateYear) -
DateToDay(firstDateDay, firstDateMonth, firstDateYear) DateToDay(firstDateDay, firstDateMonth, firstDateYear)

View File

@ -1,21 +1,36 @@
import { DateDayDifference } from '../DateDayDifference' import { DateDayDifference } from '../DateDayDifference'
test('The difference between 17/08/2002 & 10/10/2020 is 6630', () => { describe('DateDayDifference', () => {
const res = DateDayDifference('17/08/2002', '10/10/2020') it.each([
expect(res).toBe(6630) ['17/08/2002', '10/10/2020', 6629],
}) ['18/02/2001', '16/03/2022', 7696],
['11/11/2011', '12/12/2012', 397],
['01/01/2001', '16/03/2011', 3726],
['04/03/2024', '04/03/2024', 0],
['03/03/2024', '04/03/2024', 1],
['02/03/2024', '04/03/2024', 2],
['01/03/2024', '04/03/2024', 3],
['29/02/2024', '04/03/2024', 4],
['04/03/2024', '04/03/2025', 365],
['04/03/2023', '04/03/2024', 366]
])(
'The difference between %s and %s is %i',
(firstDate, secondDate, expected) => {
expect(DateDayDifference(firstDate, secondDate)).toBe(expected)
expect(DateDayDifference(secondDate, firstDate)).toBe(expected)
}
)
test('The difference between 18/02/2001 & 16/03/2022 is 7696', () => { it('should throw when any input is not a string', () => {
const res = DateDayDifference('18/02/2001', '16/03/2022') expect(() => DateDayDifference(10102024, '11/10/2024')).toThrowError()
expect(res).toBe(7696) expect(() => DateDayDifference('11/10/2024', 10102024)).toThrowError()
}) })
test('The difference between 11/11/2011 & 12/12/2012 is 398', () => { it.each(['32/01/2000', '00/01/2000', '15/00/2000', '15/13/2000'])(
const res = DateDayDifference('11/11/2011', '12/12/2012') 'should throw when input is not a correct date %s',
expect(res).toBe(398) (wrongDate) => {
}) expect(() => DateDayDifference(wrongDate, '04/03/2024')).toThrowError()
expect(() => DateDayDifference('04/03/2024', wrongDate)).toThrowError()
test('The difference between 01/01/2001 & 16/03/2011 is 3727', () => { }
const res = DateDayDifference('01/01/2001', '16/03/2011') )
expect(res).toBe(3727)
}) })