time_parse.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package time_parse
  2. import (
  3. "github.com/jinzhu/now"
  4. "math"
  5. "net/http"
  6. "time"
  7. )
  8. var (
  9. cst *time.Location
  10. )
  11. // China Standard Time Layout
  12. const (
  13. // Deprecated: use time.DateTime golang 1.20
  14. CSTLayout = time.DateTime
  15. // Deprecated: use time.DateOnly golang 1.20
  16. CSTDateLayout = time.DateOnly
  17. )
  18. func init() {
  19. var err error
  20. if cst, err = time.LoadLocation("Asia/Shanghai"); err != nil {
  21. cst = time.Local
  22. }
  23. }
  24. func Now() *now.Now {
  25. timeFormats := append(append(now.TimeFormats, time.DateTime), time.DateOnly)
  26. c := &now.Config{
  27. WeekStartDay: time.Monday,
  28. TimeLocation: cst,
  29. TimeFormats: timeFormats,
  30. }
  31. return c.With(time.Now().In(cst))
  32. }
  33. // RFC3339ToCSTLayout convert rfc3339 value to China standard time layout
  34. // 2020-11-08T08:18:46+08:00 => 2020-11-08 08:18:46
  35. func RFC3339ToCSTLayout(value string) (string, error) {
  36. ts, err := time.Parse(time.RFC3339, value)
  37. if err != nil {
  38. return "", err
  39. }
  40. return ts.In(cst).Format(time.DateTime), nil
  41. }
  42. // GetMilliTimestamp 获取CST毫秒时间戳
  43. func GetMilliTimestamp() int64 {
  44. return time.Now().In(cst).UnixMilli()
  45. }
  46. // GetTimestamp 获取CST时间戳
  47. func GetTimestamp() int64 {
  48. return time.Now().In(cst).Unix()
  49. }
  50. // CSTLayoutString 格式化时间
  51. // 返回 "2006-01-02 15:04:05" 格式的时间
  52. func CSTLayoutString() string {
  53. return time.Now().In(cst).Format(time.DateTime)
  54. }
  55. // ParseCSTInLocation 格式化时间
  56. func ParseCSTInLocation(date string) (time.Time, error) {
  57. return time.ParseInLocation(time.DateTime, date, cst)
  58. }
  59. // CSTLayoutStringToUnix 返回 unix 时间戳
  60. // 2020-01-24 21:11:11 => 1579871471
  61. func CSTLayoutStringToUnix(cstLayoutString string) (int64, error) {
  62. stamp, err := time.ParseInLocation(time.DateTime, cstLayoutString, cst)
  63. if err != nil {
  64. return 0, err
  65. }
  66. return stamp.Unix(), nil
  67. }
  68. // GMTLayoutString 格式化时间
  69. // 返回 "Mon, 02 Jan 2006 15:04:05 GMT" 格式的时间
  70. func GMTLayoutString() string {
  71. return time.Now().In(cst).Format(http.TimeFormat)
  72. }
  73. // ParseGMTInLocation 格式化时间
  74. func ParseGMTInLocation(date string) (time.Time, error) {
  75. return time.ParseInLocation(http.TimeFormat, date, cst)
  76. }
  77. // SubInLocation 计算时间差
  78. func SubInLocation(ts time.Time) float64 {
  79. return math.Abs(time.Now().In(cst).Sub(ts).Seconds())
  80. }
  81. // GetFirstDateOfMonth 获取传入的时间所在月份的第一天,即某月第一天的0点。如传入time.Now(), 返回当前月份的第一天0点时间。
  82. func GetFirstDateOfMonth(d time.Time) time.Time {
  83. d = d.AddDate(0, 0, -d.Day()+1)
  84. return GetStartDayTime(d)
  85. }
  86. // GetLastDateOfMonth 获取传入的时间所在月份的最后一天,即某月最后一天的0点。如传入time.Now(), 返回当前月份的最后一天0点时间。
  87. func GetLastDateOfMonth(d time.Time) time.Time {
  88. return GetEndDayTime(GetFirstDateOfMonth(d).AddDate(0, 1, -1))
  89. }
  90. // GetStartDayTime 获取某一天的开始时间
  91. func GetStartDayTime(d time.Time) time.Time {
  92. return time.Date(d.Year(), d.Month(), d.Day(), 0, 0, 0, 0, cst)
  93. }
  94. // GetEndDayTime 获取某一天的结束时间
  95. func GetEndDayTime(d time.Time) time.Time {
  96. return GetStartDayTime(d).Add(24 * time.Hour).Add(-time.Second)
  97. }
  98. // GetStartYesterdayTime 获取昨天的起始时间
  99. func GetStartYesterdayTime() time.Time {
  100. d, _ := time.ParseDuration("-24h")
  101. return GetStartDayTime(time.Now().Add(d))
  102. }
  103. // SubInLocationDays 计算两个日期相差的天数
  104. func SubInLocationDays(t1, t2 time.Time) float64 {
  105. t1 = time.Date(t1.Year(), t1.Month(), t1.Day(), 0, 0, 0, 0, cst)
  106. t2 = time.Date(t2.Year(), t2.Month(), t2.Day(), 0, 0, 0, 0, cst)
  107. return math.Abs(t1.Sub(t2).Hours() / 24)
  108. }