millisecs_test.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package encodedTime
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "testing"
  7. "time"
  8. )
  9. func TestMillisecsUnmarshall(t *testing.T) {
  10. v := struct {
  11. Timestamp Millisecs
  12. }{}
  13. err := json.Unmarshal([]byte(`{"Timestamp":1449808143436}`), &v)
  14. if err != nil {
  15. t.Fatal(err)
  16. }
  17. if n := time.Time(v.Timestamp).Sub(time.Unix(1449808143436/1000, 0)); n != 0 {
  18. t.Fatal(fmt.Errorf("times not equal:%d", n))
  19. }
  20. }
  21. // SSBQuirk
  22. func TestFloats(t *testing.T) {
  23. v := struct {
  24. Timestamp Millisecs
  25. }{}
  26. err := json.Unmarshal([]byte(`{"Timestamp":1553708494043.0059}`), &v)
  27. if err != nil {
  28. t.Fatal(err)
  29. }
  30. if n := time.Time(v.Timestamp).Sub(time.Unix(15537084940430059/1000, 0)); n != 0 {
  31. t.Fatal(fmt.Errorf("times not equal:%d", n))
  32. }
  33. }
  34. func TestMillisecsMarshal(t *testing.T) {
  35. v := struct {
  36. Date Millisecs
  37. }{Millisecs(time.Unix(12345, 0))}
  38. out, err := json.Marshal(v)
  39. if err != nil {
  40. t.Fatal(err)
  41. }
  42. if bytes.Compare(out, []byte(`{"Date":12345000}`)) != 0 {
  43. t.Fatal(fmt.Errorf("times not equal - got %q", out))
  44. }
  45. }