Skip to content

save() does not update a utc: true date column when the process runs west of UTC #12945

Closed

Description

@maxymlyskov

Issue description

save() does not write a changed { type: "date", utc: true } column when the process runs west of UTC

Expected Behavior

Loading an entity, setting its utc: true date column to new Date(Date.UTC(2025, 5, 1)) and calling save() stores 2025-06-01, the same as update() does with that value.

Actual Behavior

With TZ=America/New_York, save() sends no UPDATE and the column keeps 2025-05-31. update() with the same Date writes 2025-06-01. With TZ=UTC or TZ=Asia/Tokyo, save() sends the UPDATE.

Output of the script below, the same on better-sqlite3 and postgres:

TZ=America/New_York stored 2025-05-31; save(utcDate = 2025-06-01T00:00Z) sent 0 UPDATE -> db 2025-05-31; control update() -> db 2025-06-01
TZ=UTC              stored 2025-05-31; save(utcDate = 2025-06-01T00:00Z) sent 1 UPDATE -> db 2025-06-01; control update() -> db 2025-06-01
TZ=Asia/Tokyo       stored 2025-05-31; save(utcDate = 2025-06-01T00:00Z) sent 1 UPDATE -> db 2025-06-01; control update() -> db 2025-06-01

Steps to reproduce

@Entity()
class Event {
    @PrimaryGeneratedColumn()
    id: number

    @Column({ type: "date", utc: true })
    utcDate: Date | string
}

// run with TZ=America/New_York
const repo = dataSource.getRepository(Event)
const { id } = await repo.save({ utcDate: "2025-05-31" })

const event = await repo.findOneByOrFail({ id })
event.utcDate = new Date(Date.UTC(2025, 5, 1))
await repo.save(event) // no UPDATE is sent

const row = await repo.findOneByOrFail({ id })
console.log(row.utcDate) // "2025-05-31", expected "2025-06-01"

The change detector formats the new value without the column's utc option:

case "date":
normalizedValue = column.isArray
? normalizedValue.map((date: Date) =>
DateUtils.mixedDateToDateString(date),
)
: DateUtils.mixedDateToDateString(
normalizedValue,
)
databaseValue = column.isArray
? databaseValue.map((date: Date) =>
DateUtils.mixedDateToDateString(date),
)
: DateUtils.mixedDateToDateString(databaseValue)

In New York, 2025-06-01T00:00Z is still May 31 local time, so it formats as 2025-05-31, equal to the stored value, and the column is marked unchanged. The write path formats with { utc: columnMetadata.utc } (for example PostgresDriver.preparePersistentValue), so update() stores 2025-06-01.

My Environment

Dependency Version
Operating System Ubuntu Linux
Node.js version 22.23.3
Typescript version 5.9.3
TypeORM version 1.1.1 (master 5d5fc38)
better-sqlite3 12.11.1
pg 8.23.0 (PostgreSQL 17.11)

Additional Context

The utc option came in #11740, which passes it to DateUtils.mixedDateToDateString on every driver's write path. The change detector is the one call that does not. The fix is to pass { utc: column.utc } to the scalar mixedDateToDateString call in SubjectChangedColumnsComputer. date[] columns stay as they are, because their write path does not apply utc either. I can open a PR with a failing test in test/functional/columns/date-utc/.

Relevant Database Driver(s)

  • better-sqlite3
  • postgres

Are you willing to resolve this issue by submitting a Pull Request?

Yes, I have the time, and I know how to start.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions