Step the sleep and walk trend charts by clock hour

Both charts walked the day in fixed hours from midnight, so on a DST day
the curve ended at 23:00 (autumn) or 01:00 the next day (spring), cutting
or padding that day's total and its share of the average, and today's
line ended an hour off the now marker, which reads the clock. Step through
the day with clockHourTs and plot points at their clock hour instead.
This commit is contained in:
Alexander Heldt committed 2026-10-28 20:03:22 +00:00
1 parent a7d3575474
commit 35275e6c2c
2 files changed
+32 -7

No files matched your search

+31 -7
View File
@@ -2844,6 +2844,23 @@
d.getSeconds() * 1000 + d.getMilliseconds()) / 86_400_000;
}
// The instant the clock reads h:00 on the day starting at dayStart; h = 24
// is the next midnight. On a DST day this is not dayStart + h hours, so the
// hour-of-day charts step through the day with this rather than adding up
// fixed hours. (A skipped hour lands on the next one; a repeated one on its
// first pass.)
function clockHourTs(dayStart, h) {
const d = new Date(dayStart);
d.setHours(h, 0, 0, 0);
return d.getTime();
}
// Hour of day 0..24 by the clock for a time within the day starting at
// dayStart, with the next midnight as 24 rather than 0.
function clockHourOf(ts, dayStart) {
return ts >= clockHourTs(dayStart, 24) ? 24 : clockDayFraction(ts) * 24;
}
// Vertical "now" rule for those charts: ink over a surface-coloured underlay
// so it stays legible whatever bar or cell it crosses, with a caret hung
// above the track so it doesn't read as one more gridline. Append the parts
@@ -2975,7 +2992,7 @@
// guess; walks are decisions, and projecting them would be predicting the
// handler, not the dog.
function walkTrendCurves(events) {
const HOUR = 3_600_000, MIN = 60_000, DAY = 86_400_000;
const MIN = 60_000;
const windows = walkWindows(events); // an ongoing walk is already clipped to now
const walkedMs = (from, to) => {
let total = 0;
@@ -2994,13 +3011,20 @@
return d;
};
const dayStartTs = (daysAgo) => dayAgo(daysAgo).getTime();
const pointsAt = (start, stops) =>
stops.map(t => ({ x: (t - start) / HOUR, y: walkedMs(start, t) / MIN }));
// x is the clock hour, held from stepping back inside the hour that
// repeats when the clocks go back, so the step line never folds over.
const pointsAt = (start, stops) => {
let lastX = 0;
return stops.map(t => {
lastX = Math.max(lastX, clockHourOf(t, start));
return { x: lastX, y: walkedMs(start, t) / MIN };
});
};
// Every boundary falling inside the day, plus the day's two ends, is a
// corner of the step line. capTs (today only) ends it at "now".
const curveFor = (start, capTs) => {
const end = capTs != null ? capTs : start + DAY;
const end = capTs != null ? capTs : clockHourTs(start, 24);
const stops = new Set([start, end]);
for (const w of windows) {
if (w.end <= start || w.start >= end) continue;
@@ -3011,7 +3035,7 @@
};
const hourlyFor = (start) => {
const stops = [];
for (let h = 0; h <= 24; h++) stops.push(start + h * HOUR);
for (let h = 0; h <= 24; h++) stops.push(clockHourTs(start, h));
return pointsAt(start, stops);
};
const totalOf = (pts) => pts[pts.length - 1].y;
@@ -3255,9 +3279,9 @@
const curveFor = (start, capTs) => {
const pts = [];
for (let h = 0; h <= 24; h++) {
const to = start + h * HOUR;
const to = clockHourTs(start, h);
if (capTs != null && to >= capTs) {
pts.push({ x: (capTs - start) / HOUR, y: sleptMs(start, capTs) / HOUR });
pts.push({ x: clockHourOf(capTs, start), y: sleptMs(start, capTs) / HOUR });
break;
}
pts.push({ x: h, y: sleptMs(start, to) / HOUR });
+1
View File
@@ -1,4 +1,5 @@
[
{ "date": "2026-10-28", "text": "The Sleep trend and Walk trend charts handle the days the clocks change. Their hour axis counted hours from midnight rather than reading the clock, so on the Sunday the clocks go back the day’s line stopped at 11 pm and left out the last hour — taking it off that day’s total and off the average day built from it — and in spring the day ran on into 1 am the next morning. Today’s line also ended an hour away from the “now” marker on those days. Both charts now follow the clock, so every day runs from midnight to midnight whatever its length" },
{ "date": "2026-10-28", "text": "His age is no longer a day short for part of the year. Depending on when he was born, counting the days since his birthday could come up one short between the clocks going forward in spring and going back in autumn, and the week and month counts in “16 weeks (3 months and 3 weeks) old” ticked over a day late" },
{ "date": "2026-10-28", "text": "The When sleeping and When walking grids draw the days the clocks change correctly. On the Sunday the clocks go back the day is 25 hours long, and everything after 3 am was drawn an hour late — the evening sleep ran off the right-hand end of the row. In spring the 23-hour day stopped short of 24h instead. Each nap and walk now sits at the time the clock showed, lining up with the hour lines like every other day, and the “now” marker does the same on those days" },
{ "date": "2026-10-03", "text": "The charts can cover any stretch of dates, not just the last 7, 14 or 30 days. Tap Custom on the “Charts cover” row and pick a from and a to date — up to a year at a time, and the to date can be in the past, so you can look back at, say, May on its own and see how much he slept or walked then. Every chart and panel that follows the 7 / 14 / 30 buttons follows the custom dates too, and their headings and trend sentences name the dates (“Over May 1 – Jun 15, daily sleep is down about 2h”). On a long range the daily bars sit edge to edge and the axis shows dates instead of weekdays, and the When sleeping and When walking grids use thinner rows so a few months still fit on the screen. The “average day” line on the Sleep trend and Walk trend is averaged over the custom dates as well. Tapping 7d, 14d or 30d goes back to the rolling window, and Custom remembers the dates you picked last" },