-
-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathcalifornia-clock-change.html
More file actions
387 lines (347 loc) · 15 KB
/
california-clock-change.html
File metadata and controls
387 lines (347 loc) · 15 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>California Clock Change - PST/PDT Only</title>
<style>
body {
font-family: system-ui, -apple-system, sans-serif;
margin: 0;
padding: 20px;
background-color: #f0f4f8;
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
.container {
background: white;
padding: 2rem;
border-radius: 12px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
max-width: 600px;
width: 90%;
text-align: center;
}
h1 {
color: #1a365d;
font-size: 1.8rem;
margin-bottom: 0.5rem;
}
.subtitle {
color: #4a5568;
margin-bottom: 1.5rem;
}
.info-box {
background: #e6fffa;
border: 2px solid #38b2ac;
border-radius: 8px;
padding: 1.5rem;
margin: 1rem 0;
font-size: 1.2rem;
line-height: 1.6;
}
.highlight {
color: #2b6cb0;
font-weight: bold;
}
.tonight {
display: inline-block;
background: #f6e05e;
color: #744210;
padding: 0.2em 0.5em;
border-radius: 4px;
margin-left: 0.5em;
font-weight: bold;
}
.wrong-timezone {
background: #fef3c7;
border: 2px solid #f59e0b;
border-radius: 8px;
padding: 1.5rem;
margin: 1rem 0;
font-size: 1.2rem;
line-height: 1.6;
color: #92400e;
}
.wrong-timezone p {
margin: 0 0 1rem 0;
}
.wrong-timezone p:last-child {
margin-bottom: 0;
}
.pretend-btn {
background: #4299e1;
color: white;
border: none;
padding: 0.75rem 1.5rem;
border-radius: 8px;
font-size: 1rem;
cursor: pointer;
transition: background 0.2s;
margin-top: 0.5rem;
}
.pretend-btn:hover {
background: #2b6cb0;
}
.body-clock {
background: #ebf8ff;
border: 2px solid #4299e1;
border-radius: 8px;
padding: 1.5rem;
margin: 1rem 0;
font-size: 1.2rem;
line-height: 1.6;
}
.past-change {
background: #faf5ff;
border: 2px solid #805ad5;
border-radius: 8px;
padding: 1.5rem;
margin: 1rem 0;
font-size: 1.2rem;
line-height: 1.6;
}
@media (max-width: 480px) {
.container {
padding: 1.5rem;
}
h1 {
font-size: 1.5rem;
}
.info-box, .body-clock, .past-change {
font-size: 1.1rem;
padding: 1rem;
}
}
</style>
</head>
<body>
<div class="container">
<h1>California Clock Change</h1>
<div class="subtitle">For Pacific Time (PST/PDT) only</div>
<div id="past-change"></div>
<div id="future-change"></div>
<div id="body-clock"></div>
<div id="current-dst"></div>
<details style="margin-top: 2rem;">
<summary style="cursor: pointer; color: #a0aec0; font-size: 0.8rem;">Tests</summary>
<pre id="test-results" style="text-align: left; font-size: 0.75rem; color: #718096; margin: 0.5rem 0;"></pre>
</details>
</div>
<script>
let pretendCaliforniaMode = false;
function isCaliforniaTimezone() {
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
return timezone === "America/Los_Angeles" ||
timezone === "America/Pacific" ||
timezone === "US/Pacific" ||
timezone === "PST8PDT";
}
function enablePretendMode() {
pretendCaliforniaMode = true;
updateInfo();
}
function getClockChanges() {
const now = new Date();
const year = now.getFullYear();
// Get changes for current and adjacent years to handle year boundaries
function getYearChanges(y) {
// Second Sunday in March (treat Sunday as 7 so the formula works when the 1st is a Sunday)
const marchDay = new Date(y, 2, 1).getDay() || 7;
const springForward = new Date(y, 2, 1 + (14 - marchDay));
// First Sunday in November
const novDay = new Date(y, 10, 1).getDay() || 7;
const fallBack = new Date(y, 10, 1 + (7 - novDay));
return [
{ date: springForward, isSpringForward: true },
{ date: fallBack, isSpringForward: false }
];
}
const changes = [
...getYearChanges(year - 1),
...getYearChanges(year),
...getYearChanges(year + 1)
].sort((a, b) => a.date - b.date);
// Find the most recent past change and next future change
const pastChange = changes.filter(change => change.date < now).pop();
const futureChange = changes.find(change => change.date > now);
return { pastChange, futureChange };
}
function formatDate(date) {
const months = ['January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December'];
return `${months[date.getMonth()]} ${date.getDate()}, ${date.getFullYear()}`;
}
function formatTime(date) {
return date.toLocaleTimeString('en-US', {
hour: 'numeric',
minute: '2-digit',
hour12: true
});
}
function getDaysSince(date) {
const now = new Date();
const diffTime = Math.abs(now - date);
return Math.floor(diffTime / (1000 * 60 * 60 * 24));
}
function updateBodyClockInfo(pastChange) {
const bodyClockDiv = document.getElementById('body-clock');
const daysSince = getDaysSince(pastChange.date);
if (daysSince <= 7) { // Show message for a week after the change
const now = new Date();
const dogClock = new Date(now);
if (pastChange.isSpringForward) {
dogClock.setHours(now.getHours() - 1);
} else {
dogClock.setHours(now.getHours() + 1);
}
bodyClockDiv.className = 'body-clock';
bodyClockDiv.innerHTML = `
<p>🐺 Is your dog confused about meal or walk times?</p>
<p>While your clock says it's <span class="highlight">${formatTime(now)}</span>,
your dog's internal clock thinks it's <span class="highlight">${formatTime(dogClock)}</span>!</p>
<p>Dogs have very reliable internal clocks and don't automatically adjust to Daylight Saving Time.
Don't worry - your pup's schedule should adjust naturally over the next few days.</p>
`;
} else {
bodyClockDiv.innerHTML = '';
}
}
function updateInfo() {
if (!isCaliforniaTimezone() && !pretendCaliforniaMode) {
document.getElementById('past-change').innerHTML = '';
document.getElementById('body-clock').innerHTML = '';
document.getElementById('current-dst').innerHTML = '';
document.getElementById('future-change').className = 'wrong-timezone';
document.getElementById('future-change').innerHTML = `
<p>👋 Hey there! It looks like you're not in California.</p>
<p>This tool is designed specifically for folks in the Pacific Time zone (PST/PDT), so it won't be accurate for your location.</p>
<button class="pretend-btn" onclick="enablePretendMode()">Pretend I am in California anyway</button>
`;
return;
}
const { pastChange, futureChange } = getClockChanges();
// Show past change
const pastChangeDiv = document.getElementById('past-change');
const pastSaturday = new Date(pastChange.date);
pastSaturday.setDate(pastChange.date.getDate() - 1);
pastChangeDiv.className = 'past-change';
pastChangeDiv.innerHTML = `
<div>Most recent change:</div>
On <span class="highlight">Sunday, ${formatDate(pastChange.date)}</span>,
clocks ${pastChange.isSpringForward ? 'sprang forward' : 'fell back'}
(you ${pastChange.isSpringForward ? 'lost' : 'gained'} one hour of sleep).
`;
// Show future change
const futureChangeDiv = document.getElementById('future-change');
const futureSaturday = new Date(futureChange.date);
futureSaturday.setDate(futureChange.date.getDate() - 1);
const now = new Date();
const isTonight = now.getDate() === futureSaturday.getDate() &&
now.getMonth() === futureSaturday.getMonth() &&
now.getFullYear() === futureSaturday.getFullYear();
const sleepEffect = futureChange.isSpringForward ? 'lose an hour of sleep' : 'get an extra hour of sleep';
const clockEffect = futureChange.isSpringForward ?
'clocks spring forward from 2:00 AM to 3:00 AM' :
'clocks fall back from 2:00 AM to 1:00 AM';
const tonightBadge = isTonight ? '<span class="tonight">That\'s tonight!</span>' : '';
futureChangeDiv.className = 'info-box';
futureChangeDiv.innerHTML = `
<div>Next change:</div>
When you go to bed on <span class="highlight">Saturday, ${formatDate(futureSaturday)}</span>${tonightBadge},
you will ${sleepEffect}!<br><br>
The ${clockEffect} on
<span class="highlight">Sunday, ${formatDate(futureChange.date)}</span>.
`;
// Update body clock info based on past change
updateBodyClockInfo(pastChange);
// Determine if it is currently daylight savings time or not
const currentDstDiv = document.getElementById('current-dst');
const isDst = pastChange.isSpringForward && !futureChange.isSpringForward;
currentDstDiv.className = 'info-box';
currentDstDiv.innerHTML = `
<div>Current status:</div>
It is currently <span class="highlight">${isDst ? 'Daylight Saving Time (PDT)' : 'Standard Time (PST)'}</span>.
`;
}
updateInfo();
// Update the times every minute
setInterval(updateInfo, 60000);
// Automated tests
function runTests() {
const results = [];
let passed = 0;
let failed = 0;
let currentGroup = null;
function group(name) {
currentGroup = name;
results.push({ type: 'group', name });
}
function assert(name, actual, expected) {
if (actual === expected) {
passed++;
results.push({ type: 'pass', name, actual });
} else {
failed++;
results.push({ type: 'fail', name, actual, expected });
}
}
function dateStr(d) {
return `${d.getFullYear()}-${String(d.getMonth()+1).padStart(2,'0')}-${String(d.getDate()).padStart(2,'0')}`;
}
function friendlyDate(d) {
const days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
const months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
return `${days[d.getDay()]} ${months[d.getMonth()]} ${d.getDate()}, ${d.getFullYear()}`;
}
// Helper to compute DST changes for a given year (mirrors getYearChanges)
function changesForYear(y) {
const marchDay = new Date(y, 2, 1).getDay() || 7;
const springForward = new Date(y, 2, 1 + (14 - marchDay));
const novDay = new Date(y, 10, 1).getDay() || 7;
const fallBack = new Date(y, 10, 1 + (7 - novDay));
return { springForward, fallBack };
}
// Test known DST dates for several years
const knownDates = [
{ year: 2024, spring: '2024-03-10', fall: '2024-11-03' },
{ year: 2025, spring: '2025-03-09', fall: '2025-11-02' },
{ year: 2026, spring: '2026-03-08', fall: '2026-11-01', note: '1st is Sunday' },
{ year: 2027, spring: '2027-03-14', fall: '2027-11-07' },
{ year: 2028, spring: '2028-03-12', fall: '2028-11-05' },
];
for (const { year, spring, fall, note } of knownDates) {
const label = note ? `${year} (${note})` : `${year}`;
group(label);
const c = changesForYear(year);
assert(`Spring forward = ${spring}`, dateStr(c.springForward), spring);
assert(`Fall back = ${fall}`, dateStr(c.fallBack), fall);
assert(`Spring forward (${friendlyDate(c.springForward)}) is Sunday`, c.springForward.getDay(), 0);
assert(`Fall back (${friendlyDate(c.fallBack)}) is Sunday`, c.fallBack.getDay(), 0);
}
// Render results
const el = document.getElementById('test-results');
const lines = [];
for (const r of results) {
if (r.type === 'group') {
lines.push(`\n── ${r.name} ──`);
} else if (r.type === 'pass') {
lines.push(` ✓ ${r.name}`);
} else {
lines.push(` ✗ ${r.name}`);
lines.push(` expected: ${r.expected}`);
lines.push(` got: ${r.actual}`);
}
}
const summary = `${passed} passed, ${failed} failed of ${passed + failed} tests`;
lines.push(`\n${summary}`);
el.textContent = lines.join('\n');
el.parentElement.querySelector('summary').textContent = `Tests: ${summary}`;
if (failed > 0) el.parentElement.open = true;
}
runTests();
</script>
</body>
</html>