u_time.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package util
  2. import (
  3. "strconv"
  4. "strings"
  5. "time"
  6. )
  7. func GetNowTimeCustom() string {
  8. currentTime := time.Now()
  9. formattedTime := currentTime.Format("2006-01-02-15-04-05")
  10. return formattedTime
  11. }
  12. func BagTimeInInterval(bagTime string, begin string, end string) bool {
  13. compare1 := TimeCustom1GreaterEqualThanTimeCustom2(bagTime, begin)
  14. compare2 := TimeCustom1LessEqualThanTimeCustom2(bagTime, end)
  15. return compare1 && compare2
  16. }
  17. func TimeCustom1GreaterEqualThanTimeCustom2(timeCustom1 string, timeCustom2 string) bool {
  18. timeInt1, _ := strconv.Atoi(strings.Replace(timeCustom1, "-", "", -1))
  19. timeInt2, _ := strconv.Atoi(strings.Replace(timeCustom2, "-", "", -1))
  20. return timeInt1 >= timeInt2
  21. }
  22. func GetBagTime(bagName string) string {
  23. s1 := strings.Split(bagName, "_")[0]
  24. s1Split := strings.Split(s1, "/")
  25. s2 := s1Split[len(s1Split)-1]
  26. return s2
  27. }
  28. func TimeCustomChange(originalTimeStr string, number int) string {
  29. var newTimeStr string
  30. layout := "2006-01-02-15-04-05"
  31. originalTime, err := time.Parse(layout, originalTimeStr)
  32. if err != nil {
  33. return newTimeStr
  34. }
  35. newTime := originalTime.Add(time.Duration(number) * time.Second)
  36. return newTime.Format(layout)
  37. }
  38. func CalculateDifferenceOfTimeCustom(timeCustom1 string, timeCustom2 string) int {
  39. timeInt1, _ := strconv.Atoi(strings.Replace(timeCustom1, "-", "", -1))
  40. timeInt2, _ := strconv.Atoi(strings.Replace(timeCustom2, "-", "", -1))
  41. return timeInt2 - timeInt1 + 1
  42. }
  43. func TimeCustom1GreaterTimeCustom2(timeCustom1 string, timeCustom2 string) bool {
  44. timeInt1, _ := strconv.Atoi(strings.Replace(timeCustom1, "-", "", -1))
  45. timeInt2, _ := strconv.Atoi(strings.Replace(timeCustom2, "-", "", -1))
  46. return timeInt1 > timeInt2
  47. }
  48. func TimeCustom1LessEqualThanTimeCustom2(timeCustom1 string, timeCustom2 string) bool {
  49. timeInt1, _ := strconv.Atoi(strings.Replace(timeCustom1, "-", "", -1))
  50. timeInt2, _ := strconv.Atoi(strings.Replace(timeCustom2, "-", "", -1))
  51. return timeInt1 <= timeInt2
  52. }
  53. func GetTimeString(sourceTime time.Time) string {
  54. var defaultTime = time.Date(2006, time.January, 2, 15, 4, 5, 0, time.Local)
  55. if sourceTime.Equal(defaultTime) {
  56. return ""
  57. }
  58. return sourceTime.Format("2006-01-02 15:04:05")
  59. }