diff --git a/README.cn.md b/README.cn.md index c8cf92a7..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() ``` @@ -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..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() ``` @@ -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..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() ``` @@ -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 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() } diff --git a/carbon.go b/carbon.go index 8fde818f..0d3201bf 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,9 +245,9 @@ const ( // 定义 Carbon 结构体 type Carbon struct { time time.Time - testNow int64 // timestamp with nanosecond of test now time - tag string + testNow int64 // nanosecond timestamp of test now time weekStartsAt time.Weekday + tag tag loc *time.Location lang *Language Error error 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) { 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) 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 } 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) 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. 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 } 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) }