Skip to content
This repository has been archived by the owner on Mar 27, 2024. It is now read-only.

Commit

Permalink
Add addBusinessDays helper
Browse files Browse the repository at this point in the history
  • Loading branch information
mobily committed Feb 1, 2020
1 parent edbedfb commit 89e3ff5
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 1 deletion.
2 changes: 1 addition & 1 deletion STATUS.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@

### Day helpers

- [ ] `addBusinessDays`
- [x] `addBusinessDays`
- [x] `addDays`
- [x] `subDays`
- [ ] `differenceInBusinessDays`
Expand Down
56 changes: 56 additions & 0 deletions __tests__/addBusinessDays_test.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
open Jest;

open Js.Date;

describe("addBusinessDays", () => {
open ExpectJs;

test("adds 0 business days", () => {
let date = makeWithYMD(~year=2020., ~month=0., ~date=1., ());
let expectedDate = makeWithYMD(~year=2020., ~month=0., ~date=1., ());

date |> ReDate.addBusinessDays(0) |> expect |> toEqual(expectedDate);
});

test("adds 1 business day", () => {
let date = makeWithYMD(~year=2020., ~month=0., ~date=1., ());
let expectedDate = makeWithYMD(~year=2020., ~month=0., ~date=2., ());

date |> ReDate.addBusinessDays(1) |> expect |> toEqual(expectedDate);
});

test("adds 100 business days", () => {
let date = makeWithYMD(~year=2020., ~month=0., ~date=1., ());
let expectedDate = makeWithYMD(~year=2020., ~month=4., ~date=20., ());

date |> ReDate.addBusinessDays(100) |> expect |> toEqual(expectedDate);
});

test("handles negative number", () => {
let date = makeWithYMD(~year=2020., ~month=4., ~date=20., ());
let expectedDate = makeWithYMD(~year=2020., ~month=0., ~date=1., ());

date |> ReDate.addBusinessDays(-100) |> expect |> toEqual(expectedDate);
});

test("returns Monday when 1 day is added on Friday", () => {
let date = makeWithYMD(~year=2020., ~month=0., ~date=10., ());
let expectedDate = makeWithYMD(~year=2020., ~month=0., ~date=13., ());

date |> ReDate.addBusinessDays(1) |> expect |> toEqual(expectedDate);
});

test("returns Monday when 1 day is added on Saturday", () => {
let date = makeWithYMD(~year=2020., ~month=0., ~date=11., ());
let expectedDate = makeWithYMD(~year=2020., ~month=0., ~date=13., ());

date |> ReDate.addBusinessDays(1) |> expect |> toEqual(expectedDate);
});

test("returns Monday when 1 day is added on Sunday", () => {
let date = makeWithYMD(~year=2020., ~month=0., ~date=12., ());
let expectedDate = makeWithYMD(~year=2020., ~month=0., ~date=13., ());

date |> ReDate.addBusinessDays(1) |> expect |> toEqual(expectedDate);
});
});
12 changes: 12 additions & 0 deletions docs/day.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ let date = Js.Date.makeWithYMD(~year=2018., ~month=0., ~date=1., ());
date |> ReDate.addDays(5);
```

#### addBusinessDays

> Add the specified number of business days to the given date (excluding weekends).
`let addBusinessDays: (int, Js.Date.t) => Js.Date.t`

```reason
let date = Js.Date.makeWithYMD(~year=2018., ~month=0., ~date=1., ());
date |> ReDate.addBusinessDays(5);
```

#### subDays

> Subtract the specified number of days from the given date.
Expand Down
13 changes: 13 additions & 0 deletions src/ReDate.re
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,19 @@ let isSameHour = (fst, snd) =>

let addDays = Internal.addDays;

let rec addBusinessDays = (days, date) => {
let next = days < 0 ? succ : pred;
let sign = days < 0 ? (-1) : 1;
let date = date |> addDays(sign);

if (Internal.isWeekend(date)) {
date |> addBusinessDays(days);
} else {
days == 0
? date |> addDays(- sign) : date |> addBusinessDays(days |> next);
};
};

let subDays = (days, date) => date |> addDays(- days);

let startOfDay = Internal.makeDateWithStartOfDayHours;
Expand Down
2 changes: 2 additions & 0 deletions src/ReDate.rei
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ let isSameHour: (Js.Date.t, Js.Date.t) => bool;

/* β€”β€”[Day helpersβ€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€” */

let addBusinessDays: (int, Js.Date.t) => Js.Date.t;

let addDays: (int, Js.Date.t) => Js.Date.t;

let subDays: (int, Js.Date.t) => Js.Date.t;
Expand Down

0 comments on commit 89e3ff5

Please sign in to comment.