u_time.go 2.3 KB

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