SerhiiCho /
timeago
| 1 | package timeago |
||
| 2 | |||
| 3 | import ( |
||
| 4 | "strconv" |
||
| 5 | "strings" |
||
| 6 | "time" |
||
| 7 | ) |
||
| 8 | |||
| 9 | // Take coverts given datetime into `x time ago` format. |
||
| 10 | // For displaying `Online` word if date interval within |
||
| 11 | // 60 seconds, add `|online` flag to the datetime string. |
||
| 12 | // Format must be [year-month-day hours:minutes:seconds} |
||
| 13 | func Take(datetime string) string { |
||
| 14 | option, hasOption := getOption(&datetime) |
||
| 15 | |||
| 16 | loc, _ := time.LoadLocation(location) |
||
| 17 | parsedTime, _ := time.ParseInLocation("2006-01-02 15:04:05", datetime, loc) |
||
| 18 | seconds := int(time.Now().In(loc).Sub(parsedTime).Seconds()) |
||
| 19 | |||
| 20 | switch { |
||
| 21 | case seconds < 0 && option == "online": |
||
| 22 | return trans("online") |
||
| 23 | case seconds < 0: |
||
| 24 | return getWords("seconds", 0) |
||
| 25 | } |
||
| 26 | |||
| 27 | minutes := seconds / 60 |
||
| 28 | hours := seconds / 3600 |
||
| 29 | days := seconds / 86400 |
||
| 30 | weeks := seconds / 604800 |
||
| 31 | months := seconds / 2629440 |
||
| 32 | years := seconds / 31553280 |
||
| 33 | |||
| 34 | switch { |
||
| 35 | case hasOption && option == "online" && seconds < 60: |
||
| 36 | return trans("online") |
||
| 37 | case seconds < 60: |
||
| 38 | return getWords("seconds", seconds) |
||
| 39 | case minutes < 60: |
||
| 40 | return getWords("minutes", minutes) |
||
| 41 | case hours < 24: |
||
| 42 | return getWords("hours", hours) |
||
| 43 | case days < 7: |
||
| 44 | return getWords("days", days) |
||
| 45 | case weeks < 4: |
||
| 46 | return getWords("weeks", weeks) |
||
| 47 | case months < 12: |
||
| 48 | return getWords("months", months) |
||
| 49 | } |
||
| 50 | |||
| 51 | return getWords("years", years) |
||
| 52 | } |
||
| 53 | |||
| 54 | func getLastNumber(num int) int { |
||
| 55 | numStr := strconv.Itoa(num) |
||
| 56 | result, _ := strconv.Atoi(numStr[len(numStr)-1:]) |
||
| 57 | |||
| 58 | return result |
||
| 59 | } |
||
| 60 | |||
| 61 | func getWords(timeKind string, num int) string { |
||
| 62 | lastNum := getLastNumber(num) |
||
| 63 | var index int |
||
| 64 | |||
| 65 | switch { |
||
| 66 | case lastNum == 1 && num == 11: |
||
| 67 | index = 2 |
||
| 68 | break |
||
| 69 | case lastNum == 1: |
||
| 70 | index = 0 |
||
| 71 | break |
||
| 72 | case lastNum > 1 && lastNum < 5: |
||
| 73 | index = 1 |
||
| 74 | break |
||
| 75 | default: |
||
| 76 | index = 2 |
||
| 77 | } |
||
| 78 | |||
| 79 | timeTrans := getTimeTranslations() |
||
| 80 | |||
| 81 | return strconv.Itoa(num) + " " + timeTrans[timeKind][index] + " " + trans("ago") |
||
| 82 | } |
||
| 83 | |||
| 84 | // getTimeTranslations returns array of translations for different |
||
| 85 | // cases. For example `1 second` must not have `s` at the end |
||
| 86 | // but `2 seconds` requires `s`. So this method keeps all |
||
| 87 | // possible options for the translated word. |
||
| 88 | func getTimeTranslations() map[string][]string { |
||
| 89 | return map[string][]string{ |
||
| 90 | "seconds": {trans("second"), trans("seconds"), trans("seconds2")}, |
||
| 91 | "minutes": {trans("minute"), trans("minutes"), trans("minutes2")}, |
||
| 92 | "hours": {trans("hour"), trans("hours"), trans("hours2")}, |
||
| 93 | "days": {trans("day"), trans("days"), trans("days2")}, |
||
| 94 | "weeks": {trans("week"), trans("weeks"), trans("weeks2")}, |
||
| 95 | "months": {trans("month"), trans("months"), trans("months2")}, |
||
| 96 | "years": {trans("year"), trans("years"), trans("years2")}, |
||
| 97 | } |
||
| 98 | } |
||
| 99 | |||
| 100 | func getOption(datetime *string) (string, bool) { |
||
| 101 | date := *datetime |
||
| 102 | spittedDateString := strings.Split(date, "|") |
||
| 103 | |||
| 104 | if len(spittedDateString) > 1 { |
||
| 105 | *datetime = spittedDateString[0] |
||
| 106 | return spittedDateString[1], true |
||
| 107 | } |
||
| 108 | |||
| 109 | return "", false |
||
| 110 | } |
||
| 111 | |||
| 112 | func trans(key string) string { |
||
| 113 | if language == "ru" { |
||
| 114 | return getRussian()[key] |
||
| 115 | } else { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 116 | return getEnglish()[key] |
||
| 117 | } |
||
| 118 | } |
||
| 119 |