-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #167 from doug-martin/fix_time_issues
v9.1.0
- Loading branch information
Showing
11 changed files
with
167 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# Working with time.Time | ||
|
||
By default when interpolating `time.Time` (and `*time.Time`) `goqu` will convert it `UTC` before interpolating. | ||
|
||
## Why? | ||
|
||
For 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. | ||
|
||
## How to use a different default timezone? | ||
`goqu` provides a **_global_** configuration settings to set the [location](https://golang.org/pkg/time/#Location) to convert all timestamps to. | ||
|
||
To 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. | ||
|
||
In the following example the default value `UTC` is used. | ||
|
||
```go | ||
created, err := time.Parse(time.RFC3339, "2019-10-01T15:01:00Z") | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
ds := goqu.Insert("test").Rows(goqu.Record{ | ||
"address": "111 Address", | ||
"name": "Bob Yukon", | ||
"created": created, | ||
}) | ||
``` | ||
|
||
Output: | ||
``` | ||
INSERT INTO "test" ("address", "created", "name") VALUES ('111 Address', '2019-10-01T15:01:00Z', 'Bob Yukon') | ||
``` | ||
|
||
In the following example `UTC` is overridden to `Asia/Shanghai` | ||
|
||
```go | ||
loc, err := time.LoadLocation("Asia/Shanghai") | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
goqu.SetTimeLocation(loc) | ||
|
||
created, err := time.Parse(time.RFC3339, "2019-10-01T15:01:00Z") | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
ds := goqu.Insert("test").Rows(goqu.Record{ | ||
"address": "111 Address", | ||
"name": "Bob Yukon", | ||
"created": created, | ||
}) | ||
``` | ||
|
||
Output: | ||
``` | ||
INSERT INTO "test" ("address", "created", "name") VALUES ('111 Address', '2019-10-01T23:01:00+08:00', 'Bob Yukon') | ||
``` | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package sqlgen | ||
|
||
import "time" | ||
|
||
var timeLocation = time.UTC | ||
|
||
// Set the location to use when interpolating time.Time instances. See https://golang.org/pkg/time/#LoadLocation | ||
// NOTE: This has no effect when using prepared statements. | ||
func SetTimeLocation(loc *time.Location) { | ||
timeLocation = loc | ||
} |