tool.go 5.5 KB

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