From 0ce8c5b87de1ed75bf4ed5a342ac1a49650b2d8f Mon Sep 17 00:00:00 2001 From: Peleus <245629560@qq.com> Date: Fri, 29 Dec 2023 16:39:32 +0800 Subject: [PATCH 01/14] Upgrade from v2.3.1 to 2.3.2 --- carbon.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/carbon.go b/carbon.go index 08df5999..12b33deb 100644 --- a/carbon.go +++ b/carbon.go @@ -14,7 +14,7 @@ import ( // Version current version // 当前版本号 -const Version = "2.3.1" +const Version = "2.3.2" // timezone constants // 时区常量 @@ -245,10 +245,10 @@ const ( type Carbon struct { time time.Time testNow int64 // timestamp with nanosecond of test now time - tag string weekStartsAt time.Weekday loc *time.Location lang *Language + tag tag Error error } From b8b63213b554d6c488cf13477f7c4ca94a035ded Mon Sep 17 00:00:00 2001 From: Peleus <245629560@qq.com> Date: Fri, 29 Dec 2023 16:40:07 +0800 Subject: [PATCH 02/14] Optimize code --- encoding.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/encoding.go b/encoding.go index 605a7689..2814472c 100644 --- a/encoding.go +++ b/encoding.go @@ -12,12 +12,17 @@ func (c Carbon) MarshalJSON() ([]byte, error) { return nil, c.Error } key, value, tz := c.parseTag() - data := fmt.Sprintf(`"%s"`, c.ToDateTimeString(tz)) + data := "" if key == "layout" { data = fmt.Sprintf(`"%s"`, c.Layout(value, tz)) } if key == "format" { - data = fmt.Sprintf(`"%s"`, c.Format(value, tz)) + // timestamp without double quotes in json + if value == "U" || value == "V" || value == "X" || value == "Z" { + data = fmt.Sprintf(`%s`, c.Format(value, tz)) + } else { + data = fmt.Sprintf(`"%s"`, c.Format(value, tz)) + } } return []byte(data), nil } @@ -32,11 +37,13 @@ func (c *Carbon) UnmarshalJSON(b []byte) error { data := fmt.Sprintf("%s", bytes.Trim(b, `"`)) if key == "layout" { *c = ParseByLayout(data, value, tz) - c.tag = fmt.Sprintf("layout:%s;tz:%s", value, tz) } if key == "format" { *c = ParseByFormat(data, value, tz) - c.tag = fmt.Sprintf("format:%s;tz:%s", value, tz) + } + c.tag = tag{ + carbon: fmt.Sprintf("%s:%s", key, value), + tz: tz, } return c.Error } From 48733d3564d344916165d2649d58256f14fc2041 Mon Sep 17 00:00:00 2001 From: Peleus <245629560@qq.com> Date: Fri, 29 Dec 2023 16:40:24 +0800 Subject: [PATCH 03/14] Add test cases --- encoding_unit_test.go | 53 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 46 insertions(+), 7 deletions(-) diff --git a/encoding_unit_test.go b/encoding_unit_test.go index 1d8de12b..02033ec7 100644 --- a/encoding_unit_test.go +++ b/encoding_unit_test.go @@ -12,7 +12,10 @@ type Person struct { Name string `json:"name"` Age int `json:"age"` - Birthday0 Carbon `json:"birthday0"` + Birthday01 Carbon `json:"birthday01"` + Birthday02 Carbon `json:"birthday02" carbon:"dateTime"` + Birthday03 Carbon `json:"birthday03" tz:"PRC"` + Birthday04 Carbon `json:"birthday04" carbon:"" tz:""` Birthday1 Carbon `json:"birthday1" carbon:"layout:2006-01-02"` Birthday2 Carbon `json:"birthday2" carbon:"layout:15:04:05"` @@ -76,13 +79,21 @@ type Person struct { Birthday58 Carbon `json:"birthday58" carbon:"iso8601Milli"` Birthday59 Carbon `json:"birthday59" carbon:"iso8601Micro"` Birthday60 Carbon `json:"birthday60" carbon:"iso8601Nano"` + + Birthday61 Carbon `json:"birthday61" carbon:"timestamp"` + Birthday62 Carbon `json:"birthday62" carbon:"timestampMilli"` + Birthday63 Carbon `json:"birthday63" carbon:"timestampMicro"` + Birthday64 Carbon `json:"birthday64" carbon:"timestampNano"` } var c = Parse("2020-08-05 13:14:15.999999999", PRC) var person = Person{ Name: "gouguoyin", Age: 18, - Birthday0: c, + Birthday01: c, + Birthday02: c, + Birthday03: c, + Birthday04: c, Birthday1: c, Birthday2: c, Birthday3: c, @@ -143,6 +154,10 @@ var person = Person{ Birthday58: c, Birthday59: c, Birthday60: c, + Birthday61: c, + Birthday62: c, + Birthday63: c, + Birthday64: c, } func TestCarbon_MarshalJSON(t *testing.T) { @@ -152,7 +167,10 @@ func TestCarbon_MarshalJSON(t *testing.T) { data, marshalErr := json.Marshal(&person) assert.Nil(t, marshalErr) - assert.Equal(t, "2020-08-05 13:14:15", person.Birthday0.String()) + assert.Equal(t, "2020-08-05 13:14:15", person.Birthday01.String()) + assert.Equal(t, "2020-08-05 13:14:15", person.Birthday02.String()) + assert.Equal(t, "2020-08-05 13:14:15", person.Birthday03.String()) + assert.Equal(t, "2020-08-05 13:14:15", person.Birthday04.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()) @@ -213,6 +231,10 @@ func TestCarbon_MarshalJSON(t *testing.T) { assert.Equal(t, "2020-08-05T13:14:15.999+08:00", person.Birthday58.String()) assert.Equal(t, "2020-08-05T13:14:15.999999+08:00", person.Birthday59.String()) assert.Equal(t, "2020-08-05T13:14:15.999999999+08:00", person.Birthday60.String()) + assert.Equal(t, "1596604455", person.Birthday61.String()) + assert.Equal(t, "1596604455999", person.Birthday62.String()) + assert.Equal(t, "1596604455999999", person.Birthday63.String()) + assert.Equal(t, "1596604455999999999", person.Birthday64.String()) fmt.Printf("person output by json:\n%s\n", data) } @@ -221,7 +243,10 @@ func TestCarbon_UnmarshalJSON(t *testing.T) { str := `{ "name": "gouguoyin", "age": 18, - "birthday0": "2020-08-05 13:14:15", + "birthday01": "2020-08-05 13:14:15", + "birthday02": "2020-08-05 13:14:15", + "birthday03": "2020-08-05 13:14:15", + "birthday04": "2020-08-05 13:14:15", "birthday1": "2020-08-05", "birthday2": "13:14:15", "birthday3": "2020-08-05 13:14:15", @@ -281,7 +306,11 @@ func TestCarbon_UnmarshalJSON(t *testing.T) { "birthday57": "2020-08-05T13:14:15+08:00", "birthday58": "2020-08-05T13:14:15.999+08:00", "birthday59": "2020-08-05T13:14:15.999999+08:00", - "birthday60": "2020-08-05T13:14:15.999999999+08:00" + "birthday60": "2020-08-05T13:14:15.999999999+08:00", + "birthday61": 1596604455, + "birthday62": 1596604455999, + "birthday63": 1596604455999999, + "birthday64": 1596604455999999999 }` loadErr := LoadTag(&person) @@ -290,7 +319,10 @@ func TestCarbon_UnmarshalJSON(t *testing.T) { unmarshalErr := json.Unmarshal([]byte(str), &person) assert.Nil(t, unmarshalErr) - assert.Equal(t, "2020-08-05 13:14:15", person.Birthday0.String()) + assert.Equal(t, "2020-08-05 13:14:15", person.Birthday01.String()) + assert.Equal(t, "2020-08-05 13:14:15", person.Birthday02.String()) + assert.Equal(t, "2020-08-05 13:14:15", person.Birthday03.String()) + assert.Equal(t, "2020-08-05 13:14:15", person.Birthday04.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()) @@ -351,6 +383,10 @@ func TestCarbon_UnmarshalJSON(t *testing.T) { assert.Equal(t, "2020-08-05T13:14:15.999+08:00", person.Birthday58.String()) assert.Equal(t, "2020-08-05T13:14:15.999999+08:00", person.Birthday59.String()) assert.Equal(t, "2020-08-05T13:14:15.999999999+08:00", person.Birthday60.String()) + assert.Equal(t, "1596604455", person.Birthday61.String()) + assert.Equal(t, "1596604455999", person.Birthday62.String()) + assert.Equal(t, "1596604455999999", person.Birthday63.String()) + assert.Equal(t, "1596604455999999999", person.Birthday64.String()) fmt.Printf("Json string parse to person:\n%+v\n", person) } @@ -361,6 +397,7 @@ func TestError_Json(t *testing.T) { Birthday2 Carbon `json:"birthday2" carbon:"layout:xxx"` Birthday3 Carbon `json:"birthday3" carbon:"format:xxx"` Birthday4 Carbon `json:"birthday4" carbon:"xxx"` + Birthday5 Carbon `json:"birthday5" tz:"xxx"` } student := Student{ @@ -368,6 +405,7 @@ func TestError_Json(t *testing.T) { Birthday2: Parse("2020-08-05"), Birthday3: Parse("2020-08-05"), Birthday4: Parse("2020-08-05"), + Birthday5: Parse("2020-08-05"), } _, marshalErr := json.Marshal(student) @@ -380,7 +418,8 @@ func TestError_Json(t *testing.T) { "birthday1": "2020-08-05 13:14:15", "birthday2": "2020-08-05 13:14:15", "birthday3": "2020-08-05 13:14:15", - "birthday4": "2020-08-05 13:14:15" + "birthday4": "2020-08-05 13:14:15", + "birthday5": "2020-08-05 13:14:15" }` unmarshalErr := json.Unmarshal([]byte(str), &student) From 4a1642d3521a27e249c164a1bdf31487ebc456c6 Mon Sep 17 00:00:00 2001 From: Peleus <245629560@qq.com> Date: Fri, 29 Dec 2023 16:41:57 +0800 Subject: [PATCH 04/14] Add `tag` struct, Optimize `LoadTag` method --- tag.go | 163 +++++++++++++++++++++++++++++++-------------------------- 1 file changed, 89 insertions(+), 74 deletions(-) diff --git a/tag.go b/tag.go index 45e4fb2f..a734526c 100644 --- a/tag.go +++ b/tag.go @@ -20,15 +20,16 @@ var ( } ) -// Tag gets tag. -// 获取标签 -func (c Carbon) Tag() string { - return c.tag +// tag defines a tag struct. +// 定义 tag 结构体 +type tag struct { + carbon string + tz string } // SetTag sets tag. // 设置标签 -func (c Carbon) SetTag(tag string) Carbon { +func (c Carbon) SetTag(tag tag) Carbon { if c.Error != nil { return c } @@ -39,148 +40,162 @@ func (c Carbon) SetTag(tag string) Carbon { // parseTag parses tag. // 解析标签 func (c Carbon) parseTag() (key, value, tz string) { - if c.tag == "" || len(c.tag) <= 7 { - return "", "", Local + tz = strings.TrimSpace(c.tag.tz) + if tz == "" { + tz = c.Location() } - slice := strings.Split(strings.Trim(c.tag, ";"), ";") - key = strings.TrimSpace(slice[0][:6]) - value = strings.TrimSpace(slice[0][7:]) - tz = strings.TrimSpace(slice[1][3:]) + carbon := strings.TrimSpace(c.tag.carbon) + if carbon == "" || len(carbon) <= 7 { + return "", "", tz + } + key = strings.TrimSpace(carbon[:6]) + value = strings.TrimSpace(carbon[7:]) return } // LoadTag loads tag. // 加载标签 func LoadTag(v interface{}) error { - typ := reflect.TypeOf(v) - val := reflect.ValueOf(v) - if typ.Kind() != reflect.Ptr { + typeObj, valueObj := reflect.TypeOf(v), reflect.ValueOf(v) + if typeObj.Kind() != reflect.Ptr { return invalidPtrError() } + typeElem, valueElem := typeObj.Elem(), valueObj.Elem() params := make([]reflect.Value, 1) - for i := 0; i < val.Elem().NumField(); i++ { - field := typ.Elem().Field(i) - value := val.Elem().Field(i) - if reflect.TypeOf(Carbon{}) != value.Type() { + for i := 0; i < valueElem.NumField(); i++ { + fieldType, fieldValue := typeElem.Field(i), valueElem.Field(i) + if reflect.TypeOf(Carbon{}) != fieldValue.Type() { continue } - carbonTag := field.Tag.Get("carbon") - switch carbonTag { + carbon := fieldType.Tag.Get("carbon") + switch carbon { case "", "dateTime": - carbonTag = "layout:" + DateTimeLayout + carbon = "layout:" + DateTimeLayout case "dateTimeMilli": - carbonTag = "layout:" + DateTimeMilliLayout + carbon = "layout:" + DateTimeMilliLayout case "dateTimeMicro": - carbonTag = "layout:" + DateTimeMicroLayout + carbon = "layout:" + DateTimeMicroLayout case "dateTimeNano": - carbonTag = "layout:" + DateTimeNanoLayout + carbon = "layout:" + DateTimeNanoLayout case "shortDateTime": - carbonTag = "layout:" + ShortDateTimeLayout + carbon = "layout:" + ShortDateTimeLayout case "shortDateTimeMilli": - carbonTag = "layout:" + ShortDateTimeMilliLayout + carbon = "layout:" + ShortDateTimeMilliLayout case "shortDateTimeMicro": - carbonTag = "layout:" + ShortDateTimeMicroLayout + carbon = "layout:" + ShortDateTimeMicroLayout case "shortDateTimeNano": - carbonTag = "layout:" + ShortDateTimeNanoLayout + carbon = "layout:" + ShortDateTimeNanoLayout case "dayDateTime": - carbonTag = "layout:" + DayDateTimeLayout + carbon = "layout:" + DayDateTimeLayout case "date": - carbonTag = "layout:" + DateLayout + carbon = "layout:" + DateLayout case "dateMilli": - carbonTag = "layout:" + DateMilliLayout + carbon = "layout:" + DateMilliLayout case "dateMicro": - carbonTag = "layout:" + DateMicroLayout + carbon = "layout:" + DateMicroLayout case "dateNano": - carbonTag = "layout:" + DateNanoLayout + carbon = "layout:" + DateNanoLayout case "shortDate": - carbonTag = "layout:" + ShortDateLayout + carbon = "layout:" + ShortDateLayout case "shortDateMilli": - carbonTag = "layout:" + ShortDateMilliLayout + carbon = "layout:" + ShortDateMilliLayout case "shortDateMicro": - carbonTag = "layout:" + ShortDateMicroLayout + carbon = "layout:" + ShortDateMicroLayout case "shortDateNano": - carbonTag = "layout:" + ShortDateNanoLayout + carbon = "layout:" + ShortDateNanoLayout case "time": - carbonTag = "layout:" + TimeLayout + carbon = "layout:" + TimeLayout case "timeMilli": - carbonTag = "layout:" + TimeMilliLayout + carbon = "layout:" + TimeMilliLayout case "timeMicro": - carbonTag = "layout:" + TimeMicroLayout + carbon = "layout:" + TimeMicroLayout case "timeNano": - carbonTag = "layout:" + TimeNanoLayout + carbon = "layout:" + TimeNanoLayout case "shortTime": - carbonTag = "layout:" + ShortTimeLayout + carbon = "layout:" + ShortTimeLayout case "shortTimeMilli": - carbonTag = "layout:" + ShortTimeMilliLayout + carbon = "layout:" + ShortTimeMilliLayout case "shortTimeMicro": - carbonTag = "layout:" + ShortTimeMicroLayout + carbon = "layout:" + ShortTimeMicroLayout case "shortTimeNano": - carbonTag = "layout:" + ShortTimeNanoLayout + carbon = "layout:" + ShortTimeNanoLayout case "atom": - carbonTag = "layout:" + AtomLayout + carbon = "layout:" + AtomLayout case "ansic": - carbonTag = "layout:" + ANSICLayout + carbon = "layout:" + ANSICLayout case "cookie": - carbonTag = "layout:" + CookieLayout + carbon = "layout:" + CookieLayout case "kitchen": - carbonTag = "layout:" + KitchenLayout + carbon = "layout:" + KitchenLayout case "rss": - carbonTag = "layout:" + RssLayout + carbon = "layout:" + RssLayout case "rubyDate": - carbonTag = "layout:" + RubyDateLayout + carbon = "layout:" + RubyDateLayout case "unixDate": - carbonTag = "layout:" + UnixDateLayout + carbon = "layout:" + UnixDateLayout case "rfc1036": - carbonTag = "layout:" + RFC1036Layout + carbon = "layout:" + RFC1036Layout case "rfc1123": - carbonTag = "layout:" + RFC1123Layout + carbon = "layout:" + RFC1123Layout case "rfc1123Z": - carbonTag = "layout:" + RFC1123ZLayout + carbon = "layout:" + RFC1123ZLayout case "rfc2822": - carbonTag = "layout:" + RFC2822Layout + carbon = "layout:" + RFC2822Layout case "rfc3339": - carbonTag = "layout:" + RFC3339Layout + carbon = "layout:" + RFC3339Layout case "rfc3339Milli": - carbonTag = "layout:" + RFC3339MilliLayout + carbon = "layout:" + RFC3339MilliLayout case "rfc3339Micro": - carbonTag = "layout:" + RFC3339MicroLayout + carbon = "layout:" + RFC3339MicroLayout case "rfc3339Nano": - carbonTag = "layout:" + RFC3339NanoLayout + carbon = "layout:" + RFC3339NanoLayout case "rfc7231": - carbonTag = "layout:" + RFC7231Layout + carbon = "layout:" + RFC7231Layout case "rfc822": - carbonTag = "layout:" + RFC822Layout + carbon = "layout:" + RFC822Layout case "rfc822Z": - carbonTag = "layout:" + RFC822ZLayout + carbon = "layout:" + RFC822ZLayout case "rfc850": - carbonTag = "layout:" + RFC850Layout + carbon = "layout:" + RFC850Layout case "iso8601": - carbonTag = "layout:" + ISO8601Layout + carbon = "layout:" + ISO8601Layout case "iso8601Milli": - carbonTag = "layout:" + ISO8601MilliLayout + carbon = "layout:" + ISO8601MilliLayout case "iso8601Micro": - carbonTag = "layout:" + ISO8601MicroLayout + carbon = "layout:" + ISO8601MicroLayout case "iso8601Nano": - carbonTag = "layout:" + ISO8601NanoLayout + carbon = "layout:" + ISO8601NanoLayout + + case "timestamp": + carbon = "format:U" + case "timestampMilli": + carbon = "format:V" + case "timestampMicro": + carbon = "format:X" + case "timestampNano": + carbon = "format:Z" } - if !strings.Contains(carbonTag, "layout:") && !strings.Contains(carbonTag, "format:") { + if !strings.Contains(carbon, "layout:") && !strings.Contains(carbon, "format:") { return invalidTagError() } - tzTag := field.Tag.Get("tz") - if tzTag == "" { - tzTag = Local + tz := fieldType.Tag.Get("tz") + if tz == "" { + tz = Local } - params[0] = reflect.ValueOf(carbonTag + ";tz:" + tzTag) - value.Set(value.MethodByName("SetTag").Call(params)[0]) + params[0] = reflect.ValueOf(tag{ + carbon: carbon, + tz: tz, + }) + fieldValue.Set(fieldValue.MethodByName("SetTag").Call(params)[0]) } return nil } From 70c1bfd74dc7c509e7ab779cfb2454f757140728 Mon Sep 17 00:00:00 2001 From: Peleus <245629560@qq.com> Date: Fri, 29 Dec 2023 16:42:22 +0800 Subject: [PATCH 05/14] Add test cases --- tag_bench_test.go | 12 ++++-------- tag_unit_test.go | 19 +++++++++++-------- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/tag_bench_test.go b/tag_bench_test.go index 6b218201..c43d824f 100755 --- a/tag_bench_test.go +++ b/tag_bench_test.go @@ -2,17 +2,13 @@ package carbon import "testing" -func BenchmarkCarbon_Tag(b *testing.B) { - now := Now() - for n := 0; n < b.N; n++ { - now.Tag() - } -} - func BenchmarkCarbon_SetTag(b *testing.B) { now := Now() for n := 0; n < b.N; n++ { - now.SetTag("layout:2006-01-02") + now.SetTag(tag{ + carbon: "datetime", + tz: Local, + }) } } diff --git a/tag_unit_test.go b/tag_unit_test.go index acde7ebb..0b2b20d0 100644 --- a/tag_unit_test.go +++ b/tag_unit_test.go @@ -5,18 +5,21 @@ import ( "testing" ) -func TestCarbon_Tag(t *testing.T) { - now := Now().SetTag("layout") - assert.Equal(t, "layout", now.Tag()) -} - func TestCarbon_SetTag(t *testing.T) { - now := Now().SetTag("layout") - assert.Equal(t, "layout", now.tag) + now := Now().SetTag(tag{ + carbon: "datetime", + tz: Local, + }) + assert.Nil(t, now.Error) + assert.Equal(t, "datetime", now.tag.carbon) + assert.Equal(t, Local, now.tag.tz) } func TestError_SetTag(t *testing.T) { - now := Now("xxx").SetTag("layout") + now := Now("xxx").SetTag(tag{ + carbon: "datetime", + tz: Local, + }) assert.NotNil(t, now.Error) } From 40c242e5547c4689397fc267ac256700e6157fa3 Mon Sep 17 00:00:00 2001 From: Peleus <245629560@qq.com> Date: Fri, 29 Dec 2023 16:43:06 +0800 Subject: [PATCH 06/14] Optimize code --- outputer.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/outputer.go b/outputer.go index f1df8a88..b3d79e44 100644 --- a/outputer.go +++ b/outputer.go @@ -18,7 +18,7 @@ func (c Carbon) String() string { if key == "format" { return c.Format(value, tz) } - return c.ToDateTimeString(tz) + return c.ToDateTimeString(c.Location()) } // ToString outputs a string in "2006-01-02 15:04:05.999999999 -0700 MST" layout. From 2655be5d4f8ae839eca6969a7e6c008e9bf6980d Mon Sep 17 00:00:00 2001 From: Peleus <245629560@qq.com> Date: Fri, 29 Dec 2023 16:45:15 +0800 Subject: [PATCH 07/14] Updat description about JSON --- README.cn.md | 97 +++++++++++++++++++++++++++--------------------- README.jp.md | 101 ++++++++++++++++++++++++++++---------------------- README.md | 103 ++++++++++++++++++++++++++++----------------------- 3 files changed, 167 insertions(+), 134 deletions(-) diff --git a/README.cn.md b/README.cn.md index c8cf92a7..2eacbc94 100644 --- a/README.cn.md +++ b/README.cn.md @@ -1267,13 +1267,15 @@ type Person struct { Name string `json:"name"` Age int `json:"age"` - Birthday0 Carbon `json:"birthday0"` - Birthday1 Carbon `json:"birthday1" carbon:"date"` - Birthday2 Carbon `json:"birthday2" carbon:"time"` - Birthday3 Carbon `json:"birthday3" carbon:"dateTime"` - Birthday4 Carbon `json:"birthday4" carbon:"date" tz:"PRC"` - Birthday5 Carbon `json:"birthday5" carbon:"time" tz:"PRC"` - Birthday6 Carbon `json:"birthday6" carbon:"dateTime" tz:"PRC"` + Birthday1 Carbon `json:"birthday1"` + Birthday2 Carbon `json:"birthday2" carbon:"date" tz:"PRC"` + Birthday3 Carbon `json:"birthday3" carbon:"time" tz:"PRC"` + Birthday4 Carbon `json:"birthday4" carbon:"dateTime" tz:"PRC"` + + Birthday5 Carbon `json:"birthday5" carbon:"timestamp" tz:"PRC"` + Birthday6 Carbon `json:"birthday6" carbon:"timestampMilli" tz:"PRC"` + Birthday7 Carbon `json:"birthday7" carbon:"timestampMicro" tz:"PRC"` + Birthday8 Carbon `json:"birthday8" carbon:"timestampNano" tz:"PRC"` } ``` @@ -1284,13 +1286,15 @@ type Person struct { Name string `json:"name"` Age int `json:"age"` - Birthday0 Carbon `json:"birthday0"` - 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"` - Birthday4 Carbon `json:"birthday4" carbon:"layout:2006-01-02" tz:"PRC"` - Birthday5 Carbon `json:"birthday5" carbon:"layout:15:04:05" tz:"PRC"` - Birthday6 Carbon `json:"birthday6" carbon:"layout:2006-01-02 15:04:05" tz:"PRC"` + Birthday1 Carbon `json:"birthday1"` + Birthday2 Carbon `json:"birthday2" carbon:"layout:2006-01-02" tz:"PRC"` + Birthday3 Carbon `json:"birthday3" carbon:"layout:15:04:05" tz:"PRC"` + Birthday4 Carbon `json:"birthday4" carbon:"layout:2006-01-02 15:04:05" tz:"PRC"` + + Birthday5 Carbon `json:"birthday5" carbon:"timestamp" tz:"PRC"` + Birthday6 Carbon `json:"birthday6" carbon:"timestampMilli" tz:"PRC"` + Birthday7 Carbon `json:"birthday7" carbon:"timestampMicro" tz:"PRC"` + Birthday8 Carbon `json:"birthday8" carbon:"timestampNano" tz:"PRC"` } ``` @@ -1301,13 +1305,15 @@ type Person struct { Name string `json:"name"` Age int `json:"age"` - Birthday0 Carbon `json:"birthday0"` - Birthday1 Carbon `json:"birthday1" carbon:"format:Y-m-d"` - Birthday2 Carbon `json:"birthday2" carbon:"format:H:i:s"` - Birthday3 Carbon `json:"birthday3" carbon:"format:Y-m-d H:i:s"` - Birthday4 Carbon `json:"birthday4" carbon:"format:Y-m-d" tz:"PRC"` - Birthday5 Carbon `json:"birthday5" carbon:"format:H:i:s" tz:"PRC"` - Birthday6 Carbon `json:"birthday6" carbon:"format:Y-m-d H:i:s" tz:"PRC"` + Birthday1 Carbon `json:"birthday1"` + Birthday2 Carbon `json:"birthday2" carbon:"format:Y-m-d" tz:"PRC"` + Birthday3 Carbon `json:"birthday3" carbon:"format:H:i:s" tz:"PRC"` + Birthday4 Carbon `json:"birthday4" carbon:"format:Y-m-d H:i:s" tz:"PRC"` + + Birthday5 Carbon `json:"birthday5" carbon:"timestamp" tz:"PRC"` + Birthday6 Carbon `json:"birthday6" carbon:"timestampMilli" tz:"PRC"` + Birthday7 Carbon `json:"birthday7" carbon:"timestampMicro" tz:"PRC"` + Birthday8 Carbon `json:"birthday8" carbon:"timestampNano" tz:"PRC"` } ``` @@ -1321,13 +1327,14 @@ person := Person { Name: "gouguoyin", Age: 18, - Birthday0: now, Birthday1: now, Birthday2: now, Birthday3: now, Birthday4: now, Birthday5: now, Birthday6: now, + Birthday7: now, + Birthday8: now, } ``` @@ -1349,13 +1356,14 @@ fmt.Printf("%s", data) { "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": "213:14:15", - "birthday6": "2020-08-05 13:14:15" + "birthday1": "2020-08-05 13:14:15", + "birthday2": "2020-08-05", + "birthday3": "13:14:15", + "birthday4": "2020-08-05 13:14:15", + "birthday5": 1596604455, + "birthday6": 1596604455999, + "birthday7": 1596604455999999, + "birthday8": 1596604455999999999 } ``` @@ -1365,13 +1373,14 @@ fmt.Printf("%s", data) str := `{ "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": "213:14:15", - "birthday6": "2020-08-05 13:14:15" + "birthday1": "2020-08-05 13:14:15", + "birthday2": "2020-08-05", + "birthday3": "13:14:15", + "birthday4": "2020-08-05 13:14:15", + "birthday5": 1596604455, + "birthday6": 1596604455999, + "birthday7": 1596604455999999, + "birthday8": 1596604455999999999 }` var person Person @@ -1387,13 +1396,15 @@ if unmarshalErr != nil { log.Fatal(unmarshalErr) } -fmt.Sprintf("%s", person.Birthday0) // 2002-08-05 13:14:15 -fmt.Sprintf("%s", person.Birthday1) // 2020-08-05 -fmt.Sprintf("%s", person.Birthday2) // 13:14:15 -fmt.Sprintf("%s", person.Birthday3) // 2002-08-05 13:14:15 -fmt.Sprintf("%s", person.Birthday4) // 2002-08-05 -fmt.Sprintf("%s", person.Birthday5) // 13:14:15 -fmt.Sprintf("%s", person.Birthday6) // 2002-08-05 13:14:15 +fmt.Sprintf("%s", person.Birthday1) // 2002-08-05 13:14:15 +fmt.Sprintf("%s", person.Birthday2) // 2020-08-05 +fmt.Sprintf("%s", person.Birthday3) // 13:14:15 +fmt.Sprintf("%s", person.Birthday4) // 2002-08-05 13:14:15 + +fmt.Sprintf("%d", person.Birthday5) // 1596604455 +fmt.Sprintf("%d", person.Birthday6) // 1596604455999 +fmt.Sprintf("%d", person.Birthday7) // 1596604455999999 +fmt.Sprintf("%d", person.Birthday8) // 1596604455999999999 ``` ##### 国际化 diff --git a/README.jp.md b/README.jp.md index 9bdb6eb9..763e426a 100644 --- a/README.jp.md +++ b/README.jp.md @@ -1267,13 +1267,15 @@ type Person struct { Name string `json:"name"` Age int `json:"age"` - Birthday0 Carbon `json:"birthday0"` - Birthday1 Carbon `json:"birthday1" carbon:"date"` - Birthday2 Carbon `json:"birthday2" carbon:"time"` - Birthday3 Carbon `json:"birthday3" carbon:"dateTime"` - Birthday4 Carbon `json:"birthday4" carbon:"date" tz:"PRC"` - Birthday5 Carbon `json:"birthday5" carbon:"time" tz:"PRC"` - Birthday6 Carbon `json:"birthday6" carbon:"dateTime" tz:"PRC"` + Birthday1 Carbon `json:"birthday1"` + Birthday2 Carbon `json:"birthday2" carbon:"date" tz:"PRC"` + Birthday3 Carbon `json:"birthday3" carbon:"time" tz:"PRC"` + Birthday4 Carbon `json:"birthday4" carbon:"dateTime" tz:"PRC"` + + Birthday5 Carbon `json:"birthday5" carbon:"timestamp" tz:"PRC"` + Birthday6 Carbon `json:"birthday6" carbon:"timestampMilli" tz:"PRC"` + Birthday7 Carbon `json:"birthday7" carbon:"timestampMicro" tz:"PRC"` + Birthday8 Carbon `json:"birthday8" carbon:"timestampNano" tz:"PRC"` } ``` @@ -1284,13 +1286,15 @@ type Person struct { Name string `json:"name"` Age int `json:"age"` - Birthday0 Carbon `json:"birthday0"` - 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"` - Birthday4 Carbon `json:"birthday4" carbon:"layout:2006-01-02" tz:"PRC"` - Birthday5 Carbon `json:"birthday5" carbon:"layout:15:04:05" tz:"PRC"` - Birthday6 Carbon `json:"birthday6" carbon:"layout:2006-01-02 15:04:05" tz:"PRC"` + Birthday1 Carbon `json:"birthday1"` + Birthday2 Carbon `json:"birthday2" carbon:"layout:2006-01-02" tz:"PRC"` + Birthday3 Carbon `json:"birthday3" carbon:"layout:15:04:05" tz:"PRC"` + Birthday4 Carbon `json:"birthday4" carbon:"layout:2006-01-02 15:04:05" tz:"PRC"` + + Birthday5 Carbon `json:"birthday5" carbon:"timestamp" tz:"PRC"` + Birthday6 Carbon `json:"birthday6" carbon:"timestampMilli" tz:"PRC"` + Birthday7 Carbon `json:"birthday7" carbon:"timestampMicro" tz:"PRC"` + Birthday8 Carbon `json:"birthday8" carbon:"timestampNano" tz:"PRC"` } ``` @@ -1301,13 +1305,15 @@ type Person struct { Name string `json:"name"` Age int `json:"age"` - Birthday0 Carbon `json:"birthday0"` - Birthday1 Carbon `json:"birthday1" carbon:"format:Y-m-d"` - Birthday2 Carbon `json:"birthday2" carbon:"format:H:i:s"` - Birthday3 Carbon `json:"birthday3" carbon:"format:Y-m-d H:i:s"` - Birthday4 Carbon `json:"birthday4" carbon:"format:Y-m-d" tz:"PRC"` - Birthday5 Carbon `json:"birthday5" carbon:"format:H:i:s" tz:"PRC"` - Birthday6 Carbon `json:"birthday6" carbon:"format:Y-m-d H:i:s" tz:"PRC"` + Birthday1 Carbon `json:"birthday1"` + Birthday2 Carbon `json:"birthday2" carbon:"format:Y-m-d" tz:"PRC"` + Birthday3 Carbon `json:"birthday3" carbon:"format:H:i:s" tz:"PRC"` + Birthday4 Carbon `json:"birthday4" carbon:"format:Y-m-d H:i:s" tz:"PRC"` + + Birthday5 Carbon `json:"birthday5" carbon:"timestamp" tz:"PRC"` + Birthday6 Carbon `json:"birthday6" carbon:"timestampMilli" tz:"PRC"` + Birthday7 Carbon `json:"birthday7" carbon:"timestampMicro" tz:"PRC"` + Birthday8 Carbon `json:"birthday8" carbon:"timestampNano" tz:"PRC"` } ``` @@ -1317,14 +1323,15 @@ now := Parse("2020-08-05 13:14:15", PRC) person := Person { Name: "gouguoyin", Age: 18, - - Birthday0: now, - Birthday1: now, + + Birthday1: now, Birthday2: now, Birthday3: now, Birthday4: now, Birthday5: now, Birthday6: now, + Birthday7: now, + Birthday8: now, } ``` @@ -1346,13 +1353,14 @@ fmt.Printf("%s", data) { "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": "213:14:15", - "birthday6": "2020-08-05 13:14:15" + "birthday1": "2020-08-05 13:14:15", + "birthday2": "2020-08-05", + "birthday3": "13:14:15", + "birthday4": "2020-08-05 13:14:15", + "birthday5": 1596604455, + "birthday6": 1596604455999, + "birthday7": 1596604455999999, + "birthday8": 1596604455999999999 } ``` @@ -1362,13 +1370,14 @@ fmt.Printf("%s", data) str := `{ "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": "213:14:15", - "birthday6": "2020-08-05 13:14:15" + "birthday1": "2020-08-05 13:14:15", + "birthday2": "2020-08-05", + "birthday3": "13:14:15", + "birthday4": "2020-08-05 13:14:15", + "birthday5": 1596604455, + "birthday6": 1596604455999, + "birthday7": 1596604455999999, + "birthday8": 1596604455999999999 }` var person Person @@ -1384,13 +1393,15 @@ if unmarshalErr != nil { log.Fatal(unmarshalErr) } -fmt.Sprintf("%s", person.Birthday0) // 2002-08-05 13:14:15 -fmt.Sprintf("%s", person.Birthday1) // 2020-08-05 -fmt.Sprintf("%s", person.Birthday2) // 13:14:15 -fmt.Sprintf("%s", person.Birthday3) // 2002-08-05 13:14:15 -fmt.Sprintf("%s", person.Birthday4) // 2002-08-05 -fmt.Sprintf("%s", person.Birthday5) // 13:14:15 -fmt.Sprintf("%s", person.Birthday6) // 2002-08-05 13:14:15 +fmt.Sprintf("%s", person.Birthday1) // 2002-08-05 13:14:15 +fmt.Sprintf("%s", person.Birthday2) // 2020-08-05 +fmt.Sprintf("%s", person.Birthday3) // 13:14:15 +fmt.Sprintf("%s", person.Birthday4) // 2002-08-05 13:14:15 + +fmt.Sprintf("%d", person.Birthday5) // 1596604455 +fmt.Sprintf("%d", person.Birthday6) // 1596604455999 +fmt.Sprintf("%d", person.Birthday7) // 1596604455999999 +fmt.Sprintf("%d", person.Birthday8) // 1596604455999999999 ``` ##### 国際化 diff --git a/README.md b/README.md index 676cedce..628498b1 100644 --- a/README.md +++ b/README.md @@ -1259,16 +1259,18 @@ carbon.Parse("2020-03-21 21:00:00").Lunar().IsTwelfthDoubleHour() // true ```go type Person struct { - Name string `json:"name"` - Age int `json:"age"` - - Birthday0 Carbon `json:"birthday0"` - Birthday1 Carbon `json:"birthday1" carbon:"date"` - Birthday2 Carbon `json:"birthday2" carbon:"time"` - Birthday3 Carbon `json:"birthday3" carbon:"dateTime"` - Birthday4 Carbon `json:"birthday4" carbon:"date" tz:"PRC"` - Birthday5 Carbon `json:"birthday5" carbon:"time" tz:"PRC"` - Birthday6 Carbon `json:"birthday6" carbon:"dateTime" tz:"PRC"` + Name string `json:"name"` + Age int `json:"age"` + + Birthday1 Carbon `json:"birthday1"` + Birthday2 Carbon `json:"birthday2" carbon:"date" tz:"PRC"` + Birthday3 Carbon `json:"birthday3" carbon:"time" tz:"PRC"` + Birthday4 Carbon `json:"birthday4" carbon:"dateTime" tz:"PRC"` + + Birthday5 Carbon `json:"birthday5" carbon:"timestamp" tz:"PRC"` + Birthday6 Carbon `json:"birthday6" carbon:"timestampMilli" tz:"PRC"` + Birthday7 Carbon `json:"birthday7" carbon:"timestampMicro" tz:"PRC"` + Birthday8 Carbon `json:"birthday8" carbon:"timestampNano" tz:"PRC"` } ``` @@ -1279,13 +1281,15 @@ type Person struct { Name string `json:"name"` Age int `json:"age"` - Birthday0 Carbon `json:"birthday0"` - 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"` - Birthday4 Carbon `json:"birthday4" carbon:"layout:2006-01-02" tz:"PRC"` - Birthday5 Carbon `json:"birthday5" carbon:"layout:15:04:05" tz:"PRC"` - Birthday6 Carbon `json:"birthday6" carbon:"layout:2006-01-02 15:04:05" tz:"PRC"` + Birthday1 Carbon `json:"birthday1"` + Birthday2 Carbon `json:"birthday2" carbon:"layout:2006-01-02" tz:"PRC"` + Birthday3 Carbon `json:"birthday3" carbon:"layout:15:04:05" tz:"PRC"` + Birthday4 Carbon `json:"birthday4" carbon:"layout:2006-01-02 15:04:05" tz:"PRC"` + + Birthday5 Carbon `json:"birthday5" carbon:"timestamp" tz:"PRC"` + Birthday6 Carbon `json:"birthday6" carbon:"timestampMilli" tz:"PRC"` + Birthday7 Carbon `json:"birthday7" carbon:"timestampMicro" tz:"PRC"` + Birthday8 Carbon `json:"birthday8" carbon:"timestampNano" tz:"PRC"` } ``` @@ -1296,13 +1300,15 @@ type Person struct { Name string `json:"name"` Age int `json:"age"` - Birthday0 Carbon `json:"birthday0"` - Birthday1 Carbon `json:"birthday1" carbon:"format:Y-m-d"` - Birthday2 Carbon `json:"birthday2" carbon:"format:H:i:s"` - Birthday3 Carbon `json:"birthday3" carbon:"format:Y-m-d H:i:s"` - Birthday4 Carbon `json:"birthday4" carbon:"format:Y-m-d" tz:"PRC"` - Birthday5 Carbon `json:"birthday5" carbon:"format:H:i:s" tz:"PRC"` - Birthday6 Carbon `json:"birthday6" carbon:"format:Y-m-d H:i:s" tz:"PRC"` + Birthday1 Carbon `json:"birthday1"` + Birthday2 Carbon `json:"birthday2" carbon:"format:Y-m-d" tz:"PRC"` + Birthday3 Carbon `json:"birthday3" carbon:"format:H:i:s" tz:"PRC"` + Birthday4 Carbon `json:"birthday4" carbon:"format:Y-m-d H:i:s" tz:"PRC"` + + Birthday5 Carbon `json:"birthday5" carbon:"timestamp" tz:"PRC"` + Birthday6 Carbon `json:"birthday6" carbon:"timestampMilli" tz:"PRC"` + Birthday7 Carbon `json:"birthday7" carbon:"timestampMicro" tz:"PRC"` + Birthday8 Carbon `json:"birthday8" carbon:"timestampNano" tz:"PRC"` } ``` @@ -1315,13 +1321,14 @@ person := Person { Name: "gouguoyin", Age: 18, - Birthday0: now, Birthday1: now, Birthday2: now, Birthday3: now, Birthday4: now, Birthday5: now, Birthday6: now, + Birthday7: now, + Birthday8: now, } ``` @@ -1343,13 +1350,14 @@ fmt.Printf("%s", data) { "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": "213:14:15", - "birthday6": "2020-08-05 13:14:15" + "birthday1": "2020-08-05 13:14:15", + "birthday2": "2020-08-05", + "birthday3": "13:14:15", + "birthday4": "2020-08-05 13:14:15", + "birthday5": 1596604455, + "birthday6": 1596604455999, + "birthday7": 1596604455999999, + "birthday8": 1596604455999999999 } ``` @@ -1359,13 +1367,14 @@ fmt.Printf("%s", data) str := `{ "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": "213:14:15", - "birthday6": "2020-08-05 13:14:15" + "birthday1": "2020-08-05 13:14:15", + "birthday2": "2020-08-05", + "birthday3": "13:14:15", + "birthday4": "2020-08-05 13:14:15", + "birthday5": 1596604455, + "birthday6": 1596604455999, + "birthday7": 1596604455999999, + "birthday8": 1596604455999999999 }` var person Person @@ -1381,13 +1390,15 @@ if unmarshalErr != nil { log.Fatal(unmarshalErr) } -fmt.Sprintf("%s", person.Birthday0) // 2002-08-05 13:14:15 -fmt.Sprintf("%s", person.Birthday1) // 2020-08-05 -fmt.Sprintf("%s", person.Birthday2) // 13:14:15 -fmt.Sprintf("%s", person.Birthday3) // 2002-08-05 13:14:15 -fmt.Sprintf("%s", person.Birthday4) // 2002-08-05 -fmt.Sprintf("%s", person.Birthday5) // 13:14:15 -fmt.Sprintf("%s", person.Birthday6) // 2002-08-05 13:14:15 +fmt.Sprintf("%s", person.Birthday1) // 2002-08-05 13:14:15 +fmt.Sprintf("%s", person.Birthday2) // 2020-08-05 +fmt.Sprintf("%s", person.Birthday3) // 13:14:15 +fmt.Sprintf("%s", person.Birthday4) // 2002-08-05 13:14:15 + +fmt.Sprintf("%d", person.Birthday5) // 1596604455 +fmt.Sprintf("%d", person.Birthday6) // 1596604455999 +fmt.Sprintf("%d", person.Birthday7) // 1596604455999999 +fmt.Sprintf("%d", person.Birthday8) // 1596604455999999999 ``` ##### I18n From dbde437d6448780b6e68c2cedea0aa3ecc2bb052 Mon Sep 17 00:00:00 2001 From: Peleus <245629560@qq.com> Date: Fri, 29 Dec 2023 17:35:00 +0800 Subject: [PATCH 08/14] Upgrade from v2.3.1 to 2.3.2 --- carbon.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/carbon.go b/carbon.go index 12b33deb..180f2898 100644 --- a/carbon.go +++ b/carbon.go @@ -244,11 +244,11 @@ const ( // 定义 Carbon 结构体 type Carbon struct { time time.Time - testNow int64 // timestamp with nanosecond of test now time + testNow int64 // nanosecond timestamp of test now time weekStartsAt time.Weekday + tag tag loc *time.Location lang *Language - tag tag Error error } From 11724369a44020dd26acb75fa17378cab54f10d0 Mon Sep 17 00:00:00 2001 From: Peleus <245629560@qq.com> Date: Mon, 1 Jan 2024 21:09:26 +0800 Subject: [PATCH 09/14] Fix bug --- outputer_unit_test.go | 64 ------------------------------------------- 1 file changed, 64 deletions(-) diff --git a/outputer_unit_test.go b/outputer_unit_test.go index 8b439214..aa326aa1 100644 --- a/outputer_unit_test.go +++ b/outputer_unit_test.go @@ -1789,70 +1789,6 @@ func TestCarbon_Format(t *testing.T) { } } -func TestCarbon_Format_Layout(t *testing.T) { - assert := assert.New(t) - - now := Now() - tests := []struct { - expected string - actual string - }{ - 0: {now.Format(AtomFormat), now.Layout(AtomLayout)}, - 1: {now.Format(ANSICFormat), now.Layout(ANSICLayout)}, - 2: {now.Format(CookieFormat), now.Layout(CookieLayout)}, - 3: {now.Format(KitchenFormat), now.Layout(KitchenLayout)}, - 4: {now.Format(RssFormat), now.Layout(RssLayout)}, - 5: {now.Format(RubyDateFormat), now.Layout(RubyDateLayout)}, - 6: {now.Format(UnixDateFormat), now.Layout(UnixDateLayout)}, - 7: {now.Format(RFC1036Format), now.Layout(RFC1036Layout)}, - 8: {now.Format(RFC1123Format), now.Layout(RFC1123Layout)}, - 9: {now.Format(RFC1123ZFormat), now.Layout(RFC1123ZLayout)}, - 10: {now.Format(RFC2822Format), now.Layout(RFC2822Layout)}, - 11: {now.Format(RFC3339Format), now.Layout(RFC3339Layout)}, - 12: {now.Format(RFC3339MilliFormat), now.Layout(RFC3339MilliLayout)}, - 13: {now.Format(RFC3339MicroFormat), now.Layout(RFC3339MicroLayout)}, - 14: {now.Format(RFC3339NanoFormat), now.Layout(RFC3339NanoLayout)}, - 15: {now.Format(RFC7231Format), now.Layout(RFC7231Layout)}, - 16: {now.Format(RFC822Format), now.Layout(RFC822Layout)}, - 17: {now.Format(RFC822ZFormat), now.Layout(RFC822ZLayout)}, - 18: {now.Format(RFC850Format), now.Layout(RFC850Layout)}, - 19: {now.Format(ISO8601Format), now.Layout(ISO8601Layout)}, - 20: {now.Format(ISO8601MilliFormat), now.Layout(ISO8601MilliLayout)}, - 21: {now.Format(ISO8601MicroFormat), now.Layout(ISO8601MicroLayout)}, - 22: {now.Format(ISO8601NanoFormat), now.Layout(ISO8601NanoLayout)}, - 23: {now.Format(DayDateTimeFormat), now.Layout(DayDateTimeLayout)}, - 24: {now.Format(DateTimeFormat), now.Layout(DateTimeLayout)}, - 25: {now.Format(DateTimeMilliFormat), now.Layout(DateTimeMilliLayout)}, - 26: {now.Format(DateTimeMicroFormat), now.Layout(DateTimeMicroLayout)}, - 27: {now.Format(DateTimeNanoFormat), now.Layout(DateTimeNanoLayout)}, - 28: {now.Format(ShortDateTimeFormat), now.Layout(ShortDateTimeLayout)}, - 29: {now.Format(ShortDateTimeMilliFormat), now.Layout(ShortDateTimeMilliLayout)}, - 30: {now.Format(ShortDateTimeMicroFormat), now.Layout(ShortDateTimeMicroLayout)}, - 31: {now.Format(ShortDateTimeNanoFormat), now.Layout(ShortDateTimeNanoLayout)}, - 32: {now.Format(DateFormat), now.Layout(DateLayout)}, - 33: {now.Format(DateMilliFormat), now.Layout(DateMilliLayout)}, - 34: {now.Format(DateMicroFormat), now.Layout(DateMicroLayout)}, - 35: {now.Format(DateNanoFormat), now.Layout(DateNanoLayout)}, - 36: {now.Format(ShortDateFormat), now.Layout(ShortDateLayout)}, - 37: {now.Format(ShortDateMilliFormat), now.Layout(ShortDateMilliLayout)}, - 38: {now.Format(ShortDateMicroFormat), now.Layout(ShortDateMicroLayout)}, - 39: {now.Format(ShortDateNanoFormat), now.Layout(ShortDateNanoLayout)}, - 40: {now.Format(TimeFormat), now.Layout(TimeLayout)}, - 41: {now.Format(TimeMilliFormat), now.Layout(TimeMilliLayout)}, - 42: {now.Format(TimeMicroFormat), now.Layout(TimeMicroLayout)}, - 43: {now.Format(TimeNanoFormat), now.Layout(TimeNanoLayout)}, - 44: {now.Format(ShortTimeFormat), now.Layout(ShortTimeLayout)}, - 45: {now.Format(ShortTimeMilliFormat), now.Layout(ShortTimeMilliLayout)}, - 46: {now.Format(ShortTimeMicroFormat), now.Layout(ShortTimeMicroLayout)}, - 47: {now.Format(ShortTimeNanoFormat), now.Layout(ShortTimeNanoLayout)}, - } - - for index, test := range tests { - assert.Equal(test.expected, test.actual, "Current test index is "+strconv.Itoa(index)) - } - -} - func TestCarbon_ToStdTime(t *testing.T) { expected := time.Now().Format(DateTimeLayout) actual := Now().ToStdTime().Format(DateTimeLayout) From 326b69ad9fb1c250360c60679c934c6e636daff4 Mon Sep 17 00:00:00 2001 From: peleus <245629560@qq.com> Date: Mon, 1 Jan 2024 21:15:04 +0800 Subject: [PATCH 10/14] Update outputer_unit_test.go From e390d9ebb139dcdb997cdc5448ca2593b1ccdf5d Mon Sep 17 00:00:00 2001 From: Peleus <245629560@qq.com> Date: Tue, 2 Jan 2024 22:53:43 +0800 Subject: [PATCH 11/14] Improve benchmark testing coverage --- boundary_bench_test.go | 60 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/boundary_bench_test.go b/boundary_bench_test.go index ef4dde93..7978322b 100755 --- a/boundary_bench_test.go +++ b/boundary_bench_test.go @@ -6,6 +6,9 @@ import ( func BenchmarkCarbon_StartOfCentury(b *testing.B) { now := Now() + for n := 0; n < b.N; n++ { + NewCarbon().StartOfCentury() + } for n := 0; n < b.N; n++ { now.StartOfCentury() } @@ -13,6 +16,9 @@ func BenchmarkCarbon_StartOfCentury(b *testing.B) { func BenchmarkCarbon_EndOfCentury(b *testing.B) { now := Now() + for n := 0; n < b.N; n++ { + NewCarbon().EndOfCentury() + } for n := 0; n < b.N; n++ { now.EndOfCentury() } @@ -20,6 +26,9 @@ func BenchmarkCarbon_EndOfCentury(b *testing.B) { func BenchmarkCarbon_StartOfDecade(b *testing.B) { now := Now() + for n := 0; n < b.N; n++ { + NewCarbon().StartOfDecade() + } for n := 0; n < b.N; n++ { now.StartOfDecade() } @@ -27,6 +36,9 @@ func BenchmarkCarbon_StartOfDecade(b *testing.B) { func BenchmarkCarbon_EndOfDecade(b *testing.B) { now := Now() + for n := 0; n < b.N; n++ { + NewCarbon().EndOfDecade() + } for n := 0; n < b.N; n++ { now.EndOfDecade() } @@ -34,6 +46,9 @@ func BenchmarkCarbon_EndOfDecade(b *testing.B) { func BenchmarkCarbon_StartOfYear(b *testing.B) { now := Now() + for n := 0; n < b.N; n++ { + NewCarbon().StartOfYear() + } for n := 0; n < b.N; n++ { now.StartOfYear() } @@ -41,6 +56,9 @@ func BenchmarkCarbon_StartOfYear(b *testing.B) { func BenchmarkCarbon_EndOfYear(b *testing.B) { now := Now() + for n := 0; n < b.N; n++ { + NewCarbon().EndOfYear() + } for n := 0; n < b.N; n++ { now.EndOfYear() } @@ -48,6 +66,9 @@ func BenchmarkCarbon_EndOfYear(b *testing.B) { func BenchmarkCarbon_StartOfQuarter(b *testing.B) { now := Now() + for n := 0; n < b.N; n++ { + NewCarbon().StartOfQuarter() + } for n := 0; n < b.N; n++ { now.StartOfQuarter() } @@ -55,6 +76,9 @@ func BenchmarkCarbon_StartOfQuarter(b *testing.B) { func BenchmarkCarbon_EndOfQuarter(b *testing.B) { now := Now() + for n := 0; n < b.N; n++ { + NewCarbon().EndOfQuarter() + } for n := 0; n < b.N; n++ { now.EndOfQuarter() } @@ -62,6 +86,9 @@ func BenchmarkCarbon_EndOfQuarter(b *testing.B) { func BenchmarkCarbon_StartOfMonth(b *testing.B) { now := Now() + for n := 0; n < b.N; n++ { + NewCarbon().StartOfMonth() + } for n := 0; n < b.N; n++ { now.StartOfMonth() } @@ -69,6 +96,9 @@ func BenchmarkCarbon_StartOfMonth(b *testing.B) { func BenchmarkCarbon_EndOfMonth(b *testing.B) { now := Now() + for n := 0; n < b.N; n++ { + NewCarbon().EndOfMonth() + } for n := 0; n < b.N; n++ { now.EndOfMonth() } @@ -76,6 +106,9 @@ func BenchmarkCarbon_EndOfMonth(b *testing.B) { func BenchmarkCarbon_StartOfWeek(b *testing.B) { now := Now() + for n := 0; n < b.N; n++ { + NewCarbon().StartOfWeek() + } for n := 0; n < b.N; n++ { now.StartOfWeek() } @@ -83,6 +116,9 @@ func BenchmarkCarbon_StartOfWeek(b *testing.B) { func BenchmarkCarbon_EndOfWeek(b *testing.B) { now := Now() + for n := 0; n < b.N; n++ { + NewCarbon().EndOfWeek() + } for n := 0; n < b.N; n++ { now.EndOfWeek() } @@ -90,6 +126,9 @@ func BenchmarkCarbon_EndOfWeek(b *testing.B) { func BenchmarkCarbon_StartOfDay(b *testing.B) { now := Now() + for n := 0; n < b.N; n++ { + NewCarbon().StartOfDay() + } for n := 0; n < b.N; n++ { now.StartOfDay() } @@ -97,6 +136,9 @@ func BenchmarkCarbon_StartOfDay(b *testing.B) { func BenchmarkCarbon_EndOfDay(b *testing.B) { now := Now() + for n := 0; n < b.N; n++ { + NewCarbon().EndOfDay() + } for n := 0; n < b.N; n++ { now.EndOfDay() } @@ -104,6 +146,9 @@ func BenchmarkCarbon_EndOfDay(b *testing.B) { func BenchmarkCarbon_StartOfHour(b *testing.B) { now := Now() + for n := 0; n < b.N; n++ { + NewCarbon().StartOfHour() + } for n := 0; n < b.N; n++ { now.StartOfHour() } @@ -111,6 +156,9 @@ func BenchmarkCarbon_StartOfHour(b *testing.B) { func BenchmarkCarbon_EndOfHour(b *testing.B) { now := Now() + for n := 0; n < b.N; n++ { + NewCarbon().EndOfHour() + } for n := 0; n < b.N; n++ { now.EndOfHour() } @@ -118,6 +166,9 @@ func BenchmarkCarbon_EndOfHour(b *testing.B) { func BenchmarkCarbon_StartOfMinute(b *testing.B) { now := Now() + for n := 0; n < b.N; n++ { + NewCarbon().StartOfMinute() + } for n := 0; n < b.N; n++ { now.StartOfMinute() } @@ -125,6 +176,9 @@ func BenchmarkCarbon_StartOfMinute(b *testing.B) { func BenchmarkCarbon_EndOfMinute(b *testing.B) { now := Now() + for n := 0; n < b.N; n++ { + NewCarbon().EndOfMinute() + } for n := 0; n < b.N; n++ { now.EndOfMinute() } @@ -132,6 +186,9 @@ func BenchmarkCarbon_EndOfMinute(b *testing.B) { func BenchmarkCarbon_StartOfSecond(b *testing.B) { now := Now() + for n := 0; n < b.N; n++ { + NewCarbon().StartOfSecond() + } for n := 0; n < b.N; n++ { now.StartOfSecond() } @@ -139,6 +196,9 @@ func BenchmarkCarbon_StartOfSecond(b *testing.B) { func BenchmarkCarbon_EndOfSecond(b *testing.B) { now := Now() + for n := 0; n < b.N; n++ { + NewCarbon().EndOfSecond() + } for n := 0; n < b.N; n++ { now.EndOfSecond() } From ac24e7df2b3aab0f3068c14bb4429eb5df467cc9 Mon Sep 17 00:00:00 2001 From: Peleus <245629560@qq.com> Date: Tue, 2 Jan 2024 22:55:15 +0800 Subject: [PATCH 12/14] Add optional `timezone` parameter in `CreateFromStdTime` method --- creator.go | 7 +++++-- creator_unit_test.go | 12 ++++++++---- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/creator.go b/creator.go index 27eab7f4..cc496a28 100755 --- a/creator.go +++ b/creator.go @@ -6,10 +6,13 @@ import ( // CreateFromStdTime creates a Carbon instance from standard time.Time. // 从标准的 time.Time 创建 Carbon 实例 -func CreateFromStdTime(tt time.Time) Carbon { +func CreateFromStdTime(tt time.Time, timezone ...string) Carbon { c := NewCarbon() - c.time = tt c.loc = tt.Location() + if len(timezone) > 0 { + c.loc, c.Error = getLocationByTimezone(timezone[0]) + } + c.time = tt return c } diff --git a/creator_unit_test.go b/creator_unit_test.go index c43860f0..fd83a24e 100755 --- a/creator_unit_test.go +++ b/creator_unit_test.go @@ -9,11 +9,15 @@ import ( ) func TestCarbon_CreateFromStdTime(t *testing.T) { - loc, _ := time.LoadLocation("Asia/Shanghai") + loc, _ := time.LoadLocation(PRC) tt := time.Now().In(loc) - expected := tt.Format(DateTimeLayout) - actual := CreateFromStdTime(tt).ToDateTimeString() - assert.Equal(t, expected, actual) + expected := tt.Location().String() + + actual1 := CreateFromStdTime(tt).Location() + assert.Equal(t, expected, actual1) + + actual2 := CreateFromStdTime(tt, PRC).Location() + assert.Equal(t, expected, actual2) } func TestCarbon_CreateFromTimestamp(t *testing.T) { From 46ae7dc6fb2e5cfccf9e6bc5b0f7f9c476677c70 Mon Sep 17 00:00:00 2001 From: Peleus <245629560@qq.com> Date: Tue, 2 Jan 2024 22:57:47 +0800 Subject: [PATCH 13/14] Set to local time by default in database function func (c *Carbon) Scan(v interface{}) #208 --- database.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/database.go b/database.go index 08562982..06d404a6 100644 --- a/database.go +++ b/database.go @@ -10,7 +10,12 @@ import ( func (c *Carbon) Scan(v interface{}) error { value, ok := v.(time.Time) if ok { + loc := time.Local + if c.loc != nil { + loc = c.loc + } *c = CreateFromStdTime(value) + c.loc = loc return nil } return fmt.Errorf("can not convert %v to carbon", v) From 652c5f14ec28a88a182327ecc3775930b7875f90 Mon Sep 17 00:00:00 2001 From: Peleus <245629560@qq.com> Date: Tue, 2 Jan 2024 22:58:14 +0800 Subject: [PATCH 14/14] Add optional `timezone` parameter in `CreateFromStdTime` method --- README.cn.md | 2 +- README.jp.md | 2 +- README.md | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.cn.md b/README.cn.md index 2eacbc94..86a29b9c 100644 --- a/README.cn.md +++ b/README.cn.md @@ -236,7 +236,7 @@ carbon.ParseByLayout("2020-08-05 13:14:15", "2006-01-02 15:04:05", carbon.Tokyo) ```go // 将 time.Time 转换成 Carbon -carbon.CreateFromStdTime(time.Now()) +carbon.CreateFromStdTime(time.Now(), "PRC") // 将 Carbon 转换成 time.Time carbon.Now().ToStdTime() ``` diff --git a/README.jp.md b/README.jp.md index 763e426a..9dea2fa6 100644 --- a/README.jp.md +++ b/README.jp.md @@ -236,7 +236,7 @@ carbon.ParseByLayout("2020-08-05 13:14:15", "2006-01-02 15:04:05", carbon.Tokyo) ```go // time.Time を Carbon に変換します -carbon.CreateFromStdTime(time.Now()) +carbon.CreateFromStdTime(time.Now(), "PRC") // Carbon を time.Time に変換します carbon.Now().ToStdTime() ``` diff --git a/README.md b/README.md index 628498b1..fd4ac547 100644 --- a/README.md +++ b/README.md @@ -234,7 +234,7 @@ carbon.ParseByLayout("今天是 2020年08月05日13时14分15秒", "今天是 20 ```go // Convert Time.time into Carbon -carbon.CreateFromStdTime(time.Now()) +carbon.CreateFromStdTime(time.Now(), "PRC") // Convert Carbon into Time.time carbon.Now().ToStdTime() ```