-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathdoor-time.js
61 lines (54 loc) · 1.27 KB
/
door-time.js
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
// isCurrent takes a time specification and returns a boolean to
// indicate that the current time is within the specification.
//
// The specification currently supports three properties: daysOfWeek,
// startTime, and stopTime.
//
// daysOfWeek is an array of ints in the range [0,6], where 0
// represents Sunday.
//
// startTime and stopTime are strings in the format "HH:MM".
//
// This function does no error-checking.
function isCurrent(spec) {
if (!spec) {
return true;
}
var d = new Date();
var pad = function(n) {
// If we're given a single-digit number, zero-pad it.
var s = n.toString();
if (1 == s.length) {
s = "0" + s;
}
return s;
}
var padt = function(t) {
// If we're given 1:23, return 01:23.
if (4 == t.length) {
t = "0" + t;
}
return t;
}
if (spec.daysOfWeek) {
var rightDay = false;
var day = d.getDay();
for (var i = 0; i < spec.daysOfWeek; i++) {
var want = spec.daysOfWeek[i];
if (day == want) {
rightDay = true;
break;
}
}
if (!rightDay) {
return false;
}
}
if (spec.startTime && spec.stopTime) {
var t = pad(d.getHours()) + ":" + pad(d.getMinutes());
var start = padt(spec.startTime);
var stop = padt(spec.stopTime);
return start <= t && t <= stop;
}
return true;
}