-
Notifications
You must be signed in to change notification settings - Fork 1
/
shift.html
165 lines (155 loc) · 5.3 KB
/
shift.html
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Centadiem</title>
<link rel="apple-touch-icon" sizes="180x180" href="apple-touch-icon.png" />
<link rel="icon" type="image/png" sizes="32x32" href="favicon-32x32.png" />
<link rel="icon" type="image/png" sizes="16x16" href="favicon-16x16.png" />
<link rel="manifest" href="site.webmanifest" />
<link rel="mask-icon" href="safari-pinned-tab.svg" color="#5bbad5" />
<meta name="msapplication-TileColor" content="#da532c" />
<meta name="theme-color" content="#ffffff" />
<link
href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD"
crossorigin="anonymous"
/>
<script src="centadiem.js"></script>
<style type="text/css">
.container {
container-type: inline-size;
font-weight: 700;
}
@container (min-width: 6px) {
h1 {
font-size: 12cqw !important;
font-weight: 200;
}
#time-current {
font-size: 20cqw !important;
font-weight: 700;
height: fit-content !important;
}
}
tr {
width: 100%;
}
th,
td {
width: 50%;
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-12">
<h1>Centa<span class="text-secondary">shift</span></h1>
<div id="time-current"></div>
<p class="lead fs-3">
A shift is 100% long. The current time is the percentage of the
shift that has passed. Current timezone is
<span id="timezone"></span>.
</p>
</div>
</div>
<!-- Take input time and length of the shift -->
<div class="row">
<div class="col-12">
<form onsubmit="saveToLocalStorage()">
<div class="mb-3">
<label for="time-start" class="form-label">Start time</label>
<input
type="time"
class="form-control"
id="time-start"
aria-describedby="time-start-help"
value="11:00"
/>
<div id="time-start-help" class="form-text">
The time the shift starts.
</div>
</div>
<div class="mb-3">
<label for="time-end" class="form-label">End time</label>
<input
type="time"
class="form-control"
id="time-end"
aria-describedby="time-end-help"
value="19:00"
/>
<div id="time-end-help" class="form-text">
The time the shift ends.
</div>
</div>
<button type="submit" class="btn btn-primary">
Calculate shift
</button>
</form>
</div>
</div>
</div>
<script>
if ("serviceWorker" in navigator) {
// register service worker
navigator.serviceWorker.register("/centadiem/service-worker.js");
}
// get saved start and end time from local storage
const savedStart = localStorage.getItem("start");
const savedEnd = localStorage.getItem("end");
// if start and end time are saved, use them
if (savedStart && savedEnd) {
document.getElementById("time-start").value = savedStart;
document.getElementById("time-end").value = savedEnd;
}
document.getElementById("timezone").innerHTML =
Intl.DateTimeFormat().resolvedOptions().timeZone;
calculateShiftTime();
// every 0.25 seconds, calculate the shift time
setInterval(calculateShiftTime, 250);
function calculateShiftTime() {
// get start and end time from local storage
var start = localStorage.getItem("start");
var end = localStorage.getItem("end");
if (!start || !end) {
// if start and end time are not saved, use the default values
start = document.getElementById("time-start").value;
end = document.getElementById("time-end").value;
}
var now = new Date();
var start = new Date(
now.getFullYear(),
now.getMonth(),
now.getDate(),
start.split(":")[0],
start.split(":")[1]
);
var end = new Date(
now.getFullYear(),
now.getMonth(),
now.getDate(),
end.split(":")[0],
end.split(":")[1]
);
var shiftLength = end - start;
var shiftPercentage = ((now - start) / shiftLength) * 100;
document.getElementById("time-current").innerHTML =
shiftPercentage.toFixed(2);
}
function saveToLocalStorage() {
//prevent page from reloading
event.preventDefault();
var start = document.getElementById("time-start").value;
var end = document.getElementById("time-end").value;
localStorage.setItem("start", start);
localStorage.setItem("end", end);
calculateShiftTime();
}
</script>
</body>
</html>