timezone or not timezone (with Go)

From: wilk <wilk(at)flibuste(dot)net>
To: pgsql-fr-generale(at)postgresql(dot)org
Subject: timezone or not timezone (with Go)
Date: 2016-11-30 10:33:28
Message-ID: o1m9to$llp$1@blaine.gmane.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-fr-generale

Bonjour,

J'accède à une bdd en Python et maintenant en Go. Je ne suis pas sûr de
bien comprendre comment manipuler les dates.

Depuis toujours mes dates sont au format timestamp (donc sans time zone)

Hors en Go, avec github.com/lib/pq si je lis une date elle me revient
avec une time zone plus ou moins nulle et quand je la compare avec
time.Now() (=time.Since) j'ai du coup un décalage d'une heure.

db.Exec("create table test (now_timestamp timestamp, now_timestamptz timestamptz)")
db.Exec("insert into test (now_timestamp, now_timestamptz) values (now(),now())")
row := db.QueryRow("select now_timestamp, now_timestamp::timestamptz, now_timestamptz from test")
var now_timestamp time.Time
var now_timestamp_to_tz time.Time
var now_timestamptz time.Time
row.Scan(&now_timestamp, &now_timestamp_to_tz, &now_timestamptz)
fmt.Println("time.Now()=", time.Now())
fmt.Println("now_timestamp=", now_timestamp, "since=", time.Since(now_timestamp))
fmt.Println("now_timestamp::tz=", now_timestamp_to_tz, "since=", time.Since(now_timestamp_to_tz))
fmt.Println("now_timestamptz=", now_timestamptz, "since=", time.Since(now_timestamptz))

time.Now()= 2016-11-30 11:24:26.267574524 +0100 CET
now_timestamp= 2016-11-30 11:24:26.2662 +0000 +0000 since= -59m59.998597748s
now_timestamp::tz= 2016-11-30 11:24:26.2662 +0100 CET since= 1.415004ms
now_timestamptz= 2016-11-30 11:24:26.2662 +0100 CET since= 1.418921ms

Si j'utilise github.com/jackc/pgx j'ai à peu près le même problème

time.Now()= 2016-11-30 11:25:03.652587603 +0100 CET
now_timestamp= 2016-11-30 12:25:03.650999 +0100 CET since= -59m59.998355345s
now_timestamp::tz= 2016-11-30 11:25:03.650999 +0100 CET since= 1.655779ms
now_timestamptz= 2016-11-30 11:25:03.650999 +0100 CET since= 1.659894ms

Sur github il y a bien une issue qui semble parler de ce problème mais je ne suis
pas sûr de bien comprendre.
https://github.com/lib/pq/issues/329

Je ne sais pas s'il y a des gurus Go dans la salle. Autrement, mon correctif consiste à
d'utiliser ::timestampz dans toutes mes requêtes. Mais est-ce bien raisonnable ?

--
William

--
Envoi via la liste pgsql-fr-generale (pgsql-fr-generale(at)postgresql(dot)org)

Responses

Browse pgsql-fr-generale by date

  From Date Subject
Next Message Dimitri Fontaine 2016-12-02 21:21:23 Re: timezone or not timezone (with Go)
Previous Message Damien Clochard 2016-11-15 22:53:48 Assemblée Générale de PostgreSQLFr / demain soir à 17h30 / chez Mirakl