tool.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package base
  5. import (
  6. "crypto/md5"
  7. "encoding/hex"
  8. "fmt"
  9. "html/template"
  10. "strings"
  11. "time"
  12. )
  13. // Encode string to md5 hex value
  14. func EncodeMd5(str string) string {
  15. m := md5.New()
  16. m.Write([]byte(str))
  17. return hex.EncodeToString(m.Sum(nil))
  18. }
  19. // Seconds-based time units
  20. const (
  21. Minute = 60
  22. Hour = 60 * Minute
  23. Day = 24 * Hour
  24. Week = 7 * Day
  25. Month = 30 * Day
  26. Year = 12 * Month
  27. )
  28. func Str2html(raw string) template.HTML {
  29. return template.HTML(raw)
  30. }
  31. // TimeSince calculates the time interval and generate user-friendly string.
  32. func TimeSince(then time.Time) string {
  33. now := time.Now()
  34. lbl := "ago"
  35. diff := now.Unix() - then.Unix()
  36. if then.After(now) {
  37. lbl = "from now"
  38. diff = then.Unix() - now.Unix()
  39. }
  40. switch {
  41. case diff <= 0:
  42. return "now"
  43. case diff <= 2:
  44. return fmt.Sprintf("1 second %s", lbl)
  45. case diff < 1*Minute:
  46. return fmt.Sprintf("%d seconds %s", diff, lbl)
  47. case diff < 2*Minute:
  48. return fmt.Sprintf("1 minute %s", lbl)
  49. case diff < 1*Hour:
  50. return fmt.Sprintf("%d minutes %s", diff/Minute, lbl)
  51. case diff < 2*Hour:
  52. return fmt.Sprintf("1 hour %s", lbl)
  53. case diff < 1*Day:
  54. return fmt.Sprintf("%d hours %s", diff/Hour, lbl)
  55. case diff < 2*Day:
  56. return fmt.Sprintf("1 day %s", lbl)
  57. case diff < 1*Week:
  58. return fmt.Sprintf("%d days %s", diff/Day, lbl)
  59. case diff < 2*Week:
  60. return fmt.Sprintf("1 week %s", lbl)
  61. case diff < 1*Month:
  62. return fmt.Sprintf("%d weeks %s", diff/Week, lbl)
  63. case diff < 2*Month:
  64. return fmt.Sprintf("1 month %s", lbl)
  65. case diff < 1*Year:
  66. return fmt.Sprintf("%d months %s", diff/Month, lbl)
  67. case diff < 18*Month:
  68. return fmt.Sprintf("1 year %s", lbl)
  69. }
  70. return then.String()
  71. }
  72. // Subtract deals with subtraction of all types of number.
  73. func Subtract(left interface{}, right interface{}) interface{} {
  74. var rleft, rright int64
  75. var fleft, fright float64
  76. var isInt bool = true
  77. switch left.(type) {
  78. case int:
  79. rleft = int64(left.(int))
  80. case int8:
  81. rleft = int64(left.(int8))
  82. case int16:
  83. rleft = int64(left.(int16))
  84. case int32:
  85. rleft = int64(left.(int32))
  86. case int64:
  87. rleft = left.(int64)
  88. case float32:
  89. fleft = float64(left.(float32))
  90. isInt = false
  91. case float64:
  92. fleft = left.(float64)
  93. isInt = false
  94. }
  95. switch right.(type) {
  96. case int:
  97. rright = int64(right.(int))
  98. case int8:
  99. rright = int64(right.(int8))
  100. case int16:
  101. rright = int64(right.(int16))
  102. case int32:
  103. rright = int64(right.(int32))
  104. case int64:
  105. rright = right.(int64)
  106. case float32:
  107. fright = float64(left.(float32))
  108. isInt = false
  109. case float64:
  110. fleft = left.(float64)
  111. isInt = false
  112. }
  113. if isInt {
  114. return rleft - rright
  115. } else {
  116. return fleft + float64(rleft) - (fright + float64(rright))
  117. }
  118. }
  119. // DateFormat pattern rules.
  120. var datePatterns = []string{
  121. // year
  122. "Y", "2006", // A full numeric representation of a year, 4 digits Examples: 1999 or 2003
  123. "y", "06", //A two digit representation of a year Examples: 99 or 03
  124. // month
  125. "m", "01", // Numeric representation of a month, with leading zeros 01 through 12
  126. "n", "1", // Numeric representation of a month, without leading zeros 1 through 12
  127. "M", "Jan", // A short textual representation of a month, three letters Jan through Dec
  128. "F", "January", // A full textual representation of a month, such as January or March January through December
  129. // day
  130. "d", "02", // Day of the month, 2 digits with leading zeros 01 to 31
  131. "j", "2", // Day of the month without leading zeros 1 to 31
  132. // week
  133. "D", "Mon", // A textual representation of a day, three letters Mon through Sun
  134. "l", "Monday", // A full textual representation of the day of the week Sunday through Saturday
  135. // time
  136. "g", "3", // 12-hour format of an hour without leading zeros 1 through 12
  137. "G", "15", // 24-hour format of an hour without leading zeros 0 through 23
  138. "h", "03", // 12-hour format of an hour with leading zeros 01 through 12
  139. "H", "15", // 24-hour format of an hour with leading zeros 00 through 23
  140. "a", "pm", // Lowercase Ante meridiem and Post meridiem am or pm
  141. "A", "PM", // Uppercase Ante meridiem and Post meridiem AM or PM
  142. "i", "04", // Minutes with leading zeros 00 to 59
  143. "s", "05", // Seconds, with leading zeros 00 through 59
  144. // time zone
  145. "T", "MST",
  146. "P", "-07:00",
  147. "O", "-0700",
  148. // RFC 2822
  149. "r", time.RFC1123Z,
  150. }
  151. // Parse Date use PHP time format.
  152. func DateParse(dateString, format string) (time.Time, error) {
  153. replacer := strings.NewReplacer(datePatterns...)
  154. format = replacer.Replace(format)
  155. return time.ParseInLocation(format, dateString, time.Local)
  156. }
  157. // Date takes a PHP like date func to Go's time format.
  158. func DateFormat(t time.Time, format string) string {
  159. replacer := strings.NewReplacer(datePatterns...)
  160. format = replacer.Replace(format)
  161. return t.Format(format)
  162. }
  163. type Actioner interface {
  164. GetOpType() int
  165. GetActUserName() string
  166. GetRepoName() string
  167. }
  168. // ActionIcon accepts a int that represents action operation type
  169. // and returns a icon class name.
  170. func ActionIcon(opType int) string {
  171. switch opType {
  172. case 1: // Create repository.
  173. return "plus-circle"
  174. default:
  175. return "invalid type"
  176. }
  177. }
  178. const (
  179. CreateRepoTpl = `<a href="/user/%s">%s</a> created repository <a href="/%s/%s">%s</a>`
  180. )
  181. // ActionDesc accepts int that represents action operation type
  182. // and returns the description.
  183. func ActionDesc(act Actioner) string {
  184. actUserName := act.GetActUserName()
  185. repoName := act.GetRepoName()
  186. switch act.GetOpType() {
  187. case 1: // Create repository.
  188. return fmt.Sprintf(CreateRepoTpl, actUserName, actUserName, actUserName, repoName, repoName)
  189. default:
  190. return "invalid type"
  191. }
  192. }