aboutsummaryrefslogtreecommitdiffstats
path: root/src/helpers/schedule-helpers.ts
blob: 37caffd79451e638124b4f4d9d1b09ba6152f010 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
export function isInTimeframe(start: string, end: string) {
  const [startHourStr, startMinuteStr] = start.split(':');
  const startHour = Number.parseInt(startHourStr, 10);
  const startMinute = Number.parseInt(startMinuteStr, 10);

  const [endHourStr, endMinuteStr] = end.split(':');
  const endHour = Number.parseInt(endHourStr, 10);
  const endMinute = Number.parseInt(endMinuteStr, 10);

  const currentHour = new Date().getHours();
  const currentMinute = new Date().getMinutes();

  // Check if the end time is before the start time (scheduled overnight)
  // as we need to change our checks based on this
  const endBeforeStart =
    startHour > endHour || (startHour === endHour && startMinute > endMinute);

  if (
    // End is after start (e.g. 09:00-17:00)
    !endBeforeStart &&
    // Check if past start
    (currentHour > startHour ||
      (currentHour === startHour && currentMinute >= startMinute)) &&
    // Check that not past end
    (currentHour < endHour ||
      (currentHour === endHour && currentMinute < endMinute))
  ) {
    // We are in scheduled timeframe
    return true;
  }
  if (
    // End is before start (e.g. 17:00-09:00)
    endBeforeStart &&
    // Check if past start
    (currentHour > startHour ||
      (currentHour === startHour && currentMinute >= startMinute) ||
      // Check that we are not past end
      currentHour < endHour ||
      (currentHour === endHour && currentMinute < endMinute))
  ) {
    // We are also in scheduled timeframe
    return true;
  }

  // We are not in scheduled timeframe
  return false;
}