Skip to content

Matching work to the right shift

The rule

Which scheduled shift a piece of work is decides a lot: the job and cost center the shift carries, whether the person was late, what rounding snaps to. The engine’s own rule is plain: work is the shift’s it starts within (or up to shifts.early before), the first of several. With shifts back to back, or a person clocking in early for the later one, that picks the wrong shift. Companies usually say instead: work belongs to the shift whose start is nearest, within a window either side (30 minutes early to 30 minutes late); outside it, to no shift.

Who uses it

Employers running overlapping or back-to-back shifts (plants with day, swing and night crews, hospitals, call centers), and anyone whose lateness rules or shift-based job costing must look at the shift the person actually came for.

How the recipe does it

Configuration only: a package (package.toml) of one layer. The standard match step, in the match stage, runs on each workday’s segments before their labor context is resolved; the standard late step then holds the work to the shift matched.

[[layer]]
id = "matching"
[[layer.step]]
id = "match"
uses = "standard/match"
params = { early = "30m", late = "30m", prefer = "nearest" }
[[layer.step]]
id = "late"
uses = "standard/late"
params = { grace = "5m" }

A match step sees each segment (ctx.segments: times, kinds, values, shift, the engine’s own pick). The shifts to choose among are those of the days it asks of (ctx:day(date).shifts, each id, start, end, values): the standard step asks of the segment’s day, the day after and each day before it that the calendar has, since a shift of more than a day has work on the days after its own. It calls ctx:match(id, shift_id, why), or with no shift id for none. A change is stamped. The shift chosen gives the work its context and is the one every later step sees (s.shift).

Parameters

ParameterMeaning
earlyHow long before a shift’s start work may start and be the shift’s ("0s").
lateHow long after a shift’s start work may start and be the shift’s (default: until its end).
preferOf several: "nearest" start, or "first" (the engine’s own).

What the scenarios show

  • the-nearest-shift-wins: 14:50 is the swing shift’s (15:00), not the day shift’s it falls in; 06:45 is the day shift’s. Each takes its shift’s job, and no one is late.
  • too-far-from-any-shift: two hours early and an hour into the shift are no shift’s; 20 minutes in is the shift’s, and late.

This is a common way companies match punches to schedules; check the rules where you operate. Matching decides which shift work is counted against, never whether it is paid.

Get the recipe

The recipe’s package and its scenarios are in the example packages repository: attendance/shift_matching. Test it, or your own copy of it, with hourctl attend test, and publish it with hourctl attend pkg publish.

The policy package

The package’s layers, with the standard steps each one runs and their settings:

shift_matching/package.toml
name = "shift_matching"
version = "1.0.0"
description = "Work is matched to the shift whose start is nearest, within a window either side, so an early arrival for the swing shift is not a late arrival for the day shift."
# Work is matched to the nearest scheduled shift it starts within 30
# minutes of; then starting a shift more than 5 minutes late is late.
[[layer]]
id = "matching"
[[layer.step]]
id = "match"
uses = "standard/match"
params = { early = "30m", late = "30m", prefer = "nearest" }
[[layer.step]]
id = "late"
uses = "standard/late"
params = { grace = "5m" }

Scenarios

Each scenario gives a person’s punches and setup and the timecard they must produce, worked out with this package adopted (the format).

Two shifts back to back: days 07:00 to 15:00 (job packing) and swing 15:00 to 23:00 (job shipping). Monday the person, on swing, clocks in at 14:50: the engine's own rule takes the first shift that holds it, days, and would call them 7 hours 50 minutes late and put the work on packing. Matched to the nearest start within 30 minutes either way, it is the swing shift's: on shipping, on time. Tuesday's 06:45 in, 15 minutes early, is the day shift's
scenarios/the-nearest-shift-wins.toml
description = "Two shifts back to back: days 07:00 to 15:00 (job packing) and swing 15:00 to 23:00 (job shipping). Monday the person, on swing, clocks in at 14:50: the engine's own rule takes the first shift that holds it, days, and would call them 7 hours 50 minutes late and put the work on packing. Matched to the nearest start within 30 minutes either way, it is the swing shift's: on shipping, on time. Tuesday's 06:45 in, 15 minutes early, is the day shift's"
punch = [
{ at = "2026-10-05 14:50", kind = "in" }, { at = "2026-10-05 23:00", kind = "out" },
{ at = "2026-10-06 06:45", kind = "in" }, { at = "2026-10-06 15:00", kind = "out" },
]
[policy]
zone = "America/Chicago"
[[shift_definition]]
id = "days"
start = "07:00"
end = "15:00"
days = ["monday", "tuesday"]
values = { job = "packing" }
[[shift_definition]]
id = "swing"
start = "15:00"
end = "23:00"
days = ["monday", "tuesday"]
values = { job = "shipping" }
[expect]
week = "2026-10-05"
allocations = [
"2026-10-05 14:50-23:00 work 8.16 job=shipping",
"2026-10-06 06:45-15:00 work 8.25 job=packing",
]
stamps = [
"2026-10-05 14:50-23:00 work: match [[email protected]/matching rev 1]: matched to shift swing@2026-10-05: within the shift's window",
"2026-10-06 06:45-15:00 work: match [[email protected]/matching rev 1]: matched to shift days@2026-10-06: within the shift's window",
]
Work starting more than 30 minutes before a shift, or more than 30 minutes after its start, is no shift's: Monday's 05:00 start (2 hours early, overtime called in) and Tuesday's 08:00 start (an hour into the day shift, a person covering) are matched to none, so they take no shift's job and no late rule applies. Wednesday's 07:20 is 20 minutes into the shift: matched, and late
scenarios/too-far-from-any-shift.toml
description = "Work starting more than 30 minutes before a shift, or more than 30 minutes after its start, is no shift's: Monday's 05:00 start (2 hours early, overtime called in) and Tuesday's 08:00 start (an hour into the day shift, a person covering) are matched to none, so they take no shift's job and no late rule applies. Wednesday's 07:20 is 20 minutes into the shift: matched, and late"
punch = [
{ at = "2026-10-05 05:00", kind = "in" }, { at = "2026-10-05 13:00", kind = "out" },
{ at = "2026-10-06 08:00", kind = "in" }, { at = "2026-10-06 15:00", kind = "out" },
{ at = "2026-10-07 07:20", kind = "in" }, { at = "2026-10-07 15:00", kind = "out" },
]
[policy]
zone = "America/Chicago"
[[shift_definition]]
id = "days"
start = "07:00"
end = "15:00"
days = ["monday", "tuesday", "wednesday"]
values = { job = "packing" }
[expect]
week = "2026-10-05"
allocations = [
"2026-10-05 05:00-13:00 work 8.00",
"2026-10-06 08:00-15:00 work 7.00",
"2026-10-07 07:20-15:00 work 7.66 job=packing",
]
exceptions = [{ kind = "late", at = "2026-10-07 07:20" }]