dromara / carbon

A simple, semantic and developer-friendly golang package for time
https://pkg.go.dev/github.com/golang-module/carbon/v2
MIT License
4.77k stars 238 forks source link

The current JSON serialization format is `2006-01-02 15:04:05.999999999`. Is user-defined configuration allowed? #201

Closed shuqingzai closed 10 months ago

shuqingzai commented 11 months ago

参考:

格式是使用固定的常量,用户没法修改,应该改为设置不同类型的全局变量,默认值就是目前的常量模板,让用户在程序启动时,可设置 JSON 序列化的模板

对于需要使用标准库的默认模板(带有时区 carbon.ISO8601Layoutcarbon.RFC3339Layout ),目前无法实现,对于时区的掌控,我们需要交给前端部署的不同地区节点,自动转换,服务器统一使用 UTC

gouguoyin commented 11 months ago

At present, please expand the method yourself, such as:

type rfc3339 carbon.Carbon

func (t rfc3339) MarshalJSON() ([]byte, error) {
    return []byte(fmt.Sprintf(`"%s"`, t.ToRfc3339String())), nil
}

func (t *rfc3339) UnmarshalJSON(b []byte) error {
    c := carbon.ParseByLayout(string(bytes.Trim(b, `"`)), carbon.RFC3339Layout, t.Location())
    if c.Error == nil {
        *t = carbon.DateTime{Carbon: c}
    }
    return c.Error
}
shuqingzai commented 11 months ago
c := carbon.ParseByLayout(string(bytes.Trim(b, `"`)), carbon.RFC3339Layout, t.Location())
  if c.Error == nil {
      *t = carbon.DateTime{Carbon: c}
  }
  return c.Error

是的,我目前是这样的做,只是提个意见,用户可以自定义格式

Issues-translate-bot commented 11 months ago

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


c := carbon.ParseByLayout(string(bytes.Trim(b, `"`)), carbon.RFC3339Layout, t.Location())
if c.Error == nil {
*t = carbon.DateTime{Carbon: c}
}
return c.Error

Yes, this is how I do it currently. I just want to give my opinion. Users can customize the format.

gouguoyin commented 10 months ago

The problem is

type Person struct {
    Birthday1 carbon.Carbon `json:"birthday1"`
    Birthday2 carbon.Carbon `json:"birthday2"`
}

I want birthday1 to display Y-m-d H:i:s format, birthday2 to display Y-m-d format.

How to distinguish different output formats of the same type? When should users be allowed to customize different formats?

gouguoyin commented 10 months ago

In v2.3.0, the tag mechanism is used for json user-defined encoding and decoding.

定义模型
type Person struct {
    Name string `json:"name"`
    Age int `json:"age"`
    Birthday carbon.Carbon `json:"birthday" carbon:"layout:2006-01-02"`
    GraduatedAt carbon.Carbon `json:"graduated_at" carbon:"layout:15:04:05"`
    CreatedAt carbon.Carbon `json:"created_at" carbon:"layout:2006-01-02 15:04:05"`
}

type Person struct {
    Name string `json:"name"`
    Age int `json:"age"`
    Birthday carbon.Carbon `json:"birthday" carbon:"format:Y-m-d"`
    GraduatedAt carbon.Carbon `json:"graduated_at" carbon:"format:H:i:s"`
    CreatedAt carbon.Carbon `json:"created_at" carbon:"format:Y-m-d H:i:s"`
}
实例化模型
now := Parse("2020-08-05 13:14:15", PRC)
person := Person {
    Name:        "gouguoyin",
    Age:         18,
    Birthday:    now,
    GraduatedAt: now,
    CreatedAt:   now,
}
JSON 编码
err1 := carbon.LoadTag(&person)
if err1 != nil {
    // 错误处理
    log.Fatal(err1)
}
data, err2 := json.Marshal(person)
if err2 != nil {
    // 错误处理
    log.Fatal(err2)
}
fmt.Printf("%s", data)
// 输出
{
    "name": "gouguoyin",
    "age": 18,
    "birthday": "2020-08-05",
    "graduated_at": "13:14:15",
    "created_at": "2020-08-05 13:14:15"
}
JSON 解码
str := `{
    "name": "gouguoyin",
    "age": 18,
    "birthday": "2020-08-05",
    "graduated_at": "13:14:15",
    "created_at": "2020-08-05 13:14:15"
}`
var person Person

err1 := carbon.LoadTag(&person)
if err1 != nil {
    // 错误处理
    log.Fatal(err1)
}

err2 := json.Unmarshal([]byte(str), &person)
if err2 != nil {
    // 错误处理
    log.Fatal(err2)
}

fmt.Sprintf("%s", person.Birthday) // 2002-08-05
fmt.Sprintf("%s", person.GraduatedAt) // 13:14:15
fmt.Sprintf("%s", person.CreatedAt) // 2002-08-05 13:14:15