You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.4KB

  1. package goutil
  2. import (
  3. "testing"
  4. "time"
  5. )
  6. func TestFromUnixMilli(t *testing.T) {
  7. ms := 1542810446506
  8. ts := TimeFromUnixMilli(int64(ms))
  9. if ts.UTC().String() != "2018-11-21 14:27:26.506 +0000 UTC" {
  10. t.Errorf("Expected time to be '2018-11-21 14:27:26.506 +0000 UTC' got '%s'", ts.UTC().String())
  11. }
  12. }
  13. func TestFromUnixMicro(t *testing.T) {
  14. ms := 1542810446506000
  15. ts := TimeFromUnixMicro(int64(ms))
  16. if ts.UTC().String() != "2018-11-21 14:27:26.506 +0000 UTC" {
  17. t.Errorf("Expected time to be '2018-11-21 14:27:26.506 +0000 UTC' got '%s'", ts.UTC().String())
  18. }
  19. }
  20. func TestFromWebkit(t *testing.T) {
  21. var wk int64 = 13245202142853170
  22. ts := TimeFromWebKit(wk)
  23. expected := "2020-09-21 22:49:02.85317 +0000 UTC"
  24. if ts.UTC().String() != expected {
  25. t.Errorf("Expected time to be '%s' got '%s'", expected, ts.UTC().String())
  26. }
  27. }
  28. func TestNowUnixMicro(t *testing.T) {
  29. now := time.Now()
  30. nownano := now.UnixNano()
  31. nowmicro := nownano / 1000
  32. ts := TimeFromUnixMicro(nowmicro)
  33. if ts.UTC().String() != now.UTC().String() {
  34. t.Errorf("Expected '%s' got '%s'", now.UTC().String(), ts.UTC().String())
  35. }
  36. }
  37. func TestNowUnixMilli(t *testing.T) {
  38. now := time.Now()
  39. nownano := now.UnixNano()
  40. nowmilli := nownano / 1000000
  41. ts := TimeFromUnixMilli(nowmilli)
  42. if ts.UTC().Format(time.StampMilli) != now.UTC().Format(time.StampMilli) {
  43. t.Errorf("Expected '%s' got '%s'", now.UTC().String(), ts.UTC().String())
  44. }
  45. }