time.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright 2013 com authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License"): you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  11. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  12. // License for the specific language governing permissions and limitations
  13. // under the License.
  14. package com
  15. import (
  16. "fmt"
  17. "strconv"
  18. "strings"
  19. "time"
  20. )
  21. // Format unix time int64 to string
  22. func Date(ti int64, format string) string {
  23. t := time.Unix(int64(ti), 0)
  24. return DateT(t, format)
  25. }
  26. // Format unix time string to string
  27. func DateS(ts string, format string) string {
  28. i, _ := strconv.ParseInt(ts, 10, 64)
  29. return Date(i, format)
  30. }
  31. // Format time.Time struct to string
  32. // MM - month - 01
  33. // M - month - 1, single bit
  34. // DD - day - 02
  35. // D - day 2
  36. // YYYY - year - 2006
  37. // YY - year - 06
  38. // HH - 24 hours - 03
  39. // H - 24 hours - 3
  40. // hh - 12 hours - 03
  41. // h - 12 hours - 3
  42. // mm - minute - 04
  43. // m - minute - 4
  44. // ss - second - 05
  45. // s - second = 5
  46. func DateT(t time.Time, format string) string {
  47. res := strings.Replace(format, "MM", t.Format("01"), -1)
  48. res = strings.Replace(res, "M", t.Format("1"), -1)
  49. res = strings.Replace(res, "DD", t.Format("02"), -1)
  50. res = strings.Replace(res, "D", t.Format("2"), -1)
  51. res = strings.Replace(res, "YYYY", t.Format("2006"), -1)
  52. res = strings.Replace(res, "YY", t.Format("06"), -1)
  53. res = strings.Replace(res, "HH", fmt.Sprintf("%02d", t.Hour()), -1)
  54. res = strings.Replace(res, "H", fmt.Sprintf("%d", t.Hour()), -1)
  55. res = strings.Replace(res, "hh", t.Format("03"), -1)
  56. res = strings.Replace(res, "h", t.Format("3"), -1)
  57. res = strings.Replace(res, "mm", t.Format("04"), -1)
  58. res = strings.Replace(res, "m", t.Format("4"), -1)
  59. res = strings.Replace(res, "ss", t.Format("05"), -1)
  60. res = strings.Replace(res, "s", t.Format("5"), -1)
  61. return res
  62. }
  63. // DateFormat pattern rules.
  64. var datePatterns = []string{
  65. // year
  66. "Y", "2006", // A full numeric representation of a year, 4 digits Examples: 1999 or 2003
  67. "y", "06", //A two digit representation of a year Examples: 99 or 03
  68. // month
  69. "m", "01", // Numeric representation of a month, with leading zeros 01 through 12
  70. "n", "1", // Numeric representation of a month, without leading zeros 1 through 12
  71. "M", "Jan", // A short textual representation of a month, three letters Jan through Dec
  72. "F", "January", // A full textual representation of a month, such as January or March January through December
  73. // day
  74. "d", "02", // Day of the month, 2 digits with leading zeros 01 to 31
  75. "j", "2", // Day of the month without leading zeros 1 to 31
  76. // week
  77. "D", "Mon", // A textual representation of a day, three letters Mon through Sun
  78. "l", "Monday", // A full textual representation of the day of the week Sunday through Saturday
  79. // time
  80. "g", "3", // 12-hour format of an hour without leading zeros 1 through 12
  81. "G", "15", // 24-hour format of an hour without leading zeros 0 through 23
  82. "h", "03", // 12-hour format of an hour with leading zeros 01 through 12
  83. "H", "15", // 24-hour format of an hour with leading zeros 00 through 23
  84. "a", "pm", // Lowercase Ante meridiem and Post meridiem am or pm
  85. "A", "PM", // Uppercase Ante meridiem and Post meridiem AM or PM
  86. "i", "04", // Minutes with leading zeros 00 to 59
  87. "s", "05", // Seconds, with leading zeros 00 through 59
  88. // time zone
  89. "T", "MST",
  90. "P", "-07:00",
  91. "O", "-0700",
  92. // RFC 2822
  93. "r", time.RFC1123Z,
  94. }
  95. // Parse Date use PHP time format.
  96. func DateParse(dateString, format string) (time.Time, error) {
  97. replacer := strings.NewReplacer(datePatterns...)
  98. format = replacer.Replace(format)
  99. return time.ParseInLocation(format, dateString, time.Local)
  100. }