date.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package sprig
  2. import (
  3. "strconv"
  4. "time"
  5. )
  6. func date(fmt string, date interface{}) string {
  7. return dateInZone(fmt, date, "Local")
  8. }
  9. func htmlDate(date interface{}) string {
  10. return dateInZone("2006-01-02", date, "Local")
  11. }
  12. func htmlDateInZone(date interface{}, zone string) string {
  13. return dateInZone("2006-01-02", date, zone)
  14. }
  15. func dateInZone(fmt string, date interface{}, zone string) string {
  16. var t time.Time
  17. switch date := date.(type) {
  18. default:
  19. t = time.Now()
  20. case time.Time:
  21. t = date
  22. case *time.Time:
  23. t = *date
  24. case int64:
  25. t = time.Unix(date, 0)
  26. case int:
  27. t = time.Unix(int64(date), 0)
  28. case int32:
  29. t = time.Unix(int64(date), 0)
  30. }
  31. loc, err := time.LoadLocation(zone)
  32. if err != nil {
  33. loc, _ = time.LoadLocation("UTC")
  34. }
  35. return t.In(loc).Format(fmt)
  36. }
  37. func dateModify(fmt string, date time.Time) time.Time {
  38. d, err := time.ParseDuration(fmt)
  39. if err != nil {
  40. return date
  41. }
  42. return date.Add(d)
  43. }
  44. func mustDateModify(fmt string, date time.Time) (time.Time, error) {
  45. d, err := time.ParseDuration(fmt)
  46. if err != nil {
  47. return time.Time{}, err
  48. }
  49. return date.Add(d), nil
  50. }
  51. func dateAgo(date interface{}) string {
  52. var t time.Time
  53. switch date := date.(type) {
  54. default:
  55. t = time.Now()
  56. case time.Time:
  57. t = date
  58. case int64:
  59. t = time.Unix(date, 0)
  60. case int:
  61. t = time.Unix(int64(date), 0)
  62. }
  63. duration := time.Since(t).Round(time.Second)
  64. return duration.String()
  65. }
  66. func duration(sec interface{}) string {
  67. var n int64
  68. switch value := sec.(type) {
  69. default:
  70. n = 0
  71. case string:
  72. n, _ = strconv.ParseInt(value, 10, 64)
  73. case int64:
  74. n = value
  75. }
  76. return (time.Duration(n) * time.Second).String()
  77. }
  78. func durationRound(duration interface{}) string {
  79. var d time.Duration
  80. switch duration := duration.(type) {
  81. default:
  82. d = 0
  83. case string:
  84. d, _ = time.ParseDuration(duration)
  85. case int64:
  86. d = time.Duration(duration)
  87. case time.Time:
  88. d = time.Since(duration)
  89. }
  90. u := uint64(d)
  91. neg := d < 0
  92. if neg {
  93. u = -u
  94. }
  95. var (
  96. year = uint64(time.Hour) * 24 * 365
  97. month = uint64(time.Hour) * 24 * 30
  98. day = uint64(time.Hour) * 24
  99. hour = uint64(time.Hour)
  100. minute = uint64(time.Minute)
  101. second = uint64(time.Second)
  102. )
  103. switch {
  104. case u > year:
  105. return strconv.FormatUint(u/year, 10) + "y"
  106. case u > month:
  107. return strconv.FormatUint(u/month, 10) + "mo"
  108. case u > day:
  109. return strconv.FormatUint(u/day, 10) + "d"
  110. case u > hour:
  111. return strconv.FormatUint(u/hour, 10) + "h"
  112. case u > minute:
  113. return strconv.FormatUint(u/minute, 10) + "m"
  114. case u > second:
  115. return strconv.FormatUint(u/second, 10) + "s"
  116. }
  117. return "0s"
  118. }
  119. func toDate(fmt, str string) time.Time {
  120. t, _ := time.ParseInLocation(fmt, str, time.Local)
  121. return t
  122. }
  123. func mustToDate(fmt, str string) (time.Time, error) {
  124. return time.ParseInLocation(fmt, str, time.Local)
  125. }
  126. func unixEpoch(date time.Time) string {
  127. return strconv.FormatInt(date.Unix(), 10)
  128. }