kom*_*ten 2 postgresql datetime go
我有一个 postgresql db,列 date 和 repeat_until 作为带时区的时间戳。示例日期具有特定于时区的格式。后者是冬令时。
2017-08-28 09:00:00+02, 2017-12-31 23:00:00+01
使用 string 和 time.Time 第一个给出相对于 GMT+0 的时间,后者是秒(不是 unix 时间戳)。
import (
_ "github.com/lib/pq"
"fmt"
"github.com/gorilla/mux"
"github.com/jmoiron/sqlx"
"log"
"net/http"
"time"
)
type Event struct {
Date string
RepeatUntil time.Time `db:"repeat_until"`
}
event := Event{}
rows, _ := db.Queryx("select * from events order by date")
for rows.Next() {
err := rows.StructScan(&event)
if err != nil {
log.Fatalln(err)
}
fmt.Printf("%#v", event)
}
Run Code Online (Sandbox Code Playgroud)
Date:"2017-08-28T07:00:00Z"
RepeatUntil:time.Time{sec:63650354400, nsec:0, loc:(*time.Location)(nil)}
保留时区信息的推荐方法是什么?time.Time 似乎很明显,但我不确定它是如何到达 3986 年的 unixtime 秒的。
我正在使用sqlx。
PostgreSQL 中的时区是会话参数,即可以为每个会话(连接)指定时区。如果不指定,时区将根据pg_hba.conf. 当您选择记录时,日期/时间数据将自动从数据库时区转换为会话(连接)时区。
在您的情况下,要获取特定时区的时间,请在连接参数中明确指定,例如
psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
"dbname=%s sslmode=disable TimeZone=Europe/Paris",
host, port, user, dbname)
db, err := sqlx.Open("postgres", psqlInfo)
Run Code Online (Sandbox Code Playgroud)
的TimeZone可以通过以下方式获得
select * from pg_timezone_names;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3470 次 |
| 最近记录: |