time_parse.go 3.0 KB

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