Skip to content

fix: translate start_of_day/2 for time zone expressions, timestamps and dates - #293

Open
agustin2606 wants to merge 1 commit into
ash-project:mainfrom
agustin2606:fix/start-of-day-time-zone
Open

agustin2606 wants to merge 1 commit into
ash-project:mainfrom
agustin2606:fix/start-of-day-time-zone

Conversation

@agustin2606

Copy link
Copy Markdown

Fixes #289

start_of_day/2 in the data layer now returns the same instant as Ash's own evaluation, for any time zone expression and for timestamp, timestamptz and date values.

Problem

Only a timestamptz value with a literal time zone gave the right answer.

# created_at: 2026-06-16 02:00Z, a timestamp column (the default for :utc_datetime)
Post
|> Ash.Query.calculate(:start, :utc_datetime, expr(start_of_day(created_at, "America/New_York")))
|> Ash.read_one!()
# before: 2026-06-15 20:00:00Z, expected: 2026-06-15 04:00:00Z

expr(start_of_day(datetime, venue.time_zone))
# before: Ash.Error.Invalid ("Invalid filter value `nil`")

Cause

In the StartOfDay clause of AshSql.Expr, the time zone was always pinned as a parameter, so an expression like a column was never translated. Literal DateTime/Date values used the UTC date instead of the date in the time zone. timestamp and date values went through timezone(zone, date_trunc('day', timezone(zone, value))), which is only right for timestamptz.

Fix

  • Literal DateTime/Date with a literal zone: computed with Ash.Query.Function.StartOfDay.evaluate/1.
  • The time zone is translated as an expression.
  • Values whose Ash type is stored as utc_datetime/naive_datetime (cast to timestamp) are converted with timezone('UTC', value) first; dates are cast to timestamp and placed in the zone.
  • timestamptz and untyped values (such as fragment("now()")) keep the existing SQL.

Testing

ash_sql has no database tests, so the data layer tests are in ash-project/ash_postgres#931: 7 cases from the issue's table plus a date. Without this change 5 fail; with it, they and the rest of calculation_test.exs pass.

Contributor checklist

Leave anything that you believe does not apply unchecked.

  • I accept the AI Policy, or AI was not used in the creation of this PR.
  • Bug fixes include regression tests
  • Chores
  • Documentation changes
  • Features include unit/acceptance tests
  • Refactoring
  • Update dependencies

…nd dates

The time zone was always pinned as a parameter, so a zone read from a
column failed. Literal DateTime/Date values took the UTC date instead
of the date in the time zone. `timestamp` and `date` values went through
the formula meant for `timestamptz`, which applied the offset the wrong
way.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

start_of_day/2 with a time zone raises or returns the wrong instant

1 participant