Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gorm Using carbon.Carbon will add one more time zone #243

Open
yunxi177 opened this issue Aug 15, 2024 · 6 comments
Open

gorm Using carbon.Carbon will add one more time zone #243

yunxi177 opened this issue Aug 15, 2024 · 6 comments
Labels
Question Indicates that an issue, pull request, or discussion needs more information

Comments

@yunxi177
Copy link

yunxi177 commented Aug 15, 2024

设置了全局默认设置

carbon.SetDefault(carbon.Default{
      Layout:       carbon.DateTimeLayout,
      Timezone:     carbon.UTC,
      WeekStartsAt: carbon.Monday,
      Locale:       "zh-CN",
})

结构体声明

type YHXDayUp struct {
    ID                      int32          `gorm:"column:id;primaryKey;autoIncrement:true" json:"id"`
    DeviceId                int32          `json:"device_id" comment:"设备id"`                                    
    CreatedAt               carbon.Carbon  `gorm:"column:created_at;type:timestamp" json:"created_at" comment:"创建时间"`
    UpdatedAt               carbon.Carbon  `gorm:"column:updated_at;type:timestamp" json:"updated_at"`
    DeletedAt               gorm.DeletedAt `gorm:"column:deleted_at;type:timestamp" json:"deleted_at"`
}

写入代码

info := YHXDayUp{}
info.CreatedAt = carbon.CreateFromDateTime(int(dayUp.Year), int(dayUp.Month), int(dayUp.Day), int(dayUp.Hour), int(dayUp.Minute), int(dayUp.Second),)

数据库存入的 created_at 是 2024-07-04 11:17:00 JSON 序列化后返回的是 2024-07-04 19:17:00。
这里是不是要有个机制来处理一下,写入已经按照当地时区写入了,读取如果再加一次时区,会导致读出数据不正确的情况。

@yunxi177 yunxi177 added the Question Indicates that an issue, pull request, or discussion needs more information label Aug 15, 2024
@Issues-translate-bot Issues-translate-bot changed the title gorm 使用 carbon.Carbon 会多加一次时区 gorm Using carbon.Carbon will add one more time zone Aug 15, 2024
@Issues-translate-bot
Copy link
Collaborator

The issue body's language is not English, translate it automatically, please use English next time. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


Global defaults set
carbon.SetDefault(carbon.Default{
Layout: carbon.DateTimeLayout,
Timezone: carbon.UTC,
WeekStartsAt: carbon.Monday,
Locale: "zh-CN",
})

Structure declaration
type YHXDayUp struct {
ID int32 `gorm:"column:id;primaryKey;autoIncrement:true" json:"id"`
DeviceId int32 `json:"device_id" comment:"device id"`
CreatedAt carbon.Carbon `gorm:"column:created_at;type:timestamp" json:"created_at" comment:"created time"`
UpdatedAt carbon.Carbon `gorm:"column:updated_at;type:timestamp" json:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;type:timestamp" json:"deleted_at"`
}


write code
...
info := YHXDayUp{}
info.CreatedAt = carbon.CreateFromDateTime(int(dayUp.Year), int(dayUp.Month), int(dayUp.Day), int(dayUp.Hour), int(dayUp.Minute), int(dayUp.Second), )
...

The created_at stored in the database is 2024-07-04 11:17:00, and the value returned after JSON serialization is 2024-07-04 19:17:00.
Is there a mechanism to deal with it here? The write has been written according to the local time zone. If the time zone is added to the read, the read data will be incorrect.

@gouguoyin
Copy link
Collaborator

v2.4.0 has been fixed and released

@Clago
Copy link

Clago commented Oct 15, 2024

v2.4.0 has been fixed and released

json.Unmarshal()转换的时间时区会变成time.UTC,这个是啥原因?

@gouguoyin gouguoyin reopened this Oct 15, 2024
@gouguoyin
Copy link
Collaborator

Please post the code

@Clago
Copy link

Clago commented Oct 15, 2024

Please post the code

type Project struct {
  StartDate     carbon.DateTime `gorm:"column:start_date" json:"startDate"`
  EndDate       carbon.DateTime `gorm:"column:end_date" json:"endDate"`
}

// json data
{"startDate":"2024-10-01 00:00:00","endDate":"2024-10-31 23:59:59"}

// gin bind data
project := new(model.Project)
c.ShouldBind(project)


log.Printf("%#v %#v", project.StartDate.Location(), project.EndDate.Location())
// carbon 2.1.8
"Local" "Local"    
// carbon 2.4.1
"UTC" "UTC"

@Issues-translate-bot
Copy link
Collaborator

The issue body's language is not English, translate it automatically, please use English next time. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


Please post the code

type Project struct {
  StartDate     carbon.DateTime `gorm:"column:start_date" json:"startDate"`
  EndDate       carbon.DateTime `gorm:"column:end_date" json:"endDate"`
}

// json data
{"startDate":"2024-10-01 00:00:00","endDate":"2024-10-31 23:59:59"}

// gin bind data
project := new(model.Project)
c.ShouldBind(project)


log.Printf("%#v %#v", project.StartDate.Location(), project.EndDate.Location())
// carbon 2.1.8
"Local" "Local"    
// carbon 2.4.1
"UTC" "UTC"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question Indicates that an issue, pull request, or discussion needs more information
Projects
None yet
Development

No branches or pull requests

4 participants