date.go 2.9 KB

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