Skip to content

Commit 55bbc9a

Browse files
dannyotaclaude
andcommitted
fix: Date.Value() returns string to prevent timezone day-shift; add GormDBDataType()
- Date.Value() now returns the date as a string ("YYYY-MM-DD") instead of time.Time to prevent database drivers from applying timezone conversions that can shift the date by a day. - Add GormDBDataType() to Date for explicit provider-specific column types (date for postgres/mysql/sqlite, DATE for sqlserver). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 160cb6b commit 55bbc9a

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

date.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ package datatypes
33
import (
44
"database/sql"
55
"database/sql/driver"
6+
"fmt"
67
"time"
8+
9+
"gorm.io/gorm"
10+
"gorm.io/gorm/schema"
711
)
812

913
type Date time.Time
@@ -17,14 +21,30 @@ func (date *Date) Scan(value interface{}) (err error) {
1721

1822
func (date Date) Value() (driver.Value, error) {
1923
y, m, d := time.Time(date).Date()
20-
return time.Date(y, m, d, 0, 0, 0, 0, time.Time(date).Location()), nil
24+
return fmt.Sprintf("%04d-%02d-%02d", y, m, d), nil
2125
}
2226

2327
// GormDataType gorm common data type
2428
func (date Date) GormDataType() string {
2529
return "date"
2630
}
2731

32+
// GormDBDataType gorm db data type
33+
func (Date) GormDBDataType(db *gorm.DB, field *schema.Field) string {
34+
switch db.Dialector.Name() {
35+
case "mysql":
36+
return "DATE"
37+
case "postgres":
38+
return "DATE"
39+
case "sqlserver":
40+
return "DATE"
41+
case "sqlite":
42+
return "TEXT"
43+
default:
44+
return ""
45+
}
46+
}
47+
2848
func (date Date) GobEncode() ([]byte, error) {
2949
return time.Time(date).GobEncode()
3050
}

0 commit comments

Comments
 (0)