SJA APS后端代码
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.

274 lines
7.2 KiB

  1. package common
  2. import (
  3. "fmt"
  4. "log"
  5. "strconv"
  6. "strings"
  7. "time"
  8. "errors"
  9. )
  10. // DateFormat pattern rules.
  11. var datePatterns = []string{
  12. // year
  13. "Y", "2006", // A full numeric representation of a year, 4 digits Examples: 1999 or 2003
  14. "y", "06", //A two digit representation of a year Examples: 99 or 03
  15. // month
  16. "m", "01", // Numeric representation of a month, with leading zeros 01 through 12
  17. "n", "1", // Numeric representation of a month, without leading zeros 1 through 12
  18. "M", "Jan", // A short textual representation of a month, three letters Jan through Dec
  19. "F", "January", // A full textual representation of a month, such as January or March January through December
  20. // day
  21. "d", "02", // Day of the month, 2 digits with leading zeros 01 to 31
  22. "j", "2", // Day of the month without leading zeros 1 to 31
  23. // week
  24. "D", "Mon", // A textual representation of a day, three letters Mon through Sun
  25. "l", "Monday", // A full textual representation of the day of the week Sunday through Saturday
  26. // time
  27. "g", "3", // 12-hour format of an hour without leading zeros 1 through 12
  28. "G", "15", // 24-hour format of an hour without leading zeros 0 through 23
  29. "h", "03", // 12-hour format of an hour with leading zeros 01 through 12
  30. "H", "15", // 24-hour format of an hour with leading zeros 00 through 23
  31. "a", "pm", // Lowercase Ante meridiem and Post meridiem am or pm
  32. "A", "PM", // Uppercase Ante meridiem and Post meridiem AM or PM
  33. "i", "04", // Minutes with leading zeros 00 to 59
  34. "s", "05", // Seconds, with leading zeros 00 through 59
  35. // time zone
  36. "T", "MST",
  37. "P", "-07:00",
  38. "O", "-0700",
  39. // RFC 2822
  40. "r", time.RFC1123Z,
  41. }
  42. // Format time.Time struct to string
  43. // MM - month - 01
  44. // M - month - 1, single bit
  45. // DD - day - 02
  46. // D - day 2
  47. // YYYY - year - 2006
  48. // YY - year - 06
  49. // HH - 24 hours - 03
  50. // H - 24 hours - 3
  51. // hh - 12 hours - 03
  52. // h - 12 hours - 3
  53. // mm - minute - 04
  54. // m - minute - 4
  55. // ss - second - 05
  56. // s - second = 5
  57. func DateT(t time.Time, format string) string {
  58. res := strings.Replace(format, "MM", t.Format("01"), -1)
  59. res = strings.Replace(res, "M", t.Format("1"), -1)
  60. res = strings.Replace(res, "DD", t.Format("02"), -1)
  61. res = strings.Replace(res, "D", t.Format("2"), -1)
  62. res = strings.Replace(res, "YYYY", t.Format("2006"), -1)
  63. res = strings.Replace(res, "YY", t.Format("06"), -1)
  64. res = strings.Replace(res, "HH", fmt.Sprintf("%02d", t.Hour()), -1)
  65. res = strings.Replace(res, "H", fmt.Sprintf("%d", t.Hour()), -1)
  66. res = strings.Replace(res, "hh", t.Format("03"), -1)
  67. res = strings.Replace(res, "h", t.Format("3"), -1)
  68. res = strings.Replace(res, "mm", t.Format("04"), -1)
  69. res = strings.Replace(res, "m", t.Format("4"), -1)
  70. res = strings.Replace(res, "ss", t.Format("05"), -1)
  71. res = strings.Replace(res, "s", t.Format("5"), -1)
  72. return res
  73. }
  74. // Parse Date use PHP time format.
  75. func DateParse(dateString, format string) (time.Time, error) {
  76. replacer := strings.NewReplacer(datePatterns...)
  77. format = replacer.Replace(format)
  78. return time.ParseInLocation(format, dateString, time.Local)
  79. }
  80. // Format unix time int64 to string
  81. func Date(ti int64, format string) string {
  82. t := time.Unix(int64(ti), 0)
  83. return DateT(t, format)
  84. }
  85. // Format unix time string to string
  86. func DateS(ts string, format string) string {
  87. i, _ := strconv.ParseInt(ts, 10, 64)
  88. return Date(i, format)
  89. }
  90. func GetTimeFormatByType(timeType int) (format string){
  91. switch timeType{
  92. case 203:
  93. format = "YmdHi"
  94. case 204:
  95. format = "YmdHis"
  96. case 102:
  97. format = "Ymd"
  98. default:
  99. format = "YmdHi"
  100. }
  101. return
  102. }
  103. //format time like java, such as: yyyy-MM-dd HH:mm:ss
  104. func TimeFormat(t time.Time, format string) string {
  105. //year
  106. if strings.ContainsAny(format, "y") {
  107. year := strconv.Itoa(t.Year())
  108. if strings.Count(format, "yy") == 1 && strings.Count(format, "y") == 2 {
  109. format = strings.Replace(format, "yy", year[2:], 1)
  110. } else if strings.Count(format, "yyyy") == 1 && strings.Count(format, "y") == 4 {
  111. format = strings.Replace(format, "yyyy", year, 1)
  112. } else {
  113. log.Fatalln("format year error! please 'yyyy' or 'yy'")
  114. }
  115. }
  116. //month
  117. if strings.ContainsAny(format, "M") {
  118. var month string
  119. if int(t.Month()) < 10 {
  120. month = "0" + strconv.Itoa(int(t.Month()))
  121. } else {
  122. month = strconv.Itoa(int(t.Month()))
  123. }
  124. if strings.Count(format, "MM") == 1 && strings.Count(format, "M") == 2 {
  125. format = strings.Replace(format, "MM", month, 1)
  126. } else {
  127. log.Fatalln("format month error! please 'MM'")
  128. }
  129. }
  130. //day
  131. if strings.ContainsAny(format, "d") {
  132. var day string
  133. if t.Day() < 10 {
  134. day = "0" + strconv.Itoa(t.Day())
  135. } else {
  136. day = strconv.Itoa(t.Day())
  137. }
  138. if strings.Count(format, "dd") == 1 && strings.Count(format, "d") == 2 {
  139. format = strings.Replace(format, "dd", day, 1)
  140. } else {
  141. log.Fatalln("format day error! please 'dd'")
  142. }
  143. }
  144. //hour
  145. if strings.ContainsAny(format, "H") {
  146. var hour string
  147. if t.Hour() < 10 {
  148. hour = "0" + strconv.Itoa(t.Hour())
  149. } else {
  150. hour = strconv.Itoa(t.Hour())
  151. }
  152. if strings.Count(format, "HH") == 1 && strings.Count(format, "H") == 2 {
  153. format = strings.Replace(format, "HH", hour, 1)
  154. } else {
  155. log.Fatalln("format hour error! please 'HH'")
  156. }
  157. }
  158. //minute
  159. if strings.ContainsAny(format, "m") {
  160. var minute string
  161. if t.Minute() < 10 {
  162. minute = "0" + strconv.Itoa(t.Minute())
  163. } else {
  164. minute = strconv.Itoa(t.Minute())
  165. }
  166. if strings.Count(format, "mm") == 1 && strings.Count(format, "m") == 2 {
  167. format = strings.Replace(format, "mm", minute, 1)
  168. } else {
  169. log.Fatalln("format minute error! please 'mm'")
  170. }
  171. }
  172. //second
  173. if strings.ContainsAny(format, "s") {
  174. var second string
  175. if t.Second() < 10 {
  176. second = "0" + strconv.Itoa(t.Second())
  177. } else {
  178. second = strconv.Itoa(t.Second())
  179. }
  180. if strings.Count(format, "ss") == 1 && strings.Count(format, "s") == 2 {
  181. format = strings.Replace(format, "ss", second, 1)
  182. } else {
  183. log.Fatalln("format second error! please 'ss'")
  184. }
  185. }
  186. return format
  187. }
  188. //2007-11-23 10:02:14/20071123100214
  189. func TimeParse(str string) (time.Time, error) {
  190. ll := len(str)
  191. if ll == 19 {
  192. loc, _ := time.LoadLocation("Local")
  193. return time.ParseInLocation("2006-01-02 15:04:05", str, loc)
  194. } else if ll == 14 {
  195. loc, _ := time.LoadLocation("Local")
  196. return time.ParseInLocation("20060102150405", str, loc)
  197. }
  198. return time.Time{}, errors.New("str len is error")
  199. }
  200. //17:02:03/170203
  201. func TimeParseHHmmss(str string) (time.Time, error) {
  202. ll := len(str)
  203. if ll != 8 && ll != 6 {
  204. return time.Time{}, errors.New("input str is error")
  205. }
  206. if ll == 8 {
  207. return TimeParse(TimeFormat(time.Now(), "yyyy-MM-dd "+str))
  208. }
  209. return TimeParse(TimeFormat(time.Now(), "yyyyMMdd"+str))
  210. }
  211. //17:02/1702
  212. func TimeParseHHmm(str string) (time.Time, error) {
  213. ll := len(str)
  214. if ll != 5 && ll != 4 {
  215. return time.Time{}, errors.New("input str is error")
  216. }
  217. if ll == 5 {
  218. return TimeParse(TimeFormat(time.Now(), "yyyy-MM-dd "+str+":00"))
  219. }
  220. return TimeParse(TimeFormat(time.Now(), "yyyyMMdd"+str+"00"))
  221. }
  222. //2007-05-69/20070569
  223. func TimeParseyyyyMMdd(str string) (time.Time, error) {
  224. ll := len(str)
  225. if ll != 10 && ll != 8 {
  226. return time.Time{}, errors.New("input str is error")
  227. }
  228. if ll == 10 {
  229. return TimeParse(TimeFormat(time.Now(), str+" 00:00:00"))
  230. }
  231. return TimeParse(TimeFormat(time.Now(), str+"000000"))
  232. }