Coder Social home page Coder Social logo

Comments (5)

gouguoyin avatar gouguoyin commented on June 11, 2024

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
}

from carbon.

shuqingzai avatar shuqingzai commented on June 11, 2024
c := carbon.ParseByLayout(string(bytes.Trim(b, `"`)), carbon.RFC3339Layout, t.Location())
	if c.Error == nil {
		*t = carbon.DateTime{Carbon: c}
	}
	return c.Error

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

from carbon.

Issues-translate-bot avatar Issues-translate-bot commented on June 11, 2024

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.

from carbon.

gouguoyin avatar gouguoyin commented on June 11, 2024

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?

from carbon.

gouguoyin avatar gouguoyin commented on June 11, 2024

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

from carbon.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.