...
1# Working with time.Time
2
3By default when interpolating `time.Time` (and `*time.Time`) `goqu` will convert it `UTC` before interpolating.
4
5## Why?
6
7For most use cases `UTC` should be preferred, if a timezone is specified it is usually ignored silently by `postgres` and `mysql` unless you configure your DB to run in a different timezone, leading to unexpected behavior.
8
9## How to use a different default timezone?
10`goqu` provides a **_global_** configuration settings to set the [location](https://golang.org/pkg/time/#Location) to convert all timestamps to.
11
12To change the default timezone to covert time instances to you can use [`goqu.SetTimeLocation`](https://godoc.org/github.com/doug-martin/goqu#SetTimeLocation) to change the default timezone.
13
14In the following example the default value `UTC` is used.
15
16```go
17created, err := time.Parse(time.RFC3339, "2019-10-01T15:01:00Z")
18if err != nil {
19 panic(err)
20}
21
22ds := goqu.Insert("test").Rows(goqu.Record{
23 "address": "111 Address",
24 "name": "Bob Yukon",
25 "created": created,
26})
27```
28
29Output:
30```
31INSERT INTO "test" ("address", "created", "name") VALUES ('111 Address', '2019-10-01T15:01:00Z', 'Bob Yukon')
32```
33
34In the following example `UTC` is overridden to `Asia/Shanghai`
35
36```go
37loc, err := time.LoadLocation("Asia/Shanghai")
38if err != nil {
39 panic(err)
40}
41
42goqu.SetTimeLocation(loc)
43
44created, err := time.Parse(time.RFC3339, "2019-10-01T15:01:00Z")
45if err != nil {
46 panic(err)
47}
48
49ds := goqu.Insert("test").Rows(goqu.Record{
50 "address": "111 Address",
51 "name": "Bob Yukon",
52 "created": created,
53})
54```
55
56Output:
57```
58INSERT INTO "test" ("address", "created", "name") VALUES ('111 Address', '2019-10-01T23:01:00+08:00', 'Bob Yukon')
59```
60
61
62
View as plain text