Skip to content

Commit

Permalink
fix time even more
Browse files Browse the repository at this point in the history
  • Loading branch information
colinhartigan committed Sep 25, 2024
1 parent c4f6b08 commit 73d9d75
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 34 deletions.
15 changes: 9 additions & 6 deletions src/components/SpaceInfo.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,8 @@ export default function SpaceInfo({}) {
.set("hour", openState.hours[1].split(":")[0])
.set("minute", openState.hours[1].split(":")[1]);

const closingTime = dayjs()
.set("hour", openState.hours[1].split(":")[0])
.set("minute", openState.hours[1].split(":")[1]);

const timeUntilClose = dayjs.duration(
closingTime.diff(dayjs(), "milliseconds"),
closeTime.diff(dayjs(), "milliseconds"),
);
const timeUntilOpen = dayjs.duration(
openTime.diff(dayjs(), "milliseconds"),
Expand All @@ -49,7 +45,14 @@ export default function SpaceInfo({}) {
return ["Closing in", timeUntilClose.humanize()];
} else if (!openState.openNow) {
if (openState.openToday) {
return ["Closed", "After hours"];
if (
dayjs().hour() >= closeTime.hour() &&
dayjs().minute() >= closeTime.minute()
) {
return ["Closed", "After hours"];
} else {
return ["Closed", "Before hours"];
}
} else {
return ["Closed", "Today"];
}
Expand Down
43 changes: 15 additions & 28 deletions src/contexts/TimeContext.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useLocalStorage } from "@uidotdev/usehooks";
import dayjs from "dayjs";
import duration from "dayjs/plugin/duration";
import isBetween from "dayjs/plugin/isBetween";
import relativeTime from "dayjs/plugin/relativeTime";
import utc from "dayjs/plugin/utc";
import PropTypes from "prop-types";
Expand All @@ -9,6 +10,7 @@ const TimeContext = createContext();
dayjs.extend(duration);
dayjs.extend(utc);
dayjs.extend(relativeTime);
dayjs.extend(isBetween);

function TimeProvider({ children }) {
const [timeRaw, setTimeRaw] = useState(dayjs().subtract(1, "minute"));
Expand Down Expand Up @@ -49,38 +51,23 @@ function TimeProvider({ children }) {
const openState = useMemo(() => {
const time = dayjs();
const day = time.format("dddd").toLowerCase();
const timeHours = time.hour();
const timeMinutes = time.minute();

const todayHours = hours[day];
const todayOpenHours = todayHours.hours[0].split(":")[0];
const todayOpenMinutes = todayHours.hours[0].split(":")[1];
const todayCloseHours = todayHours.hours[1].split(":")[0];
const todayCloseMinutes = todayHours.hours[1].split(":")[1];
const openTime = dayjs()
.set("hour", todayHours.hours[0].split(":")[0])
.set("minute", todayHours.hours[0].split(":")[1]);
const closeTime = dayjs()
.set("hour", todayHours.hours[1].split(":")[0])
.set("minute", todayHours.hours[1].split(":")[1]);

if (todayHours.open) {
if (
timeHours >= todayOpenHours &&
timeHours <= todayCloseHours &&
timeMinutes >= todayOpenMinutes &&
timeMinutes <= todayCloseMinutes
) {
if (
timeHours === todayCloseHours &&
timeMinutes >= todayCloseMinutes
) {
return {
openNow: true,
openToday: true,
hours: todayHours.hours,
};
} else {
return {
openNow: true,
openToday: true,
hours: todayHours.hours,
};
}
// Check if the current time is between the opening and closing hours (including minutes)
if (dayjs().isBetween(openTime, closeTime)) {
return {
openNow: false,
openToday: true,
hours: todayHours.hours,
};
} else {
return {
openNow: false,
Expand Down

0 comments on commit 73d9d75

Please sign in to comment.