u_time.go 2.5 KB

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