I have a query that truncates to month
in order to group by a month. My query uses date_trunc('month', foo.date)
, but it ends up subtracting 2 hours, since I’m at UTC+2 I think
Simplified example
@Entity
class Foo(
val date: ZonedDateTime,
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
val id: Long? = null,
)
I don’t know if it matters, but I’m using Spring Data, on top of Postgres. Here’s my query:
@Query("select new com.example.GroupedDTO(count(foo.id), date_trunc('month', foo.date) as date) from Foo foo group by date")
data class GroupedDTO(val count: Long, val date: ZonedDateTime)
If I save an entity with ZonedDateTime.now()
, it has a date of today (12/03), truncated should return 01/03. Running the query directly on Postgres also returns it correctly. Running the query from the app instead returns 28/2 at 22:00, it is off by 2 hours
Is there any way to ensure I can perform this query and have the grouped by result returned correctly?
Is it possible this is an issue with Spring Data and I should ask them instead?
(Hibernate version 6.6.8.Final)
I tried the same thing using the entity manager
entityManager.createQuery("select new com.example.GroupedDTO(count(foo.id), date_trunc('month', foo.date) as date) from Foo foo group by date", GroupedDTO::class.java)
and the dates here are still offset by 2 hours