taskbar-focus 0.5.0

A tiny Pomodoro/focus timer that lives in the Windows tray and toggles Do Not Disturb
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
//! The PC lock engine.
//!
//! Pure, like `timer`: it is given the current time, the state of the Windows
//! session and the schedule, and returns what should happen. It never calls
//! Windows, which is what makes the rules testable without locking a PC.
//!
//! One day's lock is an [`Occurrence`]. It is pending until its first lock
//! attempt, then has a locked period, and is then done. Vocabulary is defined
//! in `GLOSSARY.md`.

mod occurrence;
pub mod settings;
pub mod store;
mod view;

#[cfg(test)]
mod tests;

pub use occurrence::{Moment, Zone};
pub use view::{Action, Card, CardKind, Status};

use occurrence::{AwakeCountdown, Countdown, Fixed, Occurrence, Period, Rules};
use serde::{Deserialize, Serialize};
use settings::{weekday, Day, LockSettings, MINUTE};

/// Length of the warning after a late return or a start during a locked period.
const RETURN_WARNING: i64 = MINUTE;
/// How long Windows may take to confirm a lock.
const CONFIRM_WITHIN: i64 = 5_000;
const RETRY_EVERY: i64 = 30_000;
/// How long to keep trying when there is no locked period.
const ZERO_PERIOD_RETRIES: i64 = MINUTE;
/// A longer silence between two calls means the PC slept or the app stalled.
const CONTINUOUS_WITHIN: i64 = 5_000;
/// How many occurrences are remembered. They are dropped by number and not by
/// age, so that moving the clock can neither lose one nor bring one back.
const KEEP: usize = 40;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum LockState {
    Unknown,
    Locked,
    Unlocked,
}

/// What is known about the Windows session. Connection and lock are separate.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Session {
    pub lock: LockState,
    pub connected: bool,
}

impl Session {
    fn locked(self) -> bool {
        self.lock == LockState::Locked
    }

    /// Someone may be using the PC. An unknown lock state counts as unlocked.
    fn present(self) -> bool {
        self.connected && !self.locked()
    }
}

/// Something the caller must do.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Effect {
    /// Pause focus, release Do Not Disturb and lock the PC.
    Lock,
    /// A lock took effect on a PC that was already locked.
    AlreadyLocked,
    /// A line for the activity log.
    Log(String),
}

/// Everything that survives a restart.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct Saved {
    /// Do Not Disturb stays released until the timer is started or resumed.
    pub dnd_suppressed: bool,
    /// The schedule as last accepted, so days whose cutoff passed while the app
    /// was closed are judged by the settings they had.
    accepted: LockSettings,
    occurrences: Vec<Occurrence>,
}

impl Saved {
    pub fn accepted(&self) -> &LockSettings {
        &self.accepted
    }
}

/// How a locked period is being enforced right now. Never saved.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Enforcer {
    Idle,
    /// The app started during a locked period.
    Startup,
    /// Too little of the period was left at startup to lock again.
    Exempt,
    /// Waiting for Windows to confirm a lock begun at this awake time.
    Attempting {
        since: i64,
        known_unlocked: bool,
    },
    Retry {
        at: i64,
    },
    Relock {
        until: i64,
    },
}

/// The time since the previous call to [`Engine::advance`].
#[derive(Clone, Copy)]
struct Step {
    now: Moment,
    session: Session,
    /// The PC was awake and the app running throughout.
    continuous: bool,
    /// Continuous, with someone present at both ends.
    stayed: bool,
}

impl Step {
    /// Something `late_by` ms ago was witnessed as it happened.
    fn witnessed(&self, late_by: i64) -> bool {
        self.continuous && late_by <= CONTINUOUS_WITHIN
    }
}

pub struct Engine {
    saved: Saved,
    enforcer: Enforcer,
    last: Option<(Moment, bool)>,
    /// Awake time at which a confirmation began holding the countdown.
    held: Option<i64>,
    countdown_dismissed: bool,
    /// The failure was reported since the last confirmed lock.
    failure_reported: bool,
    failure_dismissed: bool,
    dirty: bool,
}

impl Engine {
    /// Restore the engine. Occurrences are brought up to date under the last
    /// accepted schedule before `settings` apply.
    pub fn start(
        saved: Saved,
        settings: &LockSettings,
        now: Moment,
        zone: &dyn Zone,
    ) -> (Engine, Vec<Effect>) {
        let mut engine = Engine {
            saved,
            enforcer: Enforcer::Idle,
            last: None,
            held: None,
            countdown_dismissed: false,
            failure_reported: false,
            failure_dismissed: false,
            dirty: true,
        };
        let mut out = Vec::new();
        engine.saved.accepted.clamp();
        for o in engine.saved.occurrences.iter_mut() {
            o.sanitize(now);
        }
        engine.finish_zero_periods(&mut out);
        engine.prepare(now, zone);
        engine.update_periods(now);
        if engine.in_period() {
            engine.enforcer = Enforcer::Startup;
        }
        engine.apply_settings(settings, now, zone);
        (engine, out)
    }

    /// With no locked period, a restart cannot tell whether a lock begun before
    /// it happened, so it is never repeated.
    fn finish_zero_periods(&mut self, out: &mut Vec<Effect>) {
        for o in self.saved.occurrences.iter_mut() {
            if o.active() && o.rules.locked == 0 {
                o.done = true;
                out.push(Effect::Log(
                    "lock: the outcome of a lock begun before this start is unknown".into(),
                ));
            }
        }
    }

    /// Mark the state as unsaved after a failed write.
    pub fn save_again(&mut self) {
        self.dirty = true;
    }

    pub fn saved(&self) -> &Saved {
        &self.saved
    }

    pub fn set_dnd_suppressed(&mut self, on: bool) {
        self.dirty |= self.saved.dnd_suppressed != on;
        self.saved.dnd_suppressed = on;
    }

    /// Whether the state needs saving. Asking clears the flag.
    pub fn take_dirty(&mut self) -> bool {
        std::mem::take(&mut self.dirty)
    }

    /// A locked period is being counted down.
    pub fn in_period(&self) -> bool {
        self.saved.occurrences.iter().any(Occurrence::active)
    }

    fn find(&self, day: Day) -> Option<&Occurrence> {
        self.saved.occurrences.iter().find(|o| o.day == day)
    }

    /// Create the occurrences near today, and fix those whose cutoff passed.
    fn prepare(&mut self, now: Moment, zone: &dyn Zone) {
        let today = zone.day_of(now.wall);
        for day in today - 2..=today + 1 {
            if self.saved.accepted.day(weekday(day)).enabled && self.find(day).is_none() {
                let o = Occurrence::new(day, &self.saved.accepted);
                self.saved.occurrences.push(o);
                self.dirty = true;
            }
        }
        self.prune();

        for o in self.saved.occurrences.iter_mut() {
            let passed = o.cutoff_instant(zone) <= now.wall;
            if o.pending() && o.fixed.is_none() && passed && !o.missed(now, zone) {
                o.fixed = Some(Fixed::new(o.lock_instant(zone), now));
                self.dirty = true;
            }
        }
    }

    /// Forget the oldest occurrences beyond [`KEEP`].
    fn prune(&mut self) {
        let all = &mut self.saved.occurrences;
        if all.len() <= KEEP {
            return;
        }
        all.sort_by_key(|o| o.day);
        let excess = all.len() - KEEP;
        let mut index = 0;
        all.retain(|o| {
            index += 1;
            index > excess || o.active()
        });
        self.dirty = true;
    }

    /// Accept new settings. Returns true when a change does not apply to the
    /// coming lock, which the user must be told.
    pub fn apply_settings(&mut self, new: &LockSettings, now: Moment, zone: &dyn Zone) -> bool {
        self.prepare(now, zone);
        let old = std::mem::replace(&mut self.saved.accepted, new.clone());
        if old == *new {
            return false;
        }
        self.dirty = true;
        let mut deferred = false;
        let today = zone.day_of(now.wall);

        for day in today - 2..=today + 1 {
            if new.day(weekday(day)).enabled && self.find(day).is_none() {
                let mut o = Occurrence::new(day, new);
                o.cancelled = o.cutoff_instant(zone) <= now.wall;
                deferred |= o.cancelled && day >= today;
                self.saved.occurrences.push(o);
            }
        }
        for o in self.saved.occurrences.iter_mut() {
            deferred |= revise(o, &old, new, now, zone);
        }
        self.prepare(now, zone);
        deferred
    }

    /// Move everything forward to `now`.
    pub fn advance(&mut self, now: Moment, session: Session, zone: &dyn Zone) -> Vec<Effect> {
        let mut out = Vec::new();
        self.prepare(now, zone);
        self.update_periods(now);

        let (continuous, was_present) = match self.last {
            Some((before, present)) => (
                before.sign_in == now.sign_in
                    && (0..=CONTINUOUS_WITHIN).contains(&(now.tick - before.tick)),
                present,
            ),
            None => (false, false),
        };
        let step = Step {
            now,
            session,
            continuous,
            stayed: continuous && was_present && session.present(),
        };
        for i in 0..self.saved.occurrences.len() {
            if self.saved.occurrences[i].pending() {
                self.advance_pending(i, step, zone, &mut out);
            }
        }
        self.enforce(step, &mut out);
        self.last = Some((now, session.present()));
        out
    }

    fn update_periods(&mut self, now: Moment) {
        for o in self.saved.occurrences.iter_mut().filter(|o| o.active()) {
            let Some(p) = o.period.as_mut() else { continue };
            p.left.update(now);
            if let Some(s) = p.startup.as_mut() {
                s.update(now);
            }
            if !p.left.running() || (o.rules.locked == 0 && p.confirmed) {
                o.done = true;
                self.dirty = true;
            }
        }
    }

    fn advance_pending(&mut self, i: usize, step: Step, zone: &dyn Zone, out: &mut Vec<Effect>) {
        let Step { now, session, .. } = step;
        let o = &mut self.saved.occurrences[i];
        if let Some(d) = o.late.as_mut() {
            d.update(now);
        }
        if let Some(d) = o.late_short.as_mut() {
            d.update(now);
        }
        if !session.present() && o.late_short.take().is_some() {
            self.dirty = true;
        }
        let late_by = -o.remaining(now, zone);
        if late_by < 0 {
            return;
        }
        if let Some(short) = o.late_short {
            if !short.running() {
                self.attempt(i, step, out);
            }
            return;
        }
        let over = late_by > o.rules.return_window || !session.present();
        match o.late {
            None if step.witnessed(late_by) && session.locked() => {
                self.already_locked(i, late_by, now, out)
            }
            None if step.witnessed(late_by) && session.connected => self.attempt(i, step, out),
            None if over => {}
            None => {
                o.late = Some(Countdown::new(RETURN_WARNING, now));
                self.countdown_dismissed = false;
                self.dirty = true;
            }
            Some(d) if d.running() => {}
            Some(d) if step.witnessed(-d.remaining) && session.locked() => {
                self.already_locked(i, -d.remaining, now, out)
            }
            Some(d) if step.witnessed(-d.remaining) && step.stayed => self.attempt(i, step, out),
            Some(_) if over => {}
            Some(_) => {
                o.late_short = Some(AwakeCountdown::new(o.rules.relock_delay, now));
                self.countdown_dismissed = false;
                self.dirty = true;
            }
        }
    }

    /// Begin an occurrence's locked period with a lock attempt.
    fn attempt(&mut self, i: usize, step: Step, out: &mut Vec<Effect>) {
        let o = &mut self.saved.occurrences[i];
        let length = match o.rules.locked {
            0 => ZERO_PERIOD_RETRIES,
            locked => locked,
        };
        o.period = Some(Period {
            confirmed: false,
            left: Countdown::new(length, step.now),
            startup: None,
        });
        o.late_short = None;
        self.failure_reported = false;
        self.lock_now(step, out);
    }

    /// Begin a locked period on a PC seen locked when the lock was due.
    fn already_locked(&mut self, i: usize, late_by: i64, now: Moment, out: &mut Vec<Effect>) {
        let o = &mut self.saved.occurrences[i];
        o.period = Some(Period {
            confirmed: true,
            left: Countdown::new(o.rules.locked - late_by, now),
            startup: None,
        });
        self.dirty = true;
        out.push(Effect::AlreadyLocked);
        self.update_periods(now);
    }

    fn lock_now(&mut self, step: Step, out: &mut Vec<Effect>) {
        self.enforcer = Enforcer::Attempting {
            since: step.now.awake,
            known_unlocked: step.session.lock == LockState::Unlocked,
        };
        self.held = None;
        self.dirty = true;
        if !out.contains(&Effect::Lock) {
            out.push(Effect::Lock);
        }
    }

    /// Keep the PC locked while any locked period lasts.
    fn enforce(&mut self, step: Step, out: &mut Vec<Effect>) {
        if !self.in_period() {
            self.enforcer = Enforcer::Idle;
            self.held = None;
            return;
        }
        if step.session.locked() {
            self.confirm_lock(step.now);
            return;
        }
        if !step.session.connected {
            self.enforcer = Enforcer::Idle;
            return;
        }
        if self.held.is_some() {
            return;
        }
        let awake = step.now.awake;
        match self.enforcer {
            Enforcer::Idle => self.begin_relock(awake),
            Enforcer::Exempt => {}
            Enforcer::Attempting {
                since,
                known_unlocked,
            } if awake - since >= CONFIRM_WITHIN => {
                if known_unlocked && !self.failure_reported {
                    self.failure_reported = true;
                    self.failure_dismissed = false;
                    out.push(Effect::Log(
                        "lock: Windows did not lock; retrying every 30 seconds".into(),
                    ));
                }
                self.enforcer = Enforcer::Retry {
                    at: since + RETRY_EVERY,
                };
            }
            Enforcer::Retry { at } if awake >= at => self.lock_now(step, out),
            Enforcer::Relock { until } if awake >= until => self.lock_now(step, out),
            Enforcer::Attempting { .. } | Enforcer::Retry { .. } | Enforcer::Relock { .. } => {}
            Enforcer::Startup => self.enforce_startup(step, out),
        }
    }

    fn confirm_lock(&mut self, now: Moment) {
        for o in self.saved.occurrences.iter_mut().filter(|o| o.active()) {
            if let Some(p) = o.period.as_mut() {
                self.dirty |= !p.confirmed;
                p.confirmed = true;
            }
        }
        self.enforcer = Enforcer::Idle;
        self.held = None;
        self.failure_reported = false;
        self.update_periods(now);
    }

    fn begin_relock(&mut self, awake: i64) {
        let delay = self
            .saved
            .occurrences
            .iter()
            .filter(|o| o.active())
            .map(|o| o.rules.relock_delay)
            .min()
            .unwrap_or(0);
        self.enforcer = Enforcer::Relock {
            until: awake + delay,
        };
        self.countdown_dismissed = false;
    }

    /// The one warning given when the app starts during a locked period.
    fn enforce_startup(&mut self, step: Step, out: &mut Vec<Effect>) {
        let Some(period) = self.longest_period() else {
            return;
        };
        match period.startup {
            None if period.left.remaining <= RETURN_WARNING => self.enforcer = Enforcer::Exempt,
            None => {
                period.startup = Some(Countdown::new(RETURN_WARNING, step.now));
                self.dirty = true;
            }
            Some(s) if s.running() => {}
            Some(_) if step.stayed => self.lock_now(step, out),
            Some(_) => self.begin_relock(step.now.awake),
        }
    }

    fn longest_period(&mut self) -> Option<&mut Period> {
        self.saved
            .occurrences
            .iter_mut()
            .filter(|o| o.active())
            .filter_map(|o| o.period.as_mut())
            .max_by_key(|p| p.left.remaining)
    }

    /// Hold the countdown while the user is asked to confirm ending the period,
    /// if it may be ended.
    pub fn hold(&mut self, now: Moment) -> bool {
        if self.can_end() {
            self.held = Some(now.awake);
        }
        self.held.is_some()
    }

    /// Continue the countdown after a declined confirmation.
    pub fn release(&mut self, now: Moment) {
        let Some(since) = self.held.take() else {
            return;
        };
        let by = now.awake - since;
        match &mut self.enforcer {
            Enforcer::Relock { until } => *until += by,
            Enforcer::Retry { at } => *at += by,
            Enforcer::Startup => {
                if let Some(s) = self.longest_period().and_then(|p| p.startup.as_mut()) {
                    s.extend(by);
                }
            }
            _ => {}
        }
    }

    /// A strict lock's cutoff has passed and its locked period has not ended.
    pub fn exit_refused(&self, now: Moment, zone: &dyn Zone) -> bool {
        self.saved.occurrences.iter().any(|o| {
            let waiting = o.pending() && o.fixed.is_some() && !o.missed(now, zone);
            o.rules.day.strict && (o.active() || waiting)
        })
    }

    /// The lock that Postpone and Skip act on: the earliest whose original date
    /// or cutoff is today, or whose cutoff warning has begun, and whose cutoff
    /// has not passed.
    fn adjustable(&self, now: Moment, zone: &dyn Zone) -> Option<usize> {
        let today = zone.day_of(now.wall);
        (0..self.saved.occurrences.len())
            .filter(|&i| {
                let o = &self.saved.occurrences[i];
                let cutoff = o.cutoff_instant(zone);
                o.pending()
                    && o.fixed.is_none()
                    && cutoff > now.wall
                    && (o.day <= today
                        || zone.day_of(cutoff) == today
                        || cutoff - now.wall <= o.rules.warn_before_cutoff)
            })
            .min_by_key(|&i| self.saved.occurrences[i].lock_instant(zone))
    }

    /// [`Self::adjustable`], only if it is the lock the user was shown.
    fn adjustable_on(&self, day: Option<Day>, now: Moment, zone: &dyn Zone) -> Option<usize> {
        self.adjustable(now, zone)
            .filter(|&i| day.map_or(true, |d| d == self.saved.occurrences[i].day))
    }

    /// Postpone the coming lock, or only `day`'s. False when it is too late or
    /// no postponement is left.
    pub fn postpone(&mut self, day: Option<Day>, now: Moment, zone: &dyn Zone) -> bool {
        let Some(i) = self.adjustable_on(day, now, zone) else {
            return false;
        };
        let o = &mut self.saved.occurrences[i];
        if o.postponements_left() == 0 {
            return false;
        }
        o.postponed += o.rules.postpone;
        o.postponements += 1;
        o.lock_dismissed = false;
        self.dirty = true;
        true
    }

    /// Skip `day`'s lock before its cutoff.
    pub fn skip(&mut self, day: Option<Day>, now: Moment, zone: &dyn Zone) -> bool {
        let Some(i) = self.adjustable_on(day, now, zone) else {
            return false;
        };
        self.saved.occurrences[i].skipped = true;
        self.dirty = true;
        true
    }

    /// Skip a normal day's lock from its late-return warning.
    pub fn skip_late(&mut self) -> bool {
        let late = |o: &&mut Occurrence| o.warning_of_late_return() && !o.rules.day.strict;
        let Some(o) = self.saved.occurrences.iter_mut().find(late) else {
            return false;
        };
        o.skipped = true;
        self.dirty = true;
        true
    }

    /// Whether the locked period may be ended by hand.
    pub fn can_end(&self) -> bool {
        let strict = |o: &Occurrence| o.active() && o.rules.day.strict;
        self.in_period() && !self.saved.occurrences.iter().any(strict)
    }

    pub fn end_period(&mut self) -> bool {
        if !self.can_end() {
            return false;
        }
        for o in self.saved.occurrences.iter_mut().filter(|o| o.active()) {
            o.done = true;
        }
        self.enforcer = Enforcer::Idle;
        self.held = None;
        self.dirty = true;
        true
    }
}

/// Apply changed settings to one occurrence. Returns true when a change does
/// not apply to it.
fn revise(
    o: &mut Occurrence,
    old: &LockSettings,
    new: &LockSettings,
    now: Moment,
    zone: &dyn Zone,
) -> bool {
    let (was, is) = (old.day(weekday(o.day)), new.day(weekday(o.day)));
    let wanted = Rules::from(new, o.day);
    let today = zone.day_of(now.wall);
    let moved = is.enabled && (!was.enabled || was.time != is.time);
    if o.done {
        return moved && o.day >= today;
    }
    if o.missed(now, zone) {
        o.cancelled |= !is.enabled && !o.immutable();
        return moved && o.day >= today;
    }
    if o.immutable() {
        return o.rules != wanted;
    }
    if !is.enabled {
        o.cancelled = true;
        return false;
    }
    let mut fresh = o.clone();
    fresh.rules = wanted.clone();
    fresh.postponed = 0;
    fresh.cancelled = false;
    let too_late = o.fixed.is_some() || fresh.cutoff_instant(zone) <= now.wall;

    if o.cancelled {
        if !moved {
            return false;
        }
        if too_late {
            return o.day >= today;
        }
        fresh.skipped = false;
        *o = fresh;
        return false;
    }
    if was.time != is.time && !too_late {
        *o = fresh;
        return false;
    }
    let kept = o.clone();
    o.rules = wanted;
    o.rules.day.time = kept.rules.day.time;
    let cutoff_into_past = o.fixed.is_none() && o.cutoff_instant(zone) <= now.wall;
    if cutoff_into_past {
        o.rules.cutoff = kept.rules.cutoff;
    }
    was.time != is.time || cutoff_into_past
}