periodw_test.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package test
  2. import (
  3. "errors"
  4. "fmt"
  5. "testing"
  6. "time"
  7. )
  8. /*周期记录时间测试
  9. 1、2、5、6、10、10、15、20、30、60
  10. */
  11. func TestPeriod(t *testing.T) {
  12. //// 更新记录 - 缓存
  13. // 输入时间列表
  14. timeStrings := []string{
  15. "2017-04-11 13:00:00",
  16. "2017-04-11 13:01:01",
  17. "2017-04-11 13:02:12",
  18. "2017-04-11 13:03:12",
  19. "2017-04-11 13:04:12",
  20. "2017-04-11 13:05:12",
  21. "2017-04-11 13:06:12",
  22. "2017-04-11 13:07:12",
  23. "2017-04-11 13:08:12",
  24. "2017-04-11 13:09:12",
  25. "2017-04-11 13:10:12",
  26. "2017-04-11 13:20:32",
  27. "2017-02-12 13:22:12",
  28. "2017-03-13 13:24:45",
  29. "2024-04-14 13:26:10",
  30. "2024-04-15 13:29:59",
  31. "2023-04-11 13:30:10",
  32. "2023-05-11 13:33:23",
  33. "2023-05-11 13:39:38",
  34. "2023-05-11 13:41:23",
  35. "2023-05-11 13:43:34",
  36. "2023-05-11 13:54:30",
  37. "2023-05-11 13:50:00",
  38. "2023-05-11 13:51:00",
  39. "2023-05-11 13:52:00",
  40. "2023-05-11 13:53:00",
  41. "2023-05-11 13:54:00",
  42. "2023-05-11 13:55:12",
  43. "2023-05-11 13:56:19",
  44. "2023-05-11 13:56:29",
  45. "2023-05-11 13:58:49",
  46. "2023-05-11 13:59:59",
  47. "2023-05-11 14:00:05",
  48. "2023-05-11 14:01:05",
  49. "2023-05-11 14:01:15",
  50. "2023-05-11 14:02:05",
  51. "2023-05-11 14:03:05",
  52. "2023-05-11 14:04:05",
  53. "2023-05-11 14:05:05",
  54. "2023-05-11 14:06:05",
  55. "2023-05-11 14:07:05",
  56. "2023-05-11 14:08:05",
  57. "2023-05-11 14:09:05",
  58. "2023-05-11 14:10:05",
  59. "2024-09-30 00:30:00",
  60. }
  61. for _, ts := range timeStrings {
  62. time_r, err := PeriodTime(ts, 30)
  63. if err != nil {
  64. fmt.Println(err)
  65. }
  66. fmt.Println(time_r.Format("2006-01-02 15:04:05"))
  67. }
  68. }
  69. func PeriodTime(time_s string, T_saveT int) (time.Time, error) {
  70. period := time.Minute * time.Duration(T_saveT)
  71. // 解析时间字符串
  72. t, err := time.Parse("2006-01-02 15:04:05", time_s)
  73. if err != nil {
  74. fmt.Println("时间解析错误:", err)
  75. return time.Now(), errors.New("时间解析错误!")
  76. }
  77. // 计算归一化的分钟
  78. minutes := t.Minute()
  79. normalizedMinutes := (minutes / int(period.Minutes())) * int(period.Minutes())
  80. // 创建新的时间对象,使用归一化后的分钟
  81. normalizedTime := time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), normalizedMinutes, 0, 0, t.Location())
  82. // 输出结果
  83. //fmt.Println(normalizedTime.Format("2006-01-02 15:04:05"))
  84. return normalizedTime, nil
  85. }