jackc / pgtype

MIT License
308 stars 110 forks source link

Problem with saving *time.Time, ignoring timezone #121

Closed temorfeouz closed 2 years ago

temorfeouz commented 3 years ago

Hi! I`ve found bug with storing time.Time by reference and option preferSimpleProtocol = false. When value passing as a struct, time will be saved correctly, taking into account timezone(this code processing this case https://github.com/jackc/pgx/blob/5320ad87c845b5ce1d8485af5b36a0c67deb9224/values.go#L83)

But when time are passing by reference another code process time - https://github.com/jackc/pgtype/blob/90af821478c74fd8f917b7301d79886f933e5fe3/timestamptz.go#L171 and here time converts to UTC.

Ways for fix:

  1. do not use simple protocol, preferred way.
  2. add case *time.Time here https://github.com/jackc/pgx/blob/5320ad87c845b5ce1d8485af5b36a0c67deb9224/values.go#L83 and dereference stuct
  3. do not convert time to UTC here https://github.com/jackc/pgtype/blob/90af821478c74fd8f917b7301d79886f933e5fe3/timestamptz.go#L171
  4. on user side code dereference *time.Time

I`d loved to use first way, but i cant( Can someone describe why not added case with *time.TIme or time directory convers to UTC?

jackc commented 3 years ago

Times must be encoded in UTC for timestamptz. If you don't want the time zone to affect things use timestamp.

Unless I don't understand what you mean... in which case please provide example code for this case.

temorfeouz commented 2 years ago

Its not a bug. Problem was in column type, in my db column type is timestamp, without time zone. Thanks!