tool.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. "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. func Str2html(raw string) template.HTML {
  28. return template.HTML(raw)
  29. }
  30. // TimeSince calculates the time interval and generate user-friendly string.
  31. func TimeSince(then time.Time) string {
  32. now := time.Now()
  33. lbl := "ago"
  34. diff := now.Unix() - then.Unix()
  35. if then.After(now) {
  36. lbl = "from now"
  37. diff = then.Unix() - now.Unix()
  38. }
  39. switch {
  40. case diff <= 0:
  41. return "now"
  42. case diff <= 2:
  43. return fmt.Sprintf("1 second %s", lbl)
  44. case diff < 1*Minute:
  45. return fmt.Sprintf("%d seconds %s", diff, lbl)
  46. case diff < 2*Minute:
  47. return fmt.Sprintf("1 minute %s", lbl)
  48. case diff < 1*Hour:
  49. return fmt.Sprintf("%d minutes %s", diff/Minute, lbl)
  50. case diff < 2*Hour:
  51. return fmt.Sprintf("1 hour %s", lbl)
  52. case diff < 1*Day:
  53. return fmt.Sprintf("%d hours %s", diff/Hour, lbl)
  54. case diff < 2*Day:
  55. return fmt.Sprintf("1 day %s", lbl)
  56. case diff < 1*Week:
  57. return fmt.Sprintf("%d days %s", diff/Day, lbl)
  58. case diff < 2*Week:
  59. return fmt.Sprintf("1 week %s", lbl)
  60. case diff < 1*Month:
  61. return fmt.Sprintf("%d weeks %s", diff/Week, lbl)
  62. case diff < 2*Month:
  63. return fmt.Sprintf("1 month %s", lbl)
  64. case diff < 1*Year:
  65. return fmt.Sprintf("%d months %s", diff/Month, lbl)
  66. case diff < 18*Month:
  67. return fmt.Sprintf("1 year %s", lbl)
  68. }
  69. return then.String()
  70. }
  71. // Subtract deals with subtraction of all types of number.
  72. func Subtract(left interface{}, right interface{}) interface{} {
  73. var rleft, rright int64
  74. var fleft, fright float64
  75. var isInt bool = true
  76. switch left.(type) {
  77. case int:
  78. rleft = int64(left.(int))
  79. case int8:
  80. rleft = int64(left.(int8))
  81. case int16:
  82. rleft = int64(left.(int16))
  83. case int32:
  84. rleft = int64(left.(int32))
  85. case int64:
  86. rleft = left.(int64)
  87. case float32:
  88. fleft = float64(left.(float32))
  89. isInt = false
  90. case float64:
  91. fleft = left.(float64)
  92. isInt = false
  93. }
  94. switch right.(type) {
  95. case int:
  96. rright = int64(right.(int))
  97. case int8:
  98. rright = int64(right.(int8))
  99. case int16:
  100. rright = int64(right.(int16))
  101. case int32:
  102. rright = int64(right.(int32))
  103. case int64:
  104. rright = right.(int64)
  105. case float32:
  106. fright = float64(left.(float32))
  107. isInt = false
  108. case float64:
  109. fleft = left.(float64)
  110. isInt = false
  111. }
  112. if isInt {
  113. return rleft - rright
  114. } else {
  115. return fleft + float64(rleft) - (fright + float64(rright))
  116. }
  117. }
  118. type Actioner interface {
  119. GetOpType() int
  120. GetActUserName() string
  121. GetRepoName() string
  122. }
  123. // ActionIcon accepts a int that represents action operation type
  124. // and returns a icon class name.
  125. func ActionIcon(opType int) string {
  126. switch opType {
  127. case 1: // Create repository.
  128. return "plus-circle"
  129. default:
  130. return "invalid type"
  131. }
  132. }
  133. const (
  134. CreateRepoTpl = `<a href="/user/%s">%s</a> created repository <a href="/%s/%s">%s</a>`
  135. )
  136. // ActionDesc accepts int that represents action operation type
  137. // and returns the description.
  138. func ActionDesc(act Actioner) string {
  139. actUserName := act.GetActUserName()
  140. repoName := act.GetRepoName()
  141. switch act.GetOpType() {
  142. case 1: // Create repository.
  143. return fmt.Sprintf(CreateRepoTpl, actUserName, actUserName, actUserName, repoName, repoName)
  144. default:
  145. return "invalid type"
  146. }
  147. }