123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- package base
- import (
- "crypto/md5"
- "encoding/hex"
- "fmt"
- "math"
- "strings"
- "time"
- )
- func EncodeMd5(str string) string {
- m := md5.New()
- m.Write([]byte(str))
- return hex.EncodeToString(m.Sum(nil))
- }
- const (
- Minute = 60
- Hour = 60 * Minute
- Day = 24 * Hour
- Week = 7 * Day
- Month = 30 * Day
- Year = 12 * Month
- )
- func TimeSince(then time.Time) string {
- now := time.Now()
- lbl := "ago"
- diff := now.Unix() - then.Unix()
- if then.After(now) {
- lbl = "from now"
- diff = then.Unix() - now.Unix()
- }
- switch {
- case diff <= 0:
- return "now"
- case diff <= 2:
- return fmt.Sprintf("1 second %s", lbl)
- case diff < 1*Minute:
- return fmt.Sprintf("%d seconds %s", diff, lbl)
- case diff < 2*Minute:
- return fmt.Sprintf("1 minute %s", lbl)
- case diff < 1*Hour:
- return fmt.Sprintf("%d minutes %s", diff/Minute, lbl)
- case diff < 2*Hour:
- return fmt.Sprintf("1 hour %s", lbl)
- case diff < 1*Day:
- return fmt.Sprintf("%d hours %s", diff/Hour, lbl)
- case diff < 2*Day:
- return fmt.Sprintf("1 day %s", lbl)
- case diff < 1*Week:
- return fmt.Sprintf("%d days %s", diff/Day, lbl)
- case diff < 2*Week:
- return fmt.Sprintf("1 week %s", lbl)
- case diff < 1*Month:
- return fmt.Sprintf("%d weeks %s", diff/Week, lbl)
- case diff < 2*Month:
- return fmt.Sprintf("1 month %s", lbl)
- case diff < 1*Year:
- return fmt.Sprintf("%d months %s", diff/Month, lbl)
- case diff < 18*Month:
- return fmt.Sprintf("1 year %s", lbl)
- }
- return then.String()
- }
- const (
- Byte = 1
- KByte = Byte * 1024
- MByte = KByte * 1024
- GByte = MByte * 1024
- TByte = GByte * 1024
- PByte = TByte * 1024
- EByte = PByte * 1024
- )
- var bytesSizeTable = map[string]uint64{
- "b": Byte,
- "kb": KByte,
- "mb": MByte,
- "gb": GByte,
- "tb": TByte,
- "pb": PByte,
- "eb": EByte,
- }
- func logn(n, b float64) float64 {
- return math.Log(n) / math.Log(b)
- }
- func humanateBytes(s uint64, base float64, sizes []string) string {
- if s < 10 {
- return fmt.Sprintf("%dB", s)
- }
- e := math.Floor(logn(float64(s), base))
- suffix := sizes[int(e)]
- val := float64(s) / math.Pow(base, math.Floor(e))
- f := "%.0f"
- if val < 10 {
- f = "%.1f"
- }
- return fmt.Sprintf(f+"%s", val, suffix)
- }
- func FileSize(s int64) string {
- sizes := []string{"B", "KB", "MB", "GB", "TB", "PB", "EB"}
- return humanateBytes(uint64(s), 1024, sizes)
- }
- func Subtract(left interface{}, right interface{}) interface{} {
- var rleft, rright int64
- var fleft, fright float64
- var isInt bool = true
- switch left.(type) {
- case int:
- rleft = int64(left.(int))
- case int8:
- rleft = int64(left.(int8))
- case int16:
- rleft = int64(left.(int16))
- case int32:
- rleft = int64(left.(int32))
- case int64:
- rleft = left.(int64)
- case float32:
- fleft = float64(left.(float32))
- isInt = false
- case float64:
- fleft = left.(float64)
- isInt = false
- }
- switch right.(type) {
- case int:
- rright = int64(right.(int))
- case int8:
- rright = int64(right.(int8))
- case int16:
- rright = int64(right.(int16))
- case int32:
- rright = int64(right.(int32))
- case int64:
- rright = right.(int64)
- case float32:
- fright = float64(left.(float32))
- isInt = false
- case float64:
- fleft = left.(float64)
- isInt = false
- }
- if isInt {
- return rleft - rright
- } else {
- return fleft + float64(rleft) - (fright + float64(rright))
- }
- }
- var datePatterns = []string{
-
- "Y", "2006",
- "y", "06",
-
- "m", "01",
- "n", "1",
- "M", "Jan",
- "F", "January",
-
- "d", "02",
- "j", "2",
-
- "D", "Mon",
- "l", "Monday",
-
- "g", "3",
- "G", "15",
- "h", "03",
- "H", "15",
- "a", "pm",
- "A", "PM",
- "i", "04",
- "s", "05",
-
- "T", "MST",
- "P", "-07:00",
- "O", "-0700",
-
- "r", time.RFC1123Z,
- }
- func DateParse(dateString, format string) (time.Time, error) {
- replacer := strings.NewReplacer(datePatterns...)
- format = replacer.Replace(format)
- return time.ParseInLocation(format, dateString, time.Local)
- }
- func DateFormat(t time.Time, format string) string {
- replacer := strings.NewReplacer(datePatterns...)
- format = replacer.Replace(format)
- return t.Format(format)
- }
- type Actioner interface {
- GetOpType() int
- GetActUserName() string
- GetRepoName() string
- }
- func ActionIcon(opType int) string {
- switch opType {
- case 1:
- return "plus-circle"
- default:
- return "invalid type"
- }
- }
- const (
- CreateRepoTpl = `<a href="/user/%s">%s</a> created repository <a href="/%s/%s">%s</a>`
- )
- func ActionDesc(act Actioner) string {
- actUserName := act.GetActUserName()
- repoName := act.GetRepoName()
- switch act.GetOpType() {
- case 1:
- return fmt.Sprintf(CreateRepoTpl, actUserName, actUserName, actUserName, repoName, repoName)
- default:
- return "invalid type"
- }
- }
|