tool.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. "math"
  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. // TimeSince calculates the time interval and generate user-friendly string.
  29. func TimeSince(then time.Time) string {
  30. now := time.Now()
  31. lbl := "ago"
  32. diff := now.Unix() - then.Unix()
  33. if then.After(now) {
  34. lbl = "from now"
  35. diff = then.Unix() - now.Unix()
  36. }
  37. switch {
  38. case diff <= 0:
  39. return "now"
  40. case diff <= 2:
  41. return fmt.Sprintf("1 second %s", lbl)
  42. case diff < 1*Minute:
  43. return fmt.Sprintf("%d seconds %s", diff, lbl)
  44. case diff < 2*Minute:
  45. return fmt.Sprintf("1 minute %s", lbl)
  46. case diff < 1*Hour:
  47. return fmt.Sprintf("%d minutes %s", diff/Minute, lbl)
  48. case diff < 2*Hour:
  49. return fmt.Sprintf("1 hour %s", lbl)
  50. case diff < 1*Day:
  51. return fmt.Sprintf("%d hours %s", diff/Hour, lbl)
  52. case diff < 2*Day:
  53. return fmt.Sprintf("1 day %s", lbl)
  54. case diff < 1*Week:
  55. return fmt.Sprintf("%d days %s", diff/Day, lbl)
  56. case diff < 2*Week:
  57. return fmt.Sprintf("1 week %s", lbl)
  58. case diff < 1*Month:
  59. return fmt.Sprintf("%d weeks %s", diff/Week, lbl)
  60. case diff < 2*Month:
  61. return fmt.Sprintf("1 month %s", lbl)
  62. case diff < 1*Year:
  63. return fmt.Sprintf("%d months %s", diff/Month, lbl)
  64. case diff < 18*Month:
  65. return fmt.Sprintf("1 year %s", lbl)
  66. }
  67. return then.String()
  68. }
  69. const (
  70. Byte = 1
  71. KByte = Byte * 1024
  72. MByte = KByte * 1024
  73. GByte = MByte * 1024
  74. TByte = GByte * 1024
  75. PByte = TByte * 1024
  76. EByte = PByte * 1024
  77. )
  78. var bytesSizeTable = map[string]uint64{
  79. "b": Byte,
  80. "kb": KByte,
  81. "mb": MByte,
  82. "gb": GByte,
  83. "tb": TByte,
  84. "pb": PByte,
  85. "eb": EByte,
  86. }
  87. func logn(n, b float64) float64 {
  88. return math.Log(n) / math.Log(b)
  89. }
  90. func humanateBytes(s uint64, base float64, sizes []string) string {
  91. if s < 10 {
  92. return fmt.Sprintf("%dB", s)
  93. }
  94. e := math.Floor(logn(float64(s), base))
  95. suffix := sizes[int(e)]
  96. val := float64(s) / math.Pow(base, math.Floor(e))
  97. f := "%.0f"
  98. if val < 10 {
  99. f = "%.1f"
  100. }
  101. return fmt.Sprintf(f+"%s", val, suffix)
  102. }
  103. // FileSize calculates the file size and generate user-friendly string.
  104. func FileSize(s int64) string {
  105. sizes := []string{"B", "KB", "MB", "GB", "TB", "PB", "EB"}
  106. return humanateBytes(uint64(s), 1024, sizes)
  107. }
  108. // Subtract deals with subtraction of all types of number.
  109. func Subtract(left interface{}, right interface{}) interface{} {
  110. var rleft, rright int64
  111. var fleft, fright float64
  112. var isInt bool = true
  113. switch left.(type) {
  114. case int:
  115. rleft = int64(left.(int))
  116. case int8:
  117. rleft = int64(left.(int8))
  118. case int16:
  119. rleft = int64(left.(int16))
  120. case int32:
  121. rleft = int64(left.(int32))
  122. case int64:
  123. rleft = left.(int64)
  124. case float32:
  125. fleft = float64(left.(float32))
  126. isInt = false
  127. case float64:
  128. fleft = left.(float64)
  129. isInt = false
  130. }
  131. switch right.(type) {
  132. case int:
  133. rright = int64(right.(int))
  134. case int8:
  135. rright = int64(right.(int8))
  136. case int16:
  137. rright = int64(right.(int16))
  138. case int32:
  139. rright = int64(right.(int32))
  140. case int64:
  141. rright = right.(int64)
  142. case float32:
  143. fright = float64(left.(float32))
  144. isInt = false
  145. case float64:
  146. fleft = left.(float64)
  147. isInt = false
  148. }
  149. if isInt {
  150. return rleft - rright
  151. } else {
  152. return fleft + float64(rleft) - (fright + float64(rright))
  153. }
  154. }
  155. // DateFormat pattern rules.
  156. var datePatterns = []string{
  157. // year
  158. "Y", "2006", // A full numeric representation of a year, 4 digits Examples: 1999 or 2003
  159. "y", "06", //A two digit representation of a year Examples: 99 or 03
  160. // month
  161. "m", "01", // Numeric representation of a month, with leading zeros 01 through 12
  162. "n", "1", // Numeric representation of a month, without leading zeros 1 through 12
  163. "M", "Jan", // A short textual representation of a month, three letters Jan through Dec
  164. "F", "January", // A full textual representation of a month, such as January or March January through December
  165. // day
  166. "d", "02", // Day of the month, 2 digits with leading zeros 01 to 31
  167. "j", "2", // Day of the month without leading zeros 1 to 31
  168. // week
  169. "D", "Mon", // A textual representation of a day, three letters Mon through Sun
  170. "l", "Monday", // A full textual representation of the day of the week Sunday through Saturday
  171. // time
  172. "g", "3", // 12-hour format of an hour without leading zeros 1 through 12
  173. "G", "15", // 24-hour format of an hour without leading zeros 0 through 23
  174. "h", "03", // 12-hour format of an hour with leading zeros 01 through 12
  175. "H", "15", // 24-hour format of an hour with leading zeros 00 through 23
  176. "a", "pm", // Lowercase Ante meridiem and Post meridiem am or pm
  177. "A", "PM", // Uppercase Ante meridiem and Post meridiem AM or PM
  178. "i", "04", // Minutes with leading zeros 00 to 59
  179. "s", "05", // Seconds, with leading zeros 00 through 59
  180. // time zone
  181. "T", "MST",
  182. "P", "-07:00",
  183. "O", "-0700",
  184. // RFC 2822
  185. "r", time.RFC1123Z,
  186. }
  187. // Parse Date use PHP time format.
  188. func DateParse(dateString, format string) (time.Time, error) {
  189. replacer := strings.NewReplacer(datePatterns...)
  190. format = replacer.Replace(format)
  191. return time.ParseInLocation(format, dateString, time.Local)
  192. }
  193. // Date takes a PHP like date func to Go's time format.
  194. func DateFormat(t time.Time, format string) string {
  195. replacer := strings.NewReplacer(datePatterns...)
  196. format = replacer.Replace(format)
  197. return t.Format(format)
  198. }
  199. type Actioner interface {
  200. GetOpType() int
  201. GetActUserName() string
  202. GetRepoName() string
  203. }
  204. // ActionIcon accepts a int that represents action operation type
  205. // and returns a icon class name.
  206. func ActionIcon(opType int) string {
  207. switch opType {
  208. case 1: // Create repository.
  209. return "plus-circle"
  210. default:
  211. return "invalid type"
  212. }
  213. }
  214. const (
  215. CreateRepoTpl = `<a href="/user/%s">%s</a> created repository <a href="/%s/%s">%s</a>`
  216. )
  217. // ActionDesc accepts int that represents action operation type
  218. // and returns the description.
  219. func ActionDesc(act Actioner) string {
  220. actUserName := act.GetActUserName()
  221. repoName := act.GetRepoName()
  222. switch act.GetOpType() {
  223. case 1: // Create repository.
  224. return fmt.Sprintf(CreateRepoTpl, actUserName, actUserName, actUserName, repoName, repoName)
  225. default:
  226. return "invalid type"
  227. }
  228. }