Comments (5)
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.
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.
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.
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.
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)
- What is the best way to mock carbon in unit testing? HOT 11
- Is there any consideration to support converting the lunar calendar to the Gregorian calendar? HOT 3
- [Feature Request] Please support MJD format in Carbon HOT 2
- 咨询关于Now函数入参的问题(About the Now function parameter issue) HOT 1
- `ToWeekString()` ignores `SetWeekStartsAt()`
- "2023-11-08T09:52:48" not support? HOT 5
- Rename the func such as `carbon.CreateFromDate()`. HOT 13
- ParseByFormat not supported "U" format HOT 3
- json is reversed, causing the diff series method to be abnormal. What should I do in this case? HOT 5
- My local time is Asia/Shanghai, But the time from the database is UTC HOT 6
- Default JSON template settings HOT 1
- Wrong zodiac sign HOT 2
- Starting from v2.3.0, after removing the series of methods such as DateTime, it is extremely inconvenient to insert the birthday (Y-m-d) field with gorm now. What is the best practice HOT 11
- build for windows_386 (untyped int constant) overflows int HOT 6
- How to get time.Duration in two carbon instances? ? HOT 1
- ParseByFormat fails to detect location Location HOT 2
- cannot parse string null HOT 3
- DiffAbsInDuration must upgrade go version to 1.9+ HOT 3
- Can you add the 1.5.5 tag? HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from carbon.