skip to content

A Rails freight-logistics app counts each depot's bookings before its 17:00 local cut-off; how do you build those daily windows so non-UTC depots and DST days stay correct?

level: seniorimportance: should knowfreq 34%

answer

  1. the depot's zone, not Time.zone
  2. zone.today, then local 17:00
  3. 23- and 25-hour days
  4. 24.hours is not one day
  5. bounds reach SQL as UTC

basics

~20 s

Build each window's bounds as wall-clock times in the depot's own ActiveSupport::TimeZone (previous day 17:00 to today 17:00) and query with those TimeWithZone values; Active Record sends them as UTC, and DST days come out 23 or 25 hours long.

solid answer

~40 s

Store each depot's IANA zone name, resolve it with `ActiveSupport::TimeZone[...]`, take "today" from that zone with `zone.today`, and build both bounds as wall-clock 17:00 in that zone: `day.in_time_zone(zone).change(hour: 17)` and the same for `day - 1`. Query with an exclusive range, `where(booked_at: open...close)`; Active Record converts both bounds to UTC, and because each bound was computed from local wall-clock time, the window on a DST day is correctly 23 or 25 hours. The usual bugs are `Date.today` (server date), `Time.current` (app zone, not the depot's), `start + 24.hours` or a hard-coded offset (wrong on DST days), and `DATE(booked_at)` in SQL, which groups by the UTC calendar day. For a calendar day rather than a cut-off, `zone.today.in_time_zone(zone).all_day` or `Time.use_zone(depot.time_zone) { Date.current.all_day }` gives the right range.

code

ruby · 18 lines
ruby
class Depot < ApplicationRecord
  has_many :bookings

  def tz = ActiveSupport::TimeZone[time_zone]   # e.g. "America/Chicago"

  def cutoff_window(day = tz.today)
    close = day.in_time_zone(tz).change(hour: 17)
    open  = (day - 1).in_time_zone(tz).change(hour: 17)
    open...close
  end

  def bookings_before_cutoff(day = tz.today)
    bookings.where(booked_at: cutoff_window(day))
  end
end

depot.cutoff_window(Date.new(2026, 3, 8))
# => 2026-03-07 17:00:00.000000000 CST -06:00...2026-03-08 17:00:00.000000000 CDT -05:00  (23 hours)

go deeper

for a junior

Recall that 'today' depends on a zone, that Rails stores times in UTC, and that a local day is not always 24 hours long.

for a middle

Show how zone.today, in_time_zone and change(hour:) build bounds in a specific zone, and how all_day relates to Time.zone versus the receiver's zone.

for a senior

Diagnose the classic bugs — Date.today, fixed offsets, 24.hours, DATE() on UTC columns — and build windows that are 23 or 25 hours on DST days, with a testable day parameter.

for a principal

Decide where business days are defined: per depot, per customer or per contract. Make that zone part of the domain model so every report and job computes windows the same way.

## The requirement Each depot ships everything booked before **17:00 local time**. The daily report for a depot therefore covers **yesterday 17:00 to today 17:00 in the depot's zone**. Depots sit in several zones — Chicago, Rotterdam, Singapore — and the app's own `config.time_zone` is just one of them, or UTC. Bookings are stored in a `booked_at` datetime column, in UTC by default. ## Bugs that pass every weekday test 1. **`Date.today`** — the server process's date. On a UTC server, a Chicago depot's "today" flips at 18:00 or 19:00 local time. 2. **`Time.current` / `Date.current`** — the application zone (or the request user's zone), not the depot's. A Chicago-configured app computes Rotterdam's cut-off seven hours late. 3. **`start + 24.hours`** or a stored fixed offset such as `-6` — a cut-off-to-cut-off window is **23 hours** on the spring-forward day and **25 hours** on the fall-back day. Fixed arithmetic shifts every window after the change by an hour, and a fixed offset is wrong for half the year. 4. **`where("DATE(booked_at) = ?", day)`** — the database applies `DATE()` to the stored UTC value, so it groups by the **UTC** calendar day. 5. **An inclusive end bound at 23:59:59** — built by hand, it drops bookings in the last fractional second; Active Support's `end_of_day` uses 23:59:59.999999999, and an exclusive range avoids the question entirely. ## Building the window correctly 1. **Store the zone, not an offset.** Keep an IANA identifier (`"America/Chicago"`, `"Europe/Amsterdam"`) per depot and resolve it with `ActiveSupport::TimeZone[name]` (returns `nil` for an unknown name) or `Time.find_zone!` (raises). 2. **Ask the depot's zone for today.** `zone.today` uses the zone's current date, independent of the server and of `Time.zone`. 3. **Compute both bounds from wall-clock time.** `day.in_time_zone(zone)` is local midnight as a `TimeWithZone`; `.change(hour: 17)` moves to 17:00 and re-resolves the offset for that date, so each bound carries its own correct offset. 4. **Query with an exclusive range.** `where(booked_at: open...close)` becomes `booked_at >= ? AND booked_at < ?`, with both values quoted in UTC by Active Record. 5. **Take `day` as a parameter.** Passing the date in, rather than reading the clock inside, makes the DST days directly testable. On Sunday 8 March 2026 (US spring-forward), the Chicago window runs from 7 March 17:00 CST (23:00 UTC) to 8 March 17:00 CDT (22:00 UTC) — 23 hours, which is exactly right. ## Calendar days and all_day For "everything booked on this local date", `all_day` already does the work: - `Time#all_day` / `TimeWithZone#all_day` → `beginning_of_day..end_of_day` in the receiver's zone. - `Date#all_day` → the same, but via `Date#beginning_of_day`, which is `in_time_zone` — i.e. **`Time.zone`**, not the depot's zone. So write `zone.today.in_time_zone(zone).all_day`, or wrap the calculation in `Time.use_zone(depot.time_zone) { Date.current.all_day }`. Either gives a 23- or 25-hour range on DST days because both ends are computed as local wall-clock times. | Approach | Non-UTC depot | DST day | |---|---|---| | `Date.today.all_day` | wrong date near midnight, wrong zone | — | | `t = Time.current.beginning_of_day; t..t + 24.hours` | app zone only | off by one hour | | `zone.today.in_time_zone(zone).all_day` | correct | correct | | cut-off bounds via `change(hour: 17)` | correct | correct | ## Review checklist - No `Date.today`, `Time.now` or `Time.current` inside the window calculation; the depot's zone supplies "today". - No fixed offsets stored or computed; only IANA names. - Both bounds built from wall-clock values with `in_time_zone` and `change`, never by adding hours to one bound. - An exclusive range (`...`) for cut-off windows, so a booking at exactly 17:00 belongs to the next window only. - Tests covering a spring-forward and a fall-back date for at least one depot on each side of the Atlantic, since the US and the EU change on different Sundays. ## Grouping many days A month-long report grouped by local day cannot use `DATE(booked_at)` on UTC values. Either bucket in Ruby — `booked_at.in_time_zone(zone).to_date` — or convert in SQL with the database's own zone-conversion function using the depot's IANA name; the first is simpler and keeps DST rules in one place.

  • Why store a depot's IANA zone name instead of its UTC offset?
    An offset is valid only for part of the year: Chicago is -06:00 in winter and -05:00 in summer, and the switch dates differ between the US and Europe. An IANA name such as `America/Chicago` lets `ActiveSupport::TimeZone` apply the right rule for each date, including future rule changes shipped in tzinfo data.
  • Why does Date.current.all_day give the wrong range for a Rotterdam depot in an app configured for Chicago?
    `Date#all_day` builds its bounds through `in_time_zone`, which uses `Time.zone`. With Chicago as `Time.zone`, the range is Chicago midnight to Chicago midnight, seven hours off Rotterdam's day. Use `zone.today.in_time_zone(zone).all_day` or run it inside `Time.use_zone(depot.time_zone)`.
  • How do you make the DST-day behaviour testable without freezing the clock?
    Pass the date in: `cutoff_window(Date.new(2026, 3, 8))`. The method then has no hidden dependency on now, and a test can assert that the spring-forward window is 23 hours and the fall-back window 25 hours by subtracting the bounds.

  • beginning_of_day + 24.hours always covers exactly one local day
  • Storing each depot's UTC offset is enough to compute its cut-off
  • DATE(booked_at) in SQL groups rows by the depot's local day
  • Date.current.all_day uses the depot's zone if the depot has one
  • all_day ranges are always 24 hours long, even across DST