From f14df47f4c0835393bb778297c6ca4ffd619816f Mon Sep 17 00:00:00 2001 From: Alexsander Lira Date: Fri, 6 Aug 2021 13:37:26 -0300 Subject: [PATCH 1/3] fix(businessMinutesDiff): Fixing diff call for 2 times in the same segment --- package.json | 2 +- src/index.ts | 4 +--- test/businessHoursDiff.spec.ts | 20 ++++++++++++++++++++ 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 546893e..ef47010 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dayjs-business-time", - "version": "1.0.3", + "version": "1.0.4", "author": "Alexsander Lira", "main": "dist/index.js", "types": "types/index.d.ts", diff --git a/src/index.ts b/src/index.ts index b21d5a0..ac36bc7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -374,9 +374,7 @@ const businessTime = ( break; } else if (from.isSameOrAfter(start) && from.isSameOrBefore(end)) { diff += end.diff(from, 'minutes'); - } else { - diff += end.diff(start, 'minutes'); - } + } } return diff ? diff * multiplier : 0; diff --git a/test/businessHoursDiff.spec.ts b/test/businessHoursDiff.spec.ts index be2043c..8d0a85e 100644 --- a/test/businessHoursDiff.spec.ts +++ b/test/businessHoursDiff.spec.ts @@ -97,4 +97,24 @@ describe('Business Hours Diff', () => { expect(diff).toBeDefined(); expect(diff).toBe(-3.5); }); + + it('should get the 1 business hour diff between 2 times in the same business hours segment in a day with 2 segments', () => { + const start = dayjs('2021-05-26 16:00:00'); + const end = dayjs('2021-05-26 17:00:00'); + + const diff = start.businessHoursDiff(end); + + expect(diff).toBeDefined(); + expect(diff).toBe(1); + }); + + it('should get the 9.5 business hour diff between 2 times in 2 business days with a day with 2 segments', () => { + const start = dayjs('2021-05-25 14:30:00'); + const end = dayjs('2021-05-26 17:00:00'); + + const diff = start.businessHoursDiff(end); + + expect(diff).toBeDefined(); + expect(diff).toBe(9.5); + }); }); From dfa59255bcc62ecced7fc6542a995d3d93f2656b Mon Sep 17 00:00:00 2001 From: Alexsander Lira Date: Fri, 6 Aug 2021 13:45:24 -0300 Subject: [PATCH 2/3] test(businessHoursDiff): Fix test for UTC environments --- test/businessHoursDiff.spec.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/businessHoursDiff.spec.ts b/test/businessHoursDiff.spec.ts index 8d0a85e..92ec0c5 100644 --- a/test/businessHoursDiff.spec.ts +++ b/test/businessHoursDiff.spec.ts @@ -89,8 +89,8 @@ describe('Business Hours Diff', () => { }); it('should get the -3.5 business hours diff between 2 times in the same day with timezone', () => { - const start = dayjs('2021-05-17T15:30:00-03:00'); - const end = dayjs('2021-05-17T12:00:00-03:00'); + const start = dayjs('2021-05-17T14:30:00-03:00'); + const end = dayjs('2021-05-17T11:00:00-03:00'); const diff = start.businessHoursDiff(end); From 89f166eaf7f561645152ef0ad65496ee61dc320e Mon Sep 17 00:00:00 2001 From: Alexsander Lira Date: Fri, 6 Aug 2021 13:47:42 -0300 Subject: [PATCH 3/3] test(businessHoursDiff): Fix test for UTC environments --- test/businessHoursDiff.spec.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/businessHoursDiff.spec.ts b/test/businessHoursDiff.spec.ts index 92ec0c5..73b6367 100644 --- a/test/businessHoursDiff.spec.ts +++ b/test/businessHoursDiff.spec.ts @@ -89,8 +89,8 @@ describe('Business Hours Diff', () => { }); it('should get the -3.5 business hours diff between 2 times in the same day with timezone', () => { - const start = dayjs('2021-05-17T14:30:00-03:00'); - const end = dayjs('2021-05-17T11:00:00-03:00'); + const start = dayjs('2021-05-17T13:30:00-03:00'); + const end = dayjs('2021-05-17T10:00:00-03:00'); const diff = start.businessHoursDiff(end);