time_parse.go 3.2 KB

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