time.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package utils
  2. import (
  3. "fmt"
  4. "strconv"
  5. "strings"
  6. "time"
  7. )
  8. // 获取某一天的0点时间
  9. func GetZeroTime(d time.Time) time.Time {
  10. return time.Date(d.Year(), d.Month(), d.Day(), 0, 0, 0, 0, d.Location())
  11. }
  12. func GetFirstDateOfMonth(d time.Time) time.Time {
  13. d = d.AddDate(0, 0, -d.Day()+1)
  14. return GetZeroTime(d)
  15. }
  16. // Format unix time int64 to string
  17. func Date(ti int64, format string) string {
  18. t := time.Unix(int64(ti), 0)
  19. if ti == 0 {
  20. return ""
  21. }
  22. return DateT(t, format)
  23. }
  24. // Format unix time string to string
  25. func DateS(ts string, format string) string {
  26. i, _ := strconv.ParseInt(ts, 10, 64)
  27. if i == 0 {
  28. return ""
  29. }
  30. return Date(i, format)
  31. }
  32. // Format time.Time struct to string
  33. // MM - month - 01
  34. // M - month - 1, single bit
  35. // DD - day - 02
  36. // D - day 2
  37. // YYYY - year - 2006
  38. // YY - year - 06
  39. // HH - 24 hours - 03
  40. // H - 24 hours - 3
  41. // hh - 12 hours - 03
  42. // h - 12 hours - 3
  43. // mm - minute - 04
  44. // m - minute - 4
  45. // ss - second - 05
  46. // s - second = 5
  47. func DateT(t time.Time, format string) string {
  48. res := strings.Replace(format, "MM", t.Format("01"), -1)
  49. res = strings.Replace(res, "M", t.Format("1"), -1)
  50. res = strings.Replace(res, "DD", t.Format("02"), -1)
  51. res = strings.Replace(res, "D", t.Format("2"), -1)
  52. res = strings.Replace(res, "YYYY", t.Format("2006"), -1)
  53. res = strings.Replace(res, "YY", t.Format("06"), -1)
  54. res = strings.Replace(res, "HH", fmt.Sprintf("%02d", t.Hour()), -1)
  55. res = strings.Replace(res, "H", fmt.Sprintf("%d", t.Hour()), -1)
  56. res = strings.Replace(res, "hh", t.Format("03"), -1)
  57. res = strings.Replace(res, "h", t.Format("3"), -1)
  58. res = strings.Replace(res, "mm", t.Format("04"), -1)
  59. res = strings.Replace(res, "m", t.Format("4"), -1)
  60. res = strings.Replace(res, "ss", t.Format("05"), -1)
  61. res = strings.Replace(res, "s", t.Format("5"), -1)
  62. return res
  63. }
  64. // pattern rules.
  65. var datePatterns = []string{
  66. // year
  67. "Y", "2006", // A full numeric representation of a year, 4 digits Examples: 1999 or 2003
  68. "y", "06", //A two digit representation of a year Examples: 99 or 03
  69. // month
  70. "m", "01", // Numeric representation of a month, with leading zeros 01 through 12
  71. "n", "1", // Numeric representation of a month, without leading zeros 1 through 12
  72. "M", "Jan", // A short textual representation of a month, three letters Jan through Dec
  73. "F", "January", // A full textual representation of a month, such as January or March January through December
  74. // day
  75. "d", "02", // Day of the month, 2 digits with leading zeros 01 to 31
  76. "j", "2", // Day of the month without leading zeros 1 to 31
  77. // week
  78. "D", "Mon", // A textual representation of a day, three letters Mon through Sun
  79. "l", "Monday", // A full textual representation of the day of the week Sunday through Saturday
  80. // time
  81. "g", "3", // 12-hour format of an hour without leading zeros 1 through 12
  82. "G", "15", // 24-hour format of an hour without leading zeros 0 through 23
  83. "h", "03", // 12-hour format of an hour with leading zeros 01 through 12
  84. "H", "15", // 24-hour format of an hour with leading zeros 00 through 23
  85. "a", "pm", // Lowercase Ante meridiem and Post meridiem am or pm
  86. "A", "PM", // Uppercase Ante meridiem and Post meridiem AM or PM
  87. "i", "04", // Minutes with leading zeros 00 to 59
  88. "s", "05", // Seconds, with leading zeros 00 through 59
  89. // time zone
  90. "T", "MST",
  91. "P", "-07:00",
  92. "O", "-0700",
  93. // RFC 2822
  94. "r", time.RFC1123Z,
  95. }
  96. // Parse Date use PHP time format.
  97. func DateParse(dateString, format string) (time.Time, error) {
  98. replacer := strings.NewReplacer(datePatterns...)
  99. format = replacer.Replace(format)
  100. return time.ParseInLocation(format, dateString, time.Local)
  101. }
  102. func DateParseUnix(dateString, format string) int {
  103. if dateString == "" {
  104. return 0
  105. }
  106. date, err := DateParse(dateString, format)
  107. if err != nil {
  108. fmt.Println(err)
  109. return 0
  110. }
  111. return ToInt(date.Unix())
  112. }