date.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package date
  2. import (
  3. "strconv"
  4. "strings"
  5. "time"
  6. )
  7. type Date struct {
  8. }
  9. const (
  10. year = "2006"
  11. month = "01"
  12. day = "02"
  13. hour = "15"
  14. minute = "04"
  15. second = "05"
  16. complete = "2006-01-02 15:05:05"
  17. stringToTimeOne = "2006-01-02 15:04:05"
  18. stringToTimeTow = "2006-01-02"
  19. )
  20. // FormatToString 时间格式化成字符串 Time forms a string
  21. func (d *Date) FormatToString(t time.Time, s string) string {
  22. replace, b := d.replace(s)
  23. if !b {
  24. return t.Format(complete)
  25. }
  26. if d.IsZero(t) {
  27. return d.Now().Format(complete)
  28. }
  29. return t.Format(replace)
  30. }
  31. //字符串替换
  32. func (d *Date) replace(s string) (string, bool) {
  33. flag := false
  34. if strings.Contains(s, "YYYY") {
  35. s = strings.Replace(s, "YYYY", year, 1)
  36. flag = true
  37. }
  38. if strings.Contains(s, "MM") {
  39. s = strings.Replace(s, "MM", month, 1)
  40. flag = true
  41. }
  42. if strings.Contains(s, "DD") {
  43. s = strings.Replace(s, "DD", day, 1)
  44. flag = true
  45. }
  46. if strings.Contains(s, "hh") {
  47. s = strings.Replace(s, "hh", hour, 1)
  48. flag = true
  49. }
  50. if strings.Contains(s, "mm") {
  51. s = strings.Replace(s, "mm", minute, 1)
  52. flag = true
  53. }
  54. if strings.Contains(s, "ss") {
  55. s = strings.Replace(s, "ss", second, 1)
  56. flag = true
  57. }
  58. return s, flag
  59. }
  60. // IsZero 判断时间是否为零 Determine whether the time is zero
  61. func (d *Date) IsZero(t time.Time) bool {
  62. return t.IsZero()
  63. }
  64. // Now 获取当前时间 Get the current time
  65. func (d *Date) Now() time.Time {
  66. return time.Now()
  67. }
  68. // InterpretStringToTimestamp String to timestamp 字符串转时间戳
  69. func (d *Date) InterpretStringToTimestamp(time_str string, strFormat string) (int64, error) {
  70. var t int64
  71. loc, _ := time.LoadLocation("Local")
  72. replace, _ := d.replace(strFormat)
  73. t1, err := time.ParseInLocation(replace, time_str, loc)
  74. if err != nil {
  75. return 0, err
  76. }
  77. t = t1.Unix()
  78. return t, err
  79. }
  80. // UnixToTime 时间戳转时间
  81. // UnixToTime timestamp to time
  82. func (d *Date) UnixToTime(unix int64) time.Time {
  83. return time.Unix(unix, 0)
  84. }
  85. // GetWeekDay 获取周几方法
  86. //How to get the day of the week
  87. func (d *Date) GetWeekDay(t time.Time) int {
  88. return int(t.Weekday())
  89. }
  90. // MinuteAddOrSub 时间分钟加减计算
  91. func (d *Date) MinuteAddOrSub(t time.Time, num int64) time.Time {
  92. s := strconv.FormatInt(num, 10)
  93. var m time.Duration
  94. m, _ = time.ParseDuration(s + "m")
  95. return t.Add(m)
  96. }
  97. // HourAddOrSub 时间小时加减计算
  98. func (d *Date) HourAddOrSub(t time.Time, num int64) time.Time {
  99. s := strconv.FormatInt(num, 10)
  100. var m time.Duration
  101. m, _ = time.ParseDuration(s + "h")
  102. return t.Add(m)
  103. }
  104. // DayAddOrSub 时间天加减计算
  105. func (d *Date) DayAddOrSub(t time.Time, num int64) time.Time {
  106. num = num * 24
  107. s := strconv.FormatInt(num, 10)
  108. var m time.Duration
  109. m, _ = time.ParseDuration(s + "h")
  110. return t.Add(m)
  111. }