lib / pq

Pure Go Postgres driver for database/sql
https://pkg.go.dev/github.com/lib/pq
MIT License
9.04k stars 909 forks source link

Feature: Scan time.Time correctly or incorrectly #1093

Open TimurIskandarov opened 1 year ago

TimurIskandarov commented 1 year ago

There is an entity

type Proposal struct {
  ID int
  Date time.Time
}

There is an entity table DDl

CREATE TABLE public.proposal (
  id serial NOT NULL,
  date timestamp NULL,
  CONSTRAINT pk_proposal PRIMARY KEY (id),
);
  1. Insert works correctly (i.e. 2022-10-26T00:00:00.000Z)
    // create entity
    location, _ := time.LoadLocation("Europe/Moscow")
    date, _ := time.Parse(time.RFC3339, "2022-10-25T21:00:00.000Z")
    p := &Proposal{
    Date: date.In(location)
    }
    // insert entity
    err := r.conn.QueryRow(
    "INSERT INTO proposal (date) VALUES ($1) RETURNING id",
     p.Date,
    ).Scan(p.ID)
  2. Fetch works incorrectly (i.e. 2022-10-26 00:00:00:00 +0000 +0000, not 2022-10-26 00:00:00:00 +0300 MSK)
    proposal := new(Proposal)
    err := r.conn.QueryRow(
    "SELECT date FROM proposal ORDER BY id DESC LIMIT 1",
    ).Scan(&p.Date)

    In the future, an incorrect date is obtained on the frontend new Date(date).toLocaleString('ru-RU'), that is, 26.10.2022, 03:00:00.

Lefree111 commented 1 year ago

thanks bro

вт, 25 окт. 2022 г. в 18:51, Timur @.***>:

There is an entity

type Proposal struct { ID int Date time.Time }

There is an entity table DDl

CREATE TABLE public.proposal ( id serial NOT NULL, date timestamp NULL, CONSTRAINT pk_proposal PRIMARY KEY (id), );

  1. Insert works correctly (i.e. 2022-10-26T00:00:00.000Z)

// create entity location, := time.LoadLocation("Europe/Moscow") date, := time.Parse(time.RFC3339, "2022-10-25T21:00:00.000Z") p := &Proposal{ Date: date.In(location) } // insert entity err := r.conn.QueryRow( "INSERT INTO proposal (date) VALUES ($1) RETURNING id", p.Date, ).Scan(p.ID)

  1. Fetch works incorrectly (i.e. 2022-10-26 00:00:00:00 +0000 +0000, not 2022-10-26 00:00:00:00 +0300 MSK)

proposal := new(Proposal) err := r.conn.QueryRow( "SELECT date FROM proposal ORDER BY id DESC LIMIT 1", ).Scan(&p.Date)

In the future, an incorrect date is obtained on the frontend new Date(date).toLocaleString('ru-RU'), that is, 26.10.2022, 03:00:00.

— Reply to this email directly, view it on GitHub https://github.com/lib/pq/issues/1093, or unsubscribe https://github.com/notifications/unsubscribe-auth/AY6WB6XEGGRQJF66SKLXP6TWE7QXVANCNFSM6AAAAAARN74JSU . You are receiving this because you are subscribed to this thread.Message ID: @.***>