date.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. // Drop resolution to seconds
  69. duration := time.Since(t).Round(time.Second)
  70. return duration.String()
  71. }
  72. func duration(sec any) string {
  73. var n int64
  74. switch value := sec.(type) {
  75. default:
  76. n = 0
  77. case string:
  78. n, _ = strconv.ParseInt(value, 10, 64)
  79. case int64:
  80. n = value
  81. }
  82. return (time.Duration(n) * time.Second).String()
  83. }
  84. func durationRound(duration any) string {
  85. var d time.Duration
  86. switch duration := duration.(type) {
  87. default:
  88. d = 0
  89. case string:
  90. d, _ = time.ParseDuration(duration)
  91. case int64:
  92. d = time.Duration(duration)
  93. case time.Time:
  94. d = time.Since(duration)
  95. }
  96. u := uint64(d)
  97. neg := d < 0
  98. if neg {
  99. u = -u
  100. }
  101. var (
  102. year = uint64(time.Hour) * 24 * 365
  103. month = uint64(time.Hour) * 24 * 30
  104. day = uint64(time.Hour) * 24
  105. hour = uint64(time.Hour)
  106. minute = uint64(time.Minute)
  107. second = uint64(time.Second)
  108. )
  109. switch {
  110. case u > year:
  111. return strconv.FormatUint(u/year, 10) + "y"
  112. case u > month:
  113. return strconv.FormatUint(u/month, 10) + "mo"
  114. case u > day:
  115. return strconv.FormatUint(u/day, 10) + "d"
  116. case u > hour:
  117. return strconv.FormatUint(u/hour, 10) + "h"
  118. case u > minute:
  119. return strconv.FormatUint(u/minute, 10) + "m"
  120. case u > second:
  121. return strconv.FormatUint(u/second, 10) + "s"
  122. }
  123. return "0s"
  124. }
  125. func toDate(fmt, str string) time.Time {
  126. t, _ := time.ParseInLocation(fmt, str, time.Local)
  127. return t
  128. }
  129. func mustToDate(fmt, str string) (time.Time, error) {
  130. return time.ParseInLocation(fmt, str, time.Local)
  131. }
  132. func unixEpoch(date time.Time) string {
  133. return strconv.FormatInt(date.Unix(), 10)
  134. }