GAAS 广汽安道拓GFrame金属件MOM项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
3.5 KiB

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