timeutils_test.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package timeutils
  2. import (
  3. "testing"
  4. "time"
  5. )
  6. var testDateTimeStr = "2024-02-27 13:49:50"
  7. var testDateStr = "2024-02-27"
  8. func TestParseLocalTime(t *testing.T) {
  9. timeParsed := ParseLocalDateTime(testDateTimeStr)
  10. if testDateTimeStr != timeParsed.Format(time.DateTime) {
  11. t.Fatal("时间解析失败")
  12. }
  13. }
  14. func TestParseLocalDate(t *testing.T) {
  15. timeParsed := ParseLocalDateOnly(testDateStr)
  16. if testDateStr != timeParsed.Format(time.DateOnly) {
  17. t.Fatal("时间解析失败")
  18. }
  19. }
  20. func TestParseNowDateOnly(t *testing.T) {
  21. now := time.Now().Local()
  22. if now.Format(time.DateOnly) != ParseNowDateOnly().Format(time.DateOnly) {
  23. t.Fatal("时间解析错误")
  24. }
  25. }
  26. func TestFormatNowDateTime(t *testing.T) {
  27. now := time.Now().Local()
  28. if now.Format(time.DateTime) != FormatNowDateTime() {
  29. t.Fatal("时间解析错误")
  30. }
  31. }
  32. func TestFormatLocalDateTime(t *testing.T) {
  33. now := time.Now().Local()
  34. if now.Format(time.DateTime) != FormatLocalDateTime(&now) {
  35. t.Fatal("时间解析错误")
  36. }
  37. }
  38. func TestFormatNowDate(t *testing.T) {
  39. now := time.Now().Local()
  40. if now.Format(time.DateOnly) != FormatNowDateOnly() {
  41. t.Fatal("时间解析错误")
  42. }
  43. }
  44. func TestFormatLocalDate(t *testing.T) {
  45. now := time.Now().Local()
  46. if now.Format(time.DateOnly) != FormatLocalDateOnly(&now) {
  47. t.Fatal("时间解析错误")
  48. }
  49. }
  50. func TestGetYear(t *testing.T) {
  51. now := time.Now().Local()
  52. if now.Year() != GetYear(&now) {
  53. t.Fatal("时间解析错误")
  54. }
  55. }
  56. func TestGetWeek(t *testing.T) {
  57. now := time.Now().Local()
  58. _, week := now.ISOWeek()
  59. if week != GetWeek(&now) {
  60. t.Fatal("时间解析错误")
  61. }
  62. }
  63. func TestDiffNatureDays(t *testing.T) {
  64. now := time.Now().Local()
  65. afterOneDay := now.Add(24 * time.Hour)
  66. if DiffNatureDays(now.Format(time.DateTime), afterOneDay.Format(time.DateTime)) != 1 {
  67. t.Fatal("时间解析错误")
  68. }
  69. }
  70. func TestGetChinaMonth(t *testing.T) {
  71. timeParsed := ParseLocalDateTime(testDateTimeStr)
  72. if GetChinaMonth(timeParsed) != "二月" {
  73. t.Fatal("时间解析错误")
  74. }
  75. }
  76. func TestGetChinaWeek(t *testing.T) {
  77. timeParsed := ParseLocalDateTime(testDateTimeStr)
  78. if GetChinaWeek(timeParsed) != "星期二" {
  79. t.Fatal("时间解析错误")
  80. }
  81. }