c_time.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package clean
  2. import (
  3. "data_ai/ul"
  4. "github.com/shopspring/decimal"
  5. qu "jygit.jydev.jianyu360.cn/data_processing/common_utils"
  6. "regexp"
  7. "strings"
  8. "time"
  9. )
  10. var numReg = regexp.MustCompile("[0-9.]+")
  11. var symbolReg = regexp.MustCompile("[%%﹪!!]")
  12. var YMD_Reg1 = regexp.MustCompile("(\\d{4}年\\d{1,2}月\\d{1,2}日)")
  13. var YMD_Reg2 = regexp.MustCompile("(\\d{4}[-]\\d{1,2}[-]\\d{1,2})")
  14. var HMS_Reg1 = regexp.MustCompile("(\\d{1,2}时\\d{1,2}分\\d{1,2}秒)")
  15. var HMS_Reg2 = regexp.MustCompile("(\\d{1,2}[:]\\d{1,2}[:]\\d{1,2})")
  16. func convertYMD(ymd string) string {
  17. if ymd1 := YMD_Reg1.FindString(ymd); ymd1 != "" {
  18. }
  19. return ""
  20. }
  21. func convertHMS(hms string) string {
  22. if hms1 := YMD_Reg1.FindString(hms); hms1 != "" {
  23. }
  24. return ""
  25. }
  26. // 清洗时间
  27. func CleanTime(st string) int64 {
  28. if st == "" || st == "无" {
  29. return 0
  30. }
  31. //YYYY-MM-DD HH:MM:SS
  32. /*
  33. 2024-02-28 09:00:00
  34. 2024年3月4日 08:00:00
  35. 2024年03月01日08时00分00秒
  36. 2024年3月8日 17:00:00
  37. 2024年03月08日09时30分00秒
  38. */
  39. st = strings.ReplaceAll(st, ":", ":")
  40. ymd, hms := convertYMD(st), convertHMS(st)
  41. st = ymd + " " + hms
  42. t, _ := time.ParseInLocation(ul.TimeLayout, st, time.Local)
  43. return t.Unix()
  44. }
  45. // 清洗折扣率
  46. func CleanDiscount(str string) float64 {
  47. str = fieldReg1.ReplaceAllString(str, "")
  48. if str == "" || str == "无" {
  49. return 0.0
  50. }
  51. if biddiscount := RateToFloat(str); biddiscount > 0.0 {
  52. baseCount := 1.0
  53. num1 := decimal.NewFromFloat(baseCount)
  54. num2 := decimal.NewFromFloat(biddiscount)
  55. if strings.Contains(str, "上浮") {
  56. decimalValue := num1.Add(num2)
  57. res, _ := decimalValue.Float64()
  58. return res
  59. } else if strings.Contains(str, "下浮") {
  60. decimalValue := num1.Sub(num2)
  61. res, _ := decimalValue.Float64()
  62. return res
  63. } else {
  64. return biddiscount
  65. }
  66. }
  67. return 0.0
  68. }
  69. // 转换系数
  70. func RateToFloat(str string) float64 {
  71. if num0 := qu.Float64All(numReg.FindString(str)); num0 > 0.0 {
  72. num1 := decimal.NewFromFloat(100.0)
  73. num2 := decimal.NewFromFloat(num0)
  74. if symbolReg.MatchString(str) {
  75. decimalValue := num2.Div(num1)
  76. res, _ := decimalValue.Float64()
  77. if res < 1.0 {
  78. return res
  79. } else {
  80. return 0.0
  81. }
  82. } else {
  83. if num0 < 1.0 {
  84. return num0
  85. } else if num0 == 1 {
  86. return 0.0
  87. } else {
  88. decimalValue := num2.Div(num1)
  89. res, _ := decimalValue.Float64()
  90. if res < 1.0 {
  91. return res
  92. } else {
  93. return 0.0
  94. }
  95. }
  96. }
  97. }
  98. return 0.0
  99. }