@@ -1205,85 +1205,11 @@ carbon.Parse("2020-08-05 13:14:15").IsWinter() // false
12051205
12061206##### JSON
12071207
1208- ###### Scene one: all time fields have the same format
1209- ``` go
1210- carbon.SetDefault (carbon.Default {
1211- Layout : carbon.DateTimeLayout ,
1212- })
1213-
1214- type Person struct {
1215- Name string ` json:"name"`
1216- Age int ` json:"age"`
1217-
1218- Field1 carbon.Carbon ` json:"field1"`
1219- Field2 carbon.Carbon ` json:"field2"`
1220- Field3 carbon.Carbon ` json:"field3"`
1221- Field4 carbon.Carbon ` json:"field4"`
1222-
1223- Field5 carbon.Carbon ` json:"field5"`
1224- Field6 carbon.Carbon ` json:"field6"`
1225- Field7 carbon.Carbon ` json:"field7"`
1226- Field8 carbon.Carbon ` json:"field8"`
1227- }
1228-
1229- now := carbon.Parse (" 2020-08-05 13:14:15" , carbon.PRC )
1230- person := Person {
1231- Name : " gouguoyin" ,
1232- Age : 18 ,
1233-
1234- Field1 : now,
1235- Field2 : now,
1236- Field3 : now,
1237- Field4 : now,
1238- Field5 : now,
1239- Field6 : now,
1240- Field7 : now,
1241- Field8 : now,
1242- }
1243-
1244- data , marshalErr := json.Marshal (person)
1245- if marshalErr != nil {
1246- // Error handle...
1247- log.Fatal (marshalErr)
1248- }
1249- fmt.Printf (" %s " , data)
1250- // Output
1251- {
1252- " name" : " gouguoyin" ,
1253- " age" : 18 ,
1254- " field1" : " 2020-08-05 13:14:15" ,
1255- " field2" : " 2020-08-05 13:14:15" ,
1256- " field3" : " 2020-08-05 13:14:15" ,
1257- " field4" : " 2020-08-05 13:14:15" ,
1258- " field5" : " 2020-08-05 13:14:15" ,
1259- " field6" : " 2020-08-05 13:14:15" ,
1260- " field7" : " 2020-08-05 13:14:15" ,
1261- " field8" : " 2020-08-05 13:14:15"
1262- }
1263-
1264- var person Person
1265- unmarshalErr := json.Unmarshal (data, &person)
1266- if unmarshalErr != nil {
1267- // Error handle...
1268- log.Fatal (unmarshalErr)
1269- }
1270-
1271- fmt.Printf (" %s " , person.Field1 ) // 2020-08-05 13:14:15
1272- fmt.Printf (" %s " , person.Field2 ) // 2020-08-05 13:14:15
1273- fmt.Printf (" %s " , person.Field3 ) // 2020-08-05 13:14:15
1274- fmt.Printf (" %s " , person.Field4 ) // 2020-08-05 13:14:15
1275-
1276- fmt.Printf (" %s " , person.Field5 ) // 2020-08-05 13:14:15
1277- fmt.Printf (" %s " , person.Field6 ) // 2020-08-05 13:14:15
1278- fmt.Printf (" %s " , person.Field7 ) // 2020-08-05 13:14:15
1279- fmt.Printf (" %s " , person.Field8 ) // 2020-08-05 13:14:15
1280- ```
1281-
1282- ###### Scene two: different time fields have different formats
12831208``` go
12841209type Person struct {
12851210 Name string ` json:"name"`
12861211 Age int ` json:"age"`
1212+ Birthday0 carbon.Carbon ` json:"birthday0"`
12871213 Birthday1 carbon.DateTime ` json:"birthday1"`
12881214 Birthday2 carbon.DateTimeMilli ` json:"birthday2"`
12891215 Birthday3 carbon.DateTimeMicro ` json:"birthday3"`
@@ -1305,6 +1231,7 @@ type Person struct {
13051231person := Person {
13061232 Name : " gouguoyin" ,
13071233 Age : 18 ,
1234+ Birthday0 : carbon.Now ().SubYears (18 ),
13081235 Birthday1 : carbon.Now ().SubYears (18 ).ToDateTimeStruct (),
13091236 Birthday2 : carbon.Now ().SubYears (18 ).ToDateTimeMilliStruct (),
13101237 Birthday3 : carbon.Now ().SubYears (18 ).ToDateTimeMicroStruct (),
@@ -1333,6 +1260,7 @@ fmt.Printf("%s", data)
13331260{
13341261 " name" : " gouguoyin" ,
13351262 " age" : 18 ,
1263+ " birthday0" : " 2003-07-16 13:14:15" ,
13361264 " birthday1" : " 2003-07-16 13:14:15" ,
13371265 " birthday2" : " 2003-07-16 13:14:15.999" ,
13381266 " birthday3" : " 2003-07-16 13:14:15.999999" ,
@@ -1357,6 +1285,7 @@ if err != nil {
13571285 log.Fatal (err)
13581286}
13591287
1288+ person.Birthday0 .String () // 2003-07-16 13:14:15
13601289person.Birthday1 .String () // 2003-07-16 13:14:15
13611290person.Birthday2 .String () // 2003-07-16 13:14:15.999
13621291person.Birthday3 .String () // 2003-07-16 13:14:15.999999
0 commit comments