dromara / carbon

A simple, semantic and developer-friendly golang package for time
https://pkg.go.dev/github.com/golang-module/carbon/v2
MIT License
4.77k stars 238 forks source link

json is reversed, causing the diff series method to be abnormal. What should I do in this case? #207

Closed liaohongxing closed 10 months ago

liaohongxing commented 10 months ago
package main

import (
    "encoding/json"
    "fmt"

    "github.com/golang-module/carbon/v2"
)

type Temp struct {
    Text      string        `json:"text"`
    Timestamp carbon.Carbon `json:"timestamp" tz:"PRC"`
}

func main() {
    str := `{"text": "123", "timestamp": "2023-12-27 11:29:40"}`
    tmp := new(Temp)
    if err := json.Unmarshal([]byte(str), tmp); err != nil {
        panic(err)
    }
    fmt.Println(tmp.Text, "==", tmp.Timestamp.Timezone(), "==", carbon.Now().Timezone())
}

golang version: 1.21

carbon version: 2.3.0

希望得到一样的时区:

123 == CST == CST

而我得到了UTC时区:

123 == UTC == CST
gouguoyin commented 10 months ago

v2.3.1 will support tz tag and will be released soon

gouguoyin commented 10 months ago

v2.3.1 has been released

liaohongxing commented 10 months ago

example code , test fail

PS F:\Software\time-test> go run .
panic: time: missing Location in call to Time.In

goroutine 1 [running]:
time.Time.In(...)
        C:/Program Files/Go/src/time/time.go:1146
github.com/golang-module/carbon/v2.Carbon.ToStdTime(...)
        C:/Users/DELL/go/pkg/mod/github.com/golang-module/carbon/v2@v2.3.1/outputer.go:822
github.com/golang-module/carbon/v2.Carbon.Timezone({{0x0, 0x0, 0x0}, 0x0, {0x0, 0x0}, 0x0, 0x0, 0x0, {0x0, ...}})
        C:/Users/DELL/go/pkg/mod/github.com/golang-module/carbon/v2@v2.3.1/getter.go:381 +0xee
main.main()
        F:/Software/time-test/main.go:22 +0xd8
exit status 2
gouguoyin commented 10 months ago

Usage error, must load the tag through the LoadTag method before json.Marshal or json.Unmarshal, please refer to README.md

image
liaohongxing commented 10 months ago

It's already working , thinks !