-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_test.go
65 lines (62 loc) · 1.97 KB
/
json_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package xtime
import (
json "encoding/json"
"github.com/stretchr/testify/assert"
"testing"
"time"
)
func TestChinaRange(t *testing.T) {
{
type Request struct {
Range ChinaRange `json:"range"`
}
req := Request{}
err := json.Unmarshal([]byte(`{"range":{"start":"2024-06-07 10:34:40","end":"2024-06-10 11:22:33"}}`), &req)
assert.NoError(t, err)
assert.Equal(t, req.Range.Start.In(time.FixedZone("CST", 8*3600)).String(), `2024-06-07 10:34:40 +0800 CST`)
assert.Equal(t, req.Range.End.In(time.FixedZone("CST", 8*3600)).String(), `2024-06-10 11:22:33 +0800 CST`)
}
}
func TestChinaTime(t *testing.T) {
String := func(v interface{}) string {
data, err := json.Marshal(v)
if err != nil {
panic(err)
}
return string(data)
}
{
tValue, err := time.ParseInLocation("2006-01-02 15:04:05", "2020-07-31 15:29:29", time.FixedZone("CST", 2*3600))
assert.Equal(t, err, nil)
assert.Equal(t, String(NewChinaTime(tValue)), `"2020-07-31 21:29:29"`)
}
{
tValue, err := time.ParseInLocation("2006-01-02 15:04:05", "2020-07-31 15:29:29", time.FixedZone("CST", 8*3600))
assert.Equal(t, err, nil)
assert.Equal(t, String(NewChinaTime(tValue)), `"2020-07-31 15:29:29"`)
}
{
tValue, err := time.ParseInLocation("2006-01-02 15:04:05", "2020-07-31 15:29:29", time.FixedZone("CST", 0*3600))
assert.Equal(t, err, nil)
assert.Equal(t, String(NewChinaTime(tValue)), `"2020-07-31 23:29:29"`)
}
{
type Request struct {
Time ChinaTime `db:"time"`
}
req := Request{}
err := json.Unmarshal([]byte(`{"time":"2020-07-31 15:37:44"}`), &req)
assert.NoError(t, err)
assert.Equal(t, req.Time.In(time.FixedZone("CST", 8*3600)).String(), "2020-07-31 15:37:44 +0800 CST")
}
{
type Reply struct {
Time ChinaTime `db:"time"`
}
reply := Reply{}
tValue, err := time.ParseInLocation("2006-01-02 15:04:05", "2020-07-31 15:29:29", time.UTC)
assert.Equal(t, err, nil)
reply.Time = NewChinaTime(tValue)
assert.Equal(t, reply.Time.String(), "2020-07-31 23:29:29 +0800 CST")
}
}