aboutsummaryrefslogtreecommitdiffstats
path: root/src/helpers/schedule-helpers.ts
blob: 754fd5556b4e13eda79c79af47dcdc20593bc414 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/* eslint-disable import/prefer-default-export */

export function isInTimeframe(start: string, end: string) {
  const [
    startHourStr,
    startMinuteStr,
  ] = start.split(':');
  const startHour = parseInt(startHourStr, 10);
  const startMinute = parseInt(startMinuteStr, 10);

  const [
    endHourStr,
    endMinuteStr,
  ] = end.split(':');
  const endHour = parseInt(endHourStr, 10);
  const endMinute = 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;
}