diff options
author | 2021-08-15 17:15:09 +0530 | |
---|---|---|
committer | 2021-08-15 13:54:00 +0000 | |
commit | bc76d19c6f5687dd18c96db249e0abe7ad79a673 (patch) | |
tree | d4e977f99be77d723ce93bb3866468d8b63ec038 /src/helpers/schedule-helpers.ts | |
parent | chore: npm upgrades (diff) | |
download | ferdium-app-bc76d19c6f5687dd18c96db249e0abe7ad79a673.tar.gz ferdium-app-bc76d19c6f5687dd18c96db249e0abe7ad79a673.tar.zst ferdium-app-bc76d19c6f5687dd18c96db249e0abe7ad79a673.zip |
chore: typescript conversion of some minor utilities
Also removed 'targz' unused package from runtime.
Diffstat (limited to 'src/helpers/schedule-helpers.ts')
-rw-r--r-- | src/helpers/schedule-helpers.ts | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/src/helpers/schedule-helpers.ts b/src/helpers/schedule-helpers.ts new file mode 100644 index 000000000..754fd5556 --- /dev/null +++ b/src/helpers/schedule-helpers.ts | |||
@@ -0,0 +1,70 @@ | |||
1 | /* eslint-disable import/prefer-default-export */ | ||
2 | |||
3 | export function isInTimeframe(start: string, end: string) { | ||
4 | const [ | ||
5 | startHourStr, | ||
6 | startMinuteStr, | ||
7 | ] = start.split(':'); | ||
8 | const startHour = parseInt(startHourStr, 10); | ||
9 | const startMinute = parseInt(startMinuteStr, 10); | ||
10 | |||
11 | const [ | ||
12 | endHourStr, | ||
13 | endMinuteStr, | ||
14 | ] = end.split(':'); | ||
15 | const endHour = parseInt(endHourStr, 10); | ||
16 | const endMinute = parseInt(endMinuteStr, 10); | ||
17 | |||
18 | const currentHour = new Date().getHours(); | ||
19 | const currentMinute = new Date().getMinutes(); | ||
20 | |||
21 | // Check if the end time is before the start time (scheduled overnight) | ||
22 | // as we need to change our checks based on this | ||
23 | const endBeforeStart = (startHour > endHour || (startHour === endHour && startMinute > endMinute)); | ||
24 | |||
25 | if ( | ||
26 | // End is after start (e.g. 09:00-17:00) | ||
27 | !endBeforeStart | ||
28 | // Check if past start | ||
29 | && ((currentHour > startHour | ||
30 | || ( | ||
31 | currentHour === startHour | ||
32 | && currentMinute >= startMinute | ||
33 | ) | ||
34 | ) | ||
35 | // Check that not past end | ||
36 | && (currentHour < endHour | ||
37 | || ( | ||
38 | currentHour === endHour | ||
39 | && currentMinute < endMinute | ||
40 | ) | ||
41 | )) | ||
42 | ) { | ||
43 | // We are in scheduled timeframe | ||
44 | return true; | ||
45 | } | ||
46 | if ( | ||
47 | // End is before start (e.g. 17:00-09:00) | ||
48 | endBeforeStart | ||
49 | // Check if past start | ||
50 | && ((currentHour > startHour | ||
51 | || ( | ||
52 | currentHour === startHour | ||
53 | && currentMinute >= startMinute | ||
54 | ) | ||
55 | ) | ||
56 | // Check that we are not past end | ||
57 | || (currentHour < endHour | ||
58 | || ( | ||
59 | currentHour === endHour | ||
60 | && currentMinute < endMinute | ||
61 | ) | ||
62 | )) | ||
63 | ) { | ||
64 | // We are also in scheduled timeframe | ||
65 | return true; | ||
66 | } | ||
67 | |||
68 | // We are not in scheduled timeframe | ||
69 | return false; | ||
70 | } | ||