mirror of
https://gitee.com/dromara/carbon.git
synced 2024-11-29 18:57:37 +08:00
Group constants
This commit is contained in:
parent
b145cbb470
commit
9db49a72a9
81
carbon.go
81
carbon.go
@ -127,28 +127,31 @@ const (
|
||||
// layout constants
|
||||
// 布局模板常量
|
||||
const (
|
||||
ANSICLayout = time.ANSIC
|
||||
UnixDateLayout = time.UnixDate
|
||||
RubyDateLayout = time.RubyDate
|
||||
RFC822Layout = time.RFC822
|
||||
RFC822ZLayout = time.RFC822Z
|
||||
RFC850Layout = time.RFC850
|
||||
RFC1123Layout = time.RFC1123
|
||||
RFC1123ZLayout = time.RFC1123Z
|
||||
RssLayout = time.RFC1123Z
|
||||
KitchenLayout = time.Kitchen
|
||||
RFC2822Layout = time.RFC1123Z
|
||||
CookieLayout = "Monday, 02-Jan-2006 15:04:05 MST"
|
||||
RFC3339Layout = "2006-01-02T15:04:05Z07:00"
|
||||
RFC3339MilliLayout = "2006-01-02T15:04:05.999Z07:00"
|
||||
RFC3339MicroLayout = "2006-01-02T15:04:05.999999Z07:00"
|
||||
RFC3339NanoLayout = "2006-01-02T15:04:05.999999999Z07:00"
|
||||
ISO8601Layout = "2006-01-02T15:04:05-07:00"
|
||||
ISO8601MilliLayout = "2006-01-02T15:04:05.999-07:00"
|
||||
ISO8601MicroLayout = "2006-01-02T15:04:05.999999-07:00"
|
||||
ISO8601NanoLayout = "2006-01-02T15:04:05.999999999-07:00"
|
||||
RFC1036Layout = "Mon, 02 Jan 06 15:04:05 -0700"
|
||||
RFC7231Layout = "Mon, 02 Jan 2006 15:04:05 MST"
|
||||
ANSICLayout = time.ANSIC
|
||||
CookieLayout = "Monday, 02-Jan-2006 15:04:05 MST"
|
||||
KitchenLayout = time.Kitchen
|
||||
RssLayout = time.RFC1123Z
|
||||
RubyDateLayout = time.RubyDate
|
||||
UnixDateLayout = time.UnixDate
|
||||
|
||||
RFC1036Layout = "Mon, 02 Jan 06 15:04:05 -0700"
|
||||
RFC1123Layout = time.RFC1123
|
||||
RFC1123ZLayout = time.RFC1123Z
|
||||
RFC2822Layout = time.RFC1123Z
|
||||
RFC3339Layout = "2006-01-02T15:04:05Z07:00"
|
||||
RFC3339MilliLayout = "2006-01-02T15:04:05.999Z07:00"
|
||||
RFC3339MicroLayout = "2006-01-02T15:04:05.999999Z07:00"
|
||||
RFC3339NanoLayout = "2006-01-02T15:04:05.999999999Z07:00"
|
||||
RFC7231Layout = "Mon, 02 Jan 2006 15:04:05 MST"
|
||||
RFC822Layout = time.RFC822
|
||||
RFC822ZLayout = time.RFC822Z
|
||||
RFC850Layout = time.RFC850
|
||||
|
||||
ISO8601Layout = "2006-01-02T15:04:05-07:00"
|
||||
ISO8601MilliLayout = "2006-01-02T15:04:05.999-07:00"
|
||||
ISO8601MicroLayout = "2006-01-02T15:04:05.999999-07:00"
|
||||
ISO8601NanoLayout = "2006-01-02T15:04:05.999999999-07:00"
|
||||
|
||||
DayDateTimeLayout = "Mon, Jan 2, 2006 3:04 PM"
|
||||
DateTimeLayout = "2006-01-02 15:04:05"
|
||||
DateTimeMilliLayout = "2006-01-02 15:04:05.999"
|
||||
@ -158,22 +161,24 @@ const (
|
||||
ShortDateTimeMilliLayout = "20060102150405.999"
|
||||
ShortDateTimeMicroLayout = "20060102150405.999999"
|
||||
ShortDateTimeNanoLayout = "20060102150405.999999999"
|
||||
DateLayout = "2006-01-02"
|
||||
DateMilliLayout = "2006-01-02.999"
|
||||
DateMicroLayout = "2006-01-02.999999"
|
||||
DateNanoLayout = "2006-01-02.999999999"
|
||||
ShortDateLayout = "20060102"
|
||||
ShortDateMilliLayout = "20060102.999"
|
||||
ShortDateMicroLayout = "20060102.999999"
|
||||
ShortDateNanoLayout = "20060102.999999999"
|
||||
TimeLayout = "15:04:05"
|
||||
TimeMilliLayout = "15:04:05.999"
|
||||
TimeMicroLayout = "15:04:05.999999"
|
||||
TimeNanoLayout = "15:04:05.999999999"
|
||||
ShortTimeLayout = "150405"
|
||||
ShortTimeMilliLayout = "150405.999"
|
||||
ShortTimeMicroLayout = "150405.999999"
|
||||
ShortTimeNanoLayout = "150405.999999999"
|
||||
|
||||
DateLayout = "2006-01-02"
|
||||
DateMilliLayout = "2006-01-02.999"
|
||||
DateMicroLayout = "2006-01-02.999999"
|
||||
DateNanoLayout = "2006-01-02.999999999"
|
||||
ShortDateLayout = "20060102"
|
||||
ShortDateMilliLayout = "20060102.999"
|
||||
ShortDateMicroLayout = "20060102.999999"
|
||||
ShortDateNanoLayout = "20060102.999999999"
|
||||
|
||||
TimeLayout = "15:04:05"
|
||||
TimeMilliLayout = "15:04:05.999"
|
||||
TimeMicroLayout = "15:04:05.999999"
|
||||
TimeNanoLayout = "15:04:05.999999999"
|
||||
ShortTimeLayout = "150405"
|
||||
ShortTimeMilliLayout = "150405.999"
|
||||
ShortTimeMicroLayout = "150405.999999"
|
||||
ShortTimeNanoLayout = "150405.999999999"
|
||||
)
|
||||
|
||||
// Carbon defines a Carbon struct.
|
||||
|
185
json_test.go
185
json_test.go
@ -12,58 +12,29 @@ type Person struct {
|
||||
Name string `json:"name"`
|
||||
Age int `json:"age"`
|
||||
|
||||
Birthday1 Carbon `json:"birthday1" carbon:"layout:2006-01-02 15:04:05"`
|
||||
Birthday2 Carbon `json:"birthday2" carbon:"layout:2006-01-02 15:04:05.999"`
|
||||
Birthday3 Carbon `json:"birthday3" carbon:"layout:2006-01-02 15:04:05.999999"`
|
||||
Birthday4 Carbon `json:"birthday4" carbon:"layout:2006-01-02 15:04:05.999999999"`
|
||||
Birthday0 Carbon `json:"birthday0"`
|
||||
|
||||
GraduatedAt1 Carbon `json:"graduated_at1" carbon:"layout:2006-01-02"`
|
||||
GraduatedAt2 Carbon `json:"graduated_at2" carbon:"layout:2006-01-02.999"`
|
||||
GraduatedAt3 Carbon `json:"graduated_at3" carbon:"layout:2006-01-02.999999"`
|
||||
GraduatedAt4 Carbon `json:"graduated_at4" carbon:"layout:2006-01-02.999999999"`
|
||||
Birthday1 Carbon `json:"birthday1" carbon:"layout:2006-01-02"`
|
||||
Birthday2 Carbon `json:"birthday2" carbon:"layout:15:04:05"`
|
||||
Birthday3 Carbon `json:"birthday3" carbon:"layout:2006-01-02 15:04:05"`
|
||||
|
||||
OperatedAt1 Carbon `json:"operated_at1" carbon:"layout:15:04:05"`
|
||||
OperatedAt2 Carbon `json:"operated_at2" carbon:"layout:15:04:05.999"`
|
||||
OperatedAt3 Carbon `json:"operated_at3" carbon:"layout:15:04:05.999999"`
|
||||
OperatedAt4 Carbon `json:"operated_at4" carbon:"layout:15:04:05.999999999"`
|
||||
|
||||
CreatedAt1 Carbon `json:"created_at1" carbon:"format:Y-m-d"`
|
||||
CreatedAt2 Carbon `json:"created_at2" carbon:"format:H:i:s"`
|
||||
CreatedAt3 Carbon `json:"created_at3" carbon:"format:Y-m-d H:i:s"`
|
||||
CreatedAt4 Carbon `json:"created_at4"`
|
||||
}
|
||||
|
||||
type Student struct {
|
||||
Name string `json:"name"`
|
||||
Age int `json:"age"`
|
||||
Birthday Carbon `json:"birthday"`
|
||||
Birthday4 Carbon `json:"birthday4" carbon:"format:Y-m-d"`
|
||||
Birthday5 Carbon `json:"birthday5" carbon:"format:H:i:s"`
|
||||
Birthday6 Carbon `json:"birthday6" carbon:"format:Y-m-d H:i:s"`
|
||||
}
|
||||
|
||||
func TestCarbon_MarshalJSON_LoadTag(t *testing.T) {
|
||||
c := Parse("2020-08-05 13:14:15.999999999", PRC)
|
||||
person := Person{
|
||||
Name: "gouguoyin",
|
||||
Age: 18,
|
||||
|
||||
Name: "gouguoyin",
|
||||
Age: 18,
|
||||
Birthday0: c,
|
||||
Birthday1: c,
|
||||
Birthday2: c,
|
||||
Birthday3: c,
|
||||
Birthday4: c,
|
||||
|
||||
GraduatedAt1: c,
|
||||
GraduatedAt2: c,
|
||||
GraduatedAt3: c,
|
||||
GraduatedAt4: c,
|
||||
|
||||
OperatedAt1: c,
|
||||
OperatedAt2: c,
|
||||
OperatedAt3: c,
|
||||
OperatedAt4: c,
|
||||
|
||||
CreatedAt1: c,
|
||||
CreatedAt2: c,
|
||||
CreatedAt3: c,
|
||||
CreatedAt4: c,
|
||||
Birthday5: c,
|
||||
Birthday6: c,
|
||||
}
|
||||
|
||||
loadErr := LoadTag(&person)
|
||||
@ -72,64 +43,55 @@ func TestCarbon_MarshalJSON_LoadTag(t *testing.T) {
|
||||
data, marshalErr := json.Marshal(&person)
|
||||
assert.Nil(t, marshalErr)
|
||||
|
||||
assert.Equal(t, "2020-08-05 13:14:15", person.Birthday1.String())
|
||||
assert.Equal(t, "2020-08-05 13:14:15.999", person.Birthday2.String())
|
||||
assert.Equal(t, "2020-08-05 13:14:15.999999", person.Birthday3.String())
|
||||
assert.Equal(t, "2020-08-05 13:14:15.999999999", person.Birthday4.String())
|
||||
assert.Equal(t, "2020-08-05 13:14:15", person.Birthday0.String())
|
||||
|
||||
assert.Equal(t, "2020-08-05", person.GraduatedAt1.String())
|
||||
assert.Equal(t, "2020-08-05.999", person.GraduatedAt2.String())
|
||||
assert.Equal(t, "2020-08-05.999999", person.GraduatedAt3.String())
|
||||
assert.Equal(t, "2020-08-05.999999999", person.GraduatedAt4.String())
|
||||
assert.Equal(t, "2020-08-05", person.Birthday1.String())
|
||||
assert.Equal(t, "13:14:15", person.Birthday2.String())
|
||||
assert.Equal(t, "2020-08-05 13:14:15", person.Birthday3.String())
|
||||
|
||||
assert.Equal(t, "13:14:15", person.OperatedAt1.String())
|
||||
assert.Equal(t, "13:14:15.999", person.OperatedAt2.String())
|
||||
assert.Equal(t, "13:14:15.999999", person.OperatedAt3.String())
|
||||
assert.Equal(t, "13:14:15.999999999", person.OperatedAt4.String())
|
||||
|
||||
assert.Equal(t, "2020-08-05", person.CreatedAt1.String())
|
||||
assert.Equal(t, "13:14:15", person.CreatedAt2.String())
|
||||
assert.Equal(t, "2020-08-05 13:14:15", person.CreatedAt3.String())
|
||||
assert.Equal(t, "2020-08-05 13:14:15", person.CreatedAt4.String())
|
||||
assert.Equal(t, "2020-08-05", person.Birthday4.String())
|
||||
assert.Equal(t, "13:14:15", person.Birthday5.String())
|
||||
assert.Equal(t, "2020-08-05 13:14:15", person.Birthday6.String())
|
||||
|
||||
fmt.Printf("person output by json:\n%s\n", data)
|
||||
}
|
||||
|
||||
func TestCarbon_MarshalJSON_UnLoadTag(t *testing.T) {
|
||||
c := Parse("2020-08-05 13:14:15.999999999", PRC)
|
||||
student := Student{
|
||||
Name: "gouguoyin",
|
||||
Age: 18,
|
||||
Birthday: c,
|
||||
person := Person{
|
||||
Name: "gouguoyin",
|
||||
Age: 18,
|
||||
Birthday1: c,
|
||||
Birthday2: c,
|
||||
Birthday3: c,
|
||||
Birthday4: c,
|
||||
Birthday5: c,
|
||||
Birthday6: c,
|
||||
}
|
||||
|
||||
data, marshalErr := json.Marshal(&student)
|
||||
data, marshalErr := json.Marshal(&person)
|
||||
assert.Nil(t, marshalErr)
|
||||
|
||||
assert.Equal(t, "2020-08-05 13:14:15", student.Birthday.String())
|
||||
assert.Equal(t, "2020-08-05 13:14:15", person.Birthday1.String())
|
||||
assert.Equal(t, "2020-08-05 13:14:15", person.Birthday2.String())
|
||||
assert.Equal(t, "2020-08-05 13:14:15", person.Birthday3.String())
|
||||
assert.Equal(t, "2020-08-05 13:14:15", person.Birthday4.String())
|
||||
assert.Equal(t, "2020-08-05 13:14:15", person.Birthday5.String())
|
||||
assert.Equal(t, "2020-08-05 13:14:15", person.Birthday6.String())
|
||||
fmt.Printf("student output by json:\n%s\n", data)
|
||||
}
|
||||
|
||||
func TestCarbon_UnmarshalJSON_LoadTag(t *testing.T) {
|
||||
str := `{
|
||||
"name": "gouguoyin",
|
||||
"age": 18,
|
||||
"birthday1": "2020-08-05 13:14:15",
|
||||
"birthday2": "2020-08-05 13:14:15.999",
|
||||
"birthday3": "2020-08-05 13:14:15.999999",
|
||||
"birthday4": "2020-08-05 13:14:15.999999999",
|
||||
"graduated_at1": "2020-08-05",
|
||||
"graduated_at2": "2020-08-05.999",
|
||||
"graduated_at3": "2020-08-05.999999",
|
||||
"graduated_at4": "2020-08-05.999999999",
|
||||
"operated_at1": "13:14:15",
|
||||
"operated_at2": "13:14:15.999",
|
||||
"operated_at3": "13:14:15.999999",
|
||||
"operated_at4": "13:14:15.999999999",
|
||||
"created_at1": "2020-08-05",
|
||||
"created_at2": "13:14:15",
|
||||
"created_at3": "2020-08-05 13:14:15",
|
||||
"created_at4": "2020-08-05 13:14:15"
|
||||
"name":"gouguoyin",
|
||||
"age":18,
|
||||
"birthday0":"2020-08-05 13:14:15",
|
||||
"birthday1":"2020-08-05",
|
||||
"birthday2":"13:14:15",
|
||||
"birthday3":"2020-08-05 13:14:15",
|
||||
"birthday4":"2020-08-05",
|
||||
"birthday5":"13:14:15",
|
||||
"birthday6":"2020-08-05 13:14:15"
|
||||
}`
|
||||
|
||||
var person Person
|
||||
@ -140,60 +102,51 @@ func TestCarbon_UnmarshalJSON_LoadTag(t *testing.T) {
|
||||
unmarshalErr := json.Unmarshal([]byte(str), &person)
|
||||
assert.Nil(t, unmarshalErr)
|
||||
|
||||
assert.Equal(t, "2020-08-05 13:14:15", person.Birthday1.String())
|
||||
assert.Equal(t, "2020-08-05 13:14:15.999", person.Birthday2.String())
|
||||
assert.Equal(t, "2020-08-05 13:14:15.999999", person.Birthday3.String())
|
||||
assert.Equal(t, "2020-08-05 13:14:15.999999999", person.Birthday4.String())
|
||||
|
||||
assert.Equal(t, "2020-08-05", person.GraduatedAt1.String())
|
||||
assert.Equal(t, "2020-08-05.999", person.GraduatedAt2.String())
|
||||
assert.Equal(t, "2020-08-05.999999", person.GraduatedAt3.String())
|
||||
assert.Equal(t, "2020-08-05.999999999", person.GraduatedAt4.String())
|
||||
|
||||
assert.Equal(t, "13:14:15", person.OperatedAt1.String())
|
||||
assert.Equal(t, "13:14:15.999", person.OperatedAt2.String())
|
||||
assert.Equal(t, "13:14:15.999999", person.OperatedAt3.String())
|
||||
assert.Equal(t, "13:14:15.999999999", person.OperatedAt4.String())
|
||||
|
||||
assert.Equal(t, "2020-08-05", person.CreatedAt1.String())
|
||||
assert.Equal(t, "13:14:15", person.CreatedAt2.String())
|
||||
assert.Equal(t, "2020-08-05 13:14:15", person.CreatedAt3.String())
|
||||
assert.Equal(t, "2020-08-05 13:14:15", person.Birthday0.String())
|
||||
assert.Equal(t, "2020-08-05", person.Birthday1.String())
|
||||
assert.Equal(t, "13:14:15", person.Birthday2.String())
|
||||
assert.Equal(t, "2020-08-05 13:14:15", person.Birthday3.String())
|
||||
assert.Equal(t, "2020-08-05", person.Birthday4.String())
|
||||
assert.Equal(t, "13:14:15", person.Birthday5.String())
|
||||
assert.Equal(t, "2020-08-05 13:14:15", person.Birthday6.String())
|
||||
|
||||
fmt.Printf("Json string parse to person:\n%+v\n", person)
|
||||
}
|
||||
|
||||
func TestCarbon_UnmarshalJSON_UnLoadTag(t *testing.T) {
|
||||
str := `{
|
||||
"name": "gouguoyin",
|
||||
"age": 18,
|
||||
"birthday": "2020-08-05 13:14:15.999"
|
||||
"name":"gouguoyin",
|
||||
"age":18,
|
||||
"birthday0":"2020-08-05 13:14:15",
|
||||
"birthday1":"2020-08-05"
|
||||
}`
|
||||
|
||||
var student Student
|
||||
unmarshalErr := json.Unmarshal([]byte(str), &student)
|
||||
var person Person
|
||||
unmarshalErr := json.Unmarshal([]byte(str), &person)
|
||||
assert.Nil(t, unmarshalErr)
|
||||
|
||||
assert.Equal(t, "2020-08-05 13:14:15", student.Birthday.String())
|
||||
fmt.Printf("Json string parse to student:\n%+v\n", student)
|
||||
assert.Equal(t, "2020-08-05 13:14:15", person.Birthday0.String())
|
||||
assert.Equal(t, "2020-08-05 00:00:00", person.Birthday1.String())
|
||||
fmt.Printf("Json string parse to person:\n%+v\n", person)
|
||||
}
|
||||
|
||||
func TestError_JSON(t *testing.T) {
|
||||
student := Student{
|
||||
Name: "gouguoyin",
|
||||
Age: 18,
|
||||
Birthday: Parse("XXX"),
|
||||
person := Person{
|
||||
Name: "gouguoyin",
|
||||
Age: 18,
|
||||
Birthday1: Parse("XXX"),
|
||||
}
|
||||
|
||||
_, marshalErr := json.Marshal(student)
|
||||
fmt.Println("marshal error", marshalErr.Error())
|
||||
_, marshalErr := json.Marshal(person)
|
||||
fmt.Println("marshal error:", marshalErr.Error())
|
||||
assert.NotNil(t, marshalErr)
|
||||
|
||||
str := `{
|
||||
"name": "gouguoyin",
|
||||
"age": 18,
|
||||
"birthday": "2020-08-05 13:14:15.999"
|
||||
"birthday1": "2020-08-05 13:14:15"
|
||||
}`
|
||||
unmarshalErr := json.Unmarshal([]byte(str), &student)
|
||||
fmt.Println("unmarshal error", unmarshalErr.Error())
|
||||
unmarshalErr := json.Unmarshal([]byte(str), &person)
|
||||
fmt.Println("unmarshal error:", unmarshalErr.Error())
|
||||
assert.NotNil(t, unmarshalErr)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user