timeutil_test.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * Public Domain Software
  3. *
  4. * I (Matthias Ladkau) am the author of the source code in this file.
  5. * I have placed the source code in this file in the public domain.
  6. *
  7. * For further information see: http://creativecommons.org/publicdomain/zero/1.0/
  8. */
  9. package timeutil
  10. import (
  11. "strconv"
  12. "strings"
  13. "testing"
  14. "time"
  15. )
  16. func TestTimestamp(t *testing.T) {
  17. ts := MakeTimestamp()
  18. millis, err := strconv.ParseInt(ts, 10, 64)
  19. if err != nil {
  20. return
  21. }
  22. tsTime := time.Unix(0, millis*1000000)
  23. tsTime = tsTime.UTC()
  24. tss, err := TimestampString(ts, "UTC")
  25. if err != nil {
  26. t.Error("Unexpected error during timestamp printing:", err)
  27. return
  28. } else if tsTime.String() != tss {
  29. t.Error("Unexpected timestamp printing result:", tss)
  30. return
  31. }
  32. _, err = TimestampString("abc", "UTC")
  33. if err.Error() != "strconv.ParseInt: parsing \"abc\": invalid syntax" {
  34. t.Error("Unexpected error during timestamp printing:", err)
  35. return
  36. }
  37. _, err = TimestampString(ts, "U_B_C")
  38. if !strings.HasPrefix(err.Error(), "unknown time zone U_B_C") {
  39. t.Error("Unexpected error during timestamp printing:", err)
  40. return
  41. }
  42. // Test compare
  43. if res, err := CompareTimestamp("1475602478271", "1475615168232"); res != 1 || err != nil {
  44. t.Error("Unexpected compare result:", res, err)
  45. return
  46. }
  47. if res, err := CompareTimestamp("1475602478271", "1375615168232"); res != -1 || err != nil {
  48. t.Error("Unexpected compare result:", res, err)
  49. return
  50. }
  51. if _, err := CompareTimestamp("1475602478271", ""); err == nil {
  52. t.Error("Unexpected compare result:", err)
  53. return
  54. }
  55. if _, err := CompareTimestamp("", "1"); err == nil {
  56. t.Error("Unexpected compare result:", err)
  57. return
  58. }
  59. if res, err := CompareTimestamp("1", "1"); res != 0 || err != nil {
  60. t.Error("Unexpected compare result:", res, err)
  61. return
  62. }
  63. }