Skip to content

Commit e36b055

Browse files
Tiwasclaude
andcommitted
fix(circadian): correct phase anchor semantics
Anchor times now mark when each phase's values are reached, with transitions happening between anchors and a flat-night hold outside morning..night. Previously the transition from evening to night was spread across the entire sleep period, so deep-night values were only reached just before morning - the opposite of what users expect. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 89dbd2e commit e36b055

1 file changed

Lines changed: 9 additions & 10 deletions

File tree

no.tiwas.booleantoolbox/lib/CircadianProfile.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -130,31 +130,30 @@ function resolveAnchorMinutes(anchor, fallback, ctx, anchorKey) {
130130
return resolved;
131131
}
132132

133+
// Anchors mark when each phase's values are reached. Transitions happen
134+
// between anchors; before `morning` and after `night` we hold flat at night.
133135
function getSegment(nowMinutes, anchors, ctx) {
134136
const morning = resolveAnchorMinutes(anchors.morning, 420, ctx, 'morning');
135137
const day = resolveAnchorMinutes(anchors.day, 600, ctx, 'day');
136138
const evening = resolveAnchorMinutes(anchors.evening, 1140, ctx, 'evening');
137139
const night = resolveAnchorMinutes(anchors.night, 1380, ctx, 'night');
138140

139-
if (nowMinutes < morning) {
140-
const start = night - 1440;
141-
return { from: 'night', to: 'day', progress: (nowMinutes - start) / (morning - start), phase: 'night' };
141+
if (nowMinutes < morning || nowMinutes >= night) {
142+
return { from: 'night', to: 'night', progress: 0, phase: 'night' };
142143
}
143144

144145
if (nowMinutes < day) {
145-
const progress = (nowMinutes - morning) / (day - morning);
146+
const progress = (nowMinutes - morning) / Math.max(1, day - morning);
146147
return { from: 'night', to: 'day', progress, phase: progress < 0.5 ? 'night' : 'day' };
147148
}
148149

149150
if (nowMinutes < evening) {
150-
return { from: 'day', to: 'day', progress: 1, phase: 'day' };
151+
const progress = (nowMinutes - day) / Math.max(1, evening - day);
152+
return { from: 'day', to: 'evening', progress, phase: progress < 0.5 ? 'day' : 'evening' };
151153
}
152154

153-
if (nowMinutes < night) {
154-
return { from: 'day', to: 'evening', progress: (nowMinutes - evening) / (night - evening), phase: 'evening' };
155-
}
156-
157-
return { from: 'evening', to: 'night', progress: (nowMinutes - night) / ((morning + 1440) - night), phase: 'night' };
155+
const progress = (nowMinutes - evening) / Math.max(1, night - evening);
156+
return { from: 'evening', to: 'night', progress, phase: progress < 0.5 ? 'evening' : 'night' };
158157
}
159158

160159
function getPhaseValues(profile, phase) {

0 commit comments

Comments
 (0)