In Rails, why should application code call Time.current and Date.current rather than Time.now and Date.today?
answer
- two local zones in one process
- ENV['TZ'] vs config.time_zone
- Time.current returns a TimeWithZone
- Date.current is Time.zone.today
- dates split around midnight
basics
~20 sTime.now and Date.today use the server process's zone (ENV['TZ']); Time.current and Date.current use Time.zone, the app's configured or per-request zone. Mixing them shifts wall-clock times by the offset and, near midnight, gives a different date.
solid answer
~40 sA Rails process has two ideas of local time: Ruby's, taken from the operating system or `ENV['TZ']`, and the application's, `Time.zone`, which starts as `config.time_zone` ("UTC" unless you set it) and can be switched per request. `Time.current` returns `Time.zone.now`, an `ActiveSupport::TimeWithZone`, and `Date.current` returns `Time.zone.today`; `Time.now` and `Date.today` ignore `Time.zone` entirely. The instant is the same, so comparing two times still works, but anything that reads the wall clock — the date, the hour, `beginning_of_day` — can differ. On a UTC server running a Chicago app, `Date.today` is already tomorrow from 19:00 Chicago time in summer. Active Support's own helpers (`Date.yesterday`, `3.days.ago`) already go through `Date.current` and `Time.current`, so using them everywhere keeps the app on one zone.
code
ruby · 9 lines# config.time_zone = "Central Time (US & Canada)"; the server process runs with TZ=UTC
# Monday 5 October 2026, 20:30 in Chicago
Time.now # => 2026-10-06 01:30:00 +0000
Time.current # => 2026-10-05 20:30:00.000000000 CDT -05:00
Time.current.class # => ActiveSupport::TimeWithZone
Date.today # => Tue, 06 Oct 2026
Date.current # => Mon, 05 Oct 2026
t = Time.now
t == t.in_time_zone # => true (same instant, two presentations)go deeper
Recall the rule: Time.current and Date.current follow config.time_zone, Time.now and Date.today follow the server. Know that Time.current returns an ActiveSupport::TimeWithZone.
Explain the two zones in one process, why the instant is the same but the date can differ, and that Date.yesterday and 3.days.ago already go through Date.current and Time.current.
Show where the mismatch costs money: day-boundary reports, cut-off checks and CI runners in UTC versus laptops in local time. Propose a lint rule and Time.zone.parse for every string input.
Frame it as a codebase convention: one time source, enforced by linting and review, so zone handling is a property of the platform rather than of each developer's memory.
## Two clocks in one Rails process Every Rails process carries **two notions of "local time"**, and most time bugs in Rails apps come from mixing them: - **Ruby's process zone** — whatever the operating system says, or the `TZ` environment variable. Ruby's own `Time.now`, `Date.today` and `Time.local` use it. On most servers and containers it is UTC, while a developer laptop is usually set to the developer's own zone. - **The application zone, `Time.zone`** — an `ActiveSupport::TimeZone` object. At boot the `active_support.initialize_time_zone` initializer sets `Time.zone_default` from `config.time_zone`, which defaults to `"UTC"`; `Time.zone` returns a per-thread override if one is set (for example by `Time.use_zone` in a controller) and `Time.zone_default` otherwise. The two are independent. Setting `config.time_zone = "Central Time (US & Canada)"` does not change `ENV['TZ']`, and starting the server with `TZ=America/Chicago` does not change `Time.zone`. ## What each method returns | Call | Zone it reads | Returns | |---|---|---| | `Time.now` | process zone | Ruby `Time` | | `Time.current` | `Time.zone` | `ActiveSupport::TimeWithZone` (falls back to `Time.now` only when no zone is set at all) | | `Time.zone.now` | `Time.zone` | `ActiveSupport::TimeWithZone` | | `Date.today` | process zone | `Date` | | `Date.current` | `Time.zone` | `Date` (it is `Time.zone.today`) | | `DateTime.current` | `Time.zone` | `DateTime` | In a Rails app `Time.zone` is never empty, because `config.time_zone` always has a value, so `Time.current` and `Date.current` always use the application zone. Active Support builds on them: `Date.yesterday` and `Date.tomorrow` are relative to `Date.current`, and `Duration#ago` and `Duration#from_now` default to `Time.current`. ## Where the mismatch bites The **instant** is identical either way — a `Time` and a `TimeWithZone` for the same moment compare equal — so sorting and comparing timestamps rarely breaks. The damage is in anything that reads the **wall clock**: 1. **"Today".** With `config.time_zone` set to Chicago and the server on UTC, `Date.today` flips to the next day at 19:00 Chicago time in summer (18:00 in winter). A dashboard that shows "today's shipments" with `Date.today` shows tomorrow's empty list for the last five or six hours of every local day. 2. **Day and hour boundaries.** `Time.now.beginning_of_day` is midnight in the process zone; `Time.current.beginning_of_day` is midnight in the app zone. Queries built on the first are off by the zone offset. 3. **Formatting.** `Time.now.hour` and `Time.now.strftime(...)` show server time, so a page can say "booked at 14:00" when the user's clock said 09:00. 4. **Environment drift.** Code that uses `Time.now` passes on a laptop set to the app's zone and fails on a UTC CI runner or server — the classic "works on my machine" time bug. ## A worked example With `config.time_zone = "Central Time (US & Canada)"` and the server process in UTC, at 20:30 Chicago time on Monday 5 October 2026: - `Time.now` is 01:30 on 6 October, offset +00:00. - `Time.current` is 20:30 on 5 October, CDT, -05:00 — a `TimeWithZone`. - `Date.today` is Tuesday 6 October; `Date.current` is Monday 5 October. The first pair describe the same instant; the second pair are two different calendar days. ## Objections you will hear - **"Our servers and `config.time_zone` are both UTC, so it makes no difference."** True today, and only today: the first per-user `Time.use_zone`, a developer laptop in Berlin, or a change of `config.time_zone` for a regional launch makes every `Time.now` a latent bug. - **"`Time.now` is faster."** `Time.current` adds a zone lookup and wraps the result; the difference is negligible next to any database query, and correctness wins. - **"We only use it in tests."** Tests that build expectations with `Date.today` while the code uses `Date.current` fail only between the two midnights — the flakiest kind of failure, usually seen first on a CI runner in UTC. - **"`Time.now.utc` fixes it."** It removes the dependence on the server's zone, but "today" and "this hour" are business questions asked in the app's zone, not in UTC. ## Habits that keep an app on one zone - Use **`Time.current`** (or `Time.zone.now`) for "now" and **`Date.current`** (or `Time.zone.today`) for "today" in models, controllers, jobs and views. - Build relative times with Active Support helpers — `2.hours.ago`, `Date.yesterday` — which already go through `Time.current`. - Parse strings with **`Time.zone.parse`**, not `Time.parse`, so they are read in the app zone. - Treat any remaining `Time.now` or `Date.today` in application code as a review comment; rubocop-rails ships the `Rails/TimeZone` and `Rails/Date` cops to flag them. - Remember that **storage is a separate setting**: Active Record writes timestamps in UTC by default whichever "now" you used, so this choice affects reading and computing, not what lands in the column. `Time.now` is not wrong as Ruby — it is simply the operating system's answer to a question a Rails app means to ask of `Time.zone`.
- If Time.now and Time.current describe the same instant, when is mixing them actually harmless?When you only compare or subtract instants: `Time.now > record.created_at` or `Time.current - started_at` give the same result either way, because comparisons use the UTC instant. It breaks as soon as you read wall-clock parts — the date, the hour, `beginning_of_day`, `strftime` — or build a calendar range from them.
- What does Time.current return in a plain Ruby script that requires Active Support but never sets a zone?`Time.current` checks `Time.zone`; with no `Time.zone_default` and no per-thread zone it is `nil`, so `Time.current` falls back to `Time.now` and returns a plain `Time` in the process zone. In a Rails app this never happens, because `config.time_zone` ("UTC" by default) always sets `Time.zone_default` at boot.
- Does switching code from Time.now to Time.current change what Active Record stores in a created_at column?No. With the default `config.active_record.default_timezone = :utc`, Active Record converts any time to UTC before writing it, so both calls store the same value. The switch matters for what you compute and display — dates, day boundaries, formatted hours — not for the stored instant.
- Setting config.time_zone also changes what Time.now returns
- Date.today and Date.current always return the same date on a server
- Time.current returns a plain Ruby Time in the server's zone
- Time.now and Time.current are different instants, so comparisons break
- Starting the server with TZ set to the app's zone makes Time.zone match