DateUtil.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package Utils
  2. import (
  3. "fmt"
  4. "math"
  5. "time"
  6. )
  7. // GO的诞辰
  8. const timeLayout = "2006-01-02 15:04:05"
  9. // 取当前系统时间
  10. func GetTimeNow() time.Time {
  11. return time.Now()
  12. }
  13. func GetTime(timeStr string) time.Time {
  14. toTime, _ := ToTime(timeStr)
  15. return toTime
  16. }
  17. func JavaLongTime(javaLong int64) time.Time {
  18. //1492566520958 -> 2017-04-19 09:48:40
  19. //fmt.Println(time.Unix(1492566520958/1000, 0))
  20. //fmt.Println(time.Unix(0, 1492566520958*1000000))
  21. return time.Unix(0, javaLong*1000000)
  22. }
  23. func ToTime(timeStr string) (time.Time, error) {
  24. loc, _ := time.LoadLocation("Local")
  25. toTime, err := time.ParseInLocation(timeLayout, timeStr, loc)
  26. //toTime, err := time.Parse(timeLayout, timeStr)
  27. return toTime, err
  28. }
  29. func ToTimeByFm(timeStr string, format string) (time.Time, error) {
  30. loc, _ := time.LoadLocation("Local")
  31. toTime, err := time.ParseInLocation(format, timeStr, loc)
  32. //toTime, err := time.Parse(timeLayout, timeStr)
  33. return toTime, err
  34. }
  35. //要想格式化为:yyyyMMddHHmmss
  36. //则 format = "20060102150405"
  37. //要想格式化为:yyyy-MM-dd HH:mm:ss
  38. //则 format = "2006-01-02 15:04:05"
  39. //要想格式化为:yyyy-MM-dd
  40. //则 format = "2006-01-02"
  41. func FormatTimeByFm(t time.Time, format string) string {
  42. return t.Format(format)
  43. }
  44. func GetCurrentTime() string {
  45. return FormatTime(time.Now())
  46. }
  47. func GetCurrentDay() string {
  48. return FormatTimeByFm(time.Now(), "2006-01-02")
  49. }
  50. func FormatTime(t time.Time) string {
  51. //
  52. return FormatTimeByFm(t, "2006-01-02 15:04:05")
  53. }
  54. func FormatTimeToNum(t time.Time) string {
  55. //
  56. return FormatTimeByFm(t, "20060102150405")
  57. }
  58. // 在当前时间之前
  59. func IsBeforeNow(t time.Time) (result bool) {
  60. result = false
  61. if &t != nil && t.Before(time.Now()) {
  62. result = true
  63. }
  64. return
  65. }
  66. // 在当前时间之后
  67. func IsAfterNow(t time.Time) (result bool) {
  68. result = false
  69. if &t != nil && t.After(time.Now()) {
  70. result = true
  71. }
  72. return
  73. }
  74. func SubDateTime(firstTime time.Time, secondTime time.Time) (result time.Duration) {
  75. result = time.Duration(0)
  76. if &firstTime != nil && &secondTime != nil {
  77. result = secondTime.Sub(firstTime)
  78. }
  79. return
  80. }
  81. func DifferDays(firstTime time.Time, secondTime time.Time) int64 {
  82. result := SubDateTime(firstTime, secondTime).Hours()
  83. return int64(math.Abs(result) / 24)
  84. }
  85. func DifferHour(firstTime time.Time, secondTime time.Time) int64 {
  86. result := SubDateTime(firstTime, secondTime).Hours()
  87. //return int64(result) 两个时间的先后顺序不一样,可能出现负数
  88. return int64(math.Abs(result))
  89. }
  90. func DifferMin(firstTime time.Time, secondTime time.Time) int64 {
  91. result := SubDateTime(firstTime, secondTime).Minutes()
  92. return int64(math.Abs(result))
  93. }
  94. func DifferSec(firstTime time.Time, secondTime time.Time) int64 {
  95. result := SubDateTime(firstTime, secondTime).Seconds()
  96. return int64(math.Abs(result))
  97. }
  98. // 24小时前的时间
  99. func Before24h() time.Time {
  100. t, _ := time.ParseDuration("-24h")
  101. return time.Now().Add(t)
  102. }
  103. func AddSecs(_time time.Time, secs int64) time.Time {
  104. t, _ := time.ParseDuration("1s")
  105. return time.Now().Add(t * time.Duration(secs))
  106. }
  107. /*
  108. 增加10分钟:utils.AddMins(time.Now(), 10)
  109. 减少5分钟:utils.AddMins(time.Now(), -5)
  110. */
  111. func AddMins(_time time.Time, mins int64) time.Time {
  112. t, _ := time.ParseDuration("1m")
  113. return time.Now().Add(t * time.Duration(mins))
  114. }
  115. func AddHours(_time time.Time, hours int64) time.Time {
  116. t, _ := time.ParseDuration("1h")
  117. return time.Now().Add(t * time.Duration(hours))
  118. }
  119. func AddDays(_time time.Time, days int) time.Time {
  120. return _time.AddDate(0, 0, days)
  121. }
  122. func AddMonths(_time time.Time, months int) time.Time {
  123. return _time.AddDate(0, months, 0)
  124. }
  125. func GetBeginTime(_time time.Time) time.Time {
  126. //2017-06-28 00:00:00 +0800 CST
  127. return GetBeginTimeByLoc(_time, time.Local)
  128. //return GetBeginTimeByLoc(_time, time.UTC)
  129. }
  130. func GetEndTime(_time time.Time) time.Time {
  131. //2017-06-28 23:59:59.999999999 +0800 CST
  132. return GetEndTimeByLoc(_time, time.Local)
  133. }
  134. func GetBeginTimeByLoc(_time time.Time, loc *time.Location) time.Time {
  135. year, month, day := _time.Date()
  136. return time.Date(year, month, day, 0, 0, 0, 0, loc)
  137. }
  138. func GetEndTimeByLoc(_time time.Time, loc *time.Location) time.Time {
  139. year, month, day := _time.Date()
  140. return time.Date(year, month, day, 23, 59, 59, 999999999, loc)
  141. }
  142. // 一行代码计算代码执行时间
  143. // defer utils.TimeCost(time.Now())
  144. func TimeCost(start time.Time) {
  145. terminal := time.Since(start)
  146. fmt.Println("TimeCost:", terminal)
  147. }
粤ICP备19079148号