time_test.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // Copyright 2025 BackendServerTemplate Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package strconvutils
  5. import (
  6. "testing"
  7. "time"
  8. )
  9. func TestReadTimeDuration(t *testing.T) {
  10. if res := ReadTimeDuration("10Min"); res != 10*time.Minute {
  11. t.Errorf("ReadTimeDuration(10Min) error")
  12. }
  13. if res := ReadTimeDuration("-10S"); res != -10*time.Second {
  14. t.Errorf("ReadTimeDuration(-10S) error")
  15. }
  16. if res := ReadTimeDurationPositive("-10S"); res != 0 {
  17. t.Errorf("ReadTimeDurationPositive(-10S) error")
  18. }
  19. }
  20. func TestTimeDurationToString(t *testing.T) {
  21. if res := TimeDurationToString(5 * 24 * time.Hour); res != "5d" {
  22. t.Errorf("TimeDurationToString(5*24*Hour) -> 5d: %s", res)
  23. }
  24. if res := TimeDurationToString(3 * time.Hour); res != "3h" {
  25. t.Errorf("TimeDurationToString(3*Hour) -> 3h: %s", res)
  26. }
  27. if res := TimeDurationToStringCN(5 * 24 * time.Hour); res != "5天" {
  28. t.Errorf("TimeDurationToString(5*24*Hour) -> 5天: %s", res)
  29. }
  30. if res := TimeDurationToStringCN(3 * time.Hour); res != "3小时" {
  31. t.Errorf("TimeDurationToString(3*Hour) -> 3小时: %s", res)
  32. }
  33. }