Skip to content

Commit

Permalink
Merge pull request #4690 from yykcool/main
Browse files Browse the repository at this point in the history
Add support for seconds in date_utils
  • Loading branch information
martijnrusschen authored Apr 15, 2024
2 parents 29db157 + 9af5770 commit 5e9cfa4
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 15 deletions.
27 changes: 16 additions & 11 deletions src/date_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,8 @@ export function isTimeInList(time, times) {
return times.some(
(listTime) =>
getHours(listTime) === getHours(time) &&
getMinutes(listTime) === getMinutes(time),
getMinutes(listTime) === getMinutes(time) &&
getSeconds(listTime) === getSeconds(time),
);
}

Expand All @@ -619,16 +620,20 @@ export function isTimeInDisabledRange(time, { minTime, maxTime }) {
if (!minTime || !maxTime) {
throw new Error("Both minTime and maxTime props required");
}
const base = newDate();
const baseTime = setHours(setMinutes(base, getMinutes(time)), getHours(time));
const min = setHours(
setMinutes(base, getMinutes(minTime)),
getHours(minTime),
);
const max = setHours(
setMinutes(base, getMinutes(maxTime)),
getHours(maxTime),
);
let baseTime = newDate();
baseTime = setHours(baseTime, getHours(time));
baseTime = setMinutes(baseTime, getMinutes(time));
baseTime = setSeconds(baseTime, getSeconds(time));

let min = newDate();
min = setHours(min, getHours(minTime));
min = setMinutes(min, getMinutes(minTime));
min = setSeconds(min, getSeconds(minTime));

let max = newDate();
max = setHours(max, getHours(maxTime));
max = setMinutes(max, getMinutes(maxTime));
max = setSeconds(max, getSeconds(maxTime));

let valid;
try {
Expand Down
11 changes: 7 additions & 4 deletions test/date_utils_test.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ import { setHours } from "date-fns/setHours";
import { addQuarters } from "date-fns/addQuarters";
import { ptBR } from "date-fns/locale/pt-BR";
import { registerLocale } from "../src/date_utils";
import { addYears } from "date-fns";
import { addYears, setSeconds } from "date-fns";

registerLocale("pt-BR", ptBR);

Expand Down Expand Up @@ -888,9 +888,12 @@ describe("date_utils", () => {
describe("isTimeInDisabledRange", () => {
it("should tell if time is in disabled range", () => {
const date = newDate("2016-03-15");
const time = setHours(setMinutes(date, 30), 1);
const minTime = setHours(setMinutes(date, 30), 0);
const maxTime = setHours(setMinutes(date, 30), 5);
let time = setHours(date, 1);
time = setMinutes(time, 30);
time = setSeconds(time, 30); // 2016-03-15 01:30:30

const minTime = setHours(setMinutes(date, 30), 0); //2016-03-15 00:30:00
const maxTime = setHours(setMinutes(setSeconds(date, 35), 30), 1); //2016-03-15 01:30:35
expect(isTimeInDisabledRange(time, { minTime, maxTime })).toBe(false);
});

Expand Down
25 changes: 25 additions & 0 deletions test/include_times_test.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,29 @@ describe("TimeComponent", () => {
});
expect(enabledTimeItemsHasNoAriaDisabled).toBe(true);
});

it("should factor in seconds", () => {
const includeHoursMinutesSeconds = [
utils.addHours(utils.addSeconds(today, 30), 1), //01:00:30
utils.addSeconds(today, 30), //00:00:30
];
const { container: timeComponent } = render(
<TimeComponent
format="HH:mm:ss"
includeTimes={includeHoursMinutesSeconds}
/>,
);

const disabledTimeItems = timeComponent.querySelectorAll(
".react-datepicker__time-list-item--disabled",
);

// 01:00:00 and 00:00:00 should be correctly disabled because they are not included
expect(
Array.from(disabledTimeItems).map((node) => node.textContent),
).toContain("01:00:00");
expect(
Array.from(disabledTimeItems).map((node) => node.textContent),
).toContain("00:00:00");
});
});

0 comments on commit 5e9cfa4

Please sign in to comment.