timeutil_test.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. ts = MakeTimestamp()
  44. if res, err := CompareTimestamp("1475602478271", "1475615168232"); res != 1 || err != nil {
  45. t.Error("Unexpected compare result:", res, err)
  46. return
  47. }
  48. if res, err := CompareTimestamp("1475602478271", "1375615168232"); res != -1 || err != nil {
  49. t.Error("Unexpected compare result:", res, err)
  50. return
  51. }
  52. if _, err := CompareTimestamp("1475602478271", ""); err == nil {
  53. t.Error("Unexpected compare result:", err)
  54. return
  55. }
  56. if _, err := CompareTimestamp("", "1"); err == nil {
  57. t.Error("Unexpected compare result:", err)
  58. return
  59. }
  60. if res, err := CompareTimestamp("1", "1"); res != 0 || err != nil {
  61. t.Error("Unexpected compare result:", res, err)
  62. return
  63. }
  64. }