Total Lines | 64 |
Duplicated Lines | 0 % |
Coverage | 76.19% |
Changes | 0 |
1 | package timeago |
||
2 | |||
3 | import ( |
||
4 | "encoding/json" |
||
5 | "errors" |
||
6 | "io/ioutil" |
||
7 | "log" |
||
8 | "os" |
||
9 | "strconv" |
||
10 | "time" |
||
11 | ) |
||
12 | |||
13 | func fileExists(filePath string) (bool, error) { |
||
14 | 1 | _, err := os.Stat(filePath) |
|
15 | |||
16 | 1 | if err == nil { |
|
17 | 1 | return true, nil |
|
18 | } |
||
19 | |||
20 | if errors.Is(err, os.ErrNotExist) { |
||
21 | return false, nil |
||
22 | } |
||
23 | |||
24 | return false, err |
||
25 | } |
||
26 | |||
27 | func getFileContent(filePath string) []byte { |
||
28 | 1 | content, err := ioutil.ReadFile(filePath) |
|
29 | |||
30 | 1 | if err != nil { |
|
31 | log.Fatal(err) |
||
32 | } |
||
33 | |||
34 | 1 | return content |
|
35 | } |
||
36 | |||
37 | func getTimestampOfPastDate(subDuration time.Duration) int { |
||
38 | 1 | return int(time.Now().Add(-subDuration).UnixNano() / 1000000000) |
|
39 | } |
||
40 | |||
41 | func parseTimestampToString(timestamp int) string { |
||
42 | 1 | return time.Unix(int64(timestamp), 0).Format("2006-01-02 15:04:05") |
|
43 | } |
||
44 | |||
45 | // gets the last digit from a |
||
46 | func getLastNumberDigit(num int) int { |
||
47 | 1 | numStr := strconv.Itoa(num) |
|
48 | 1 | result, _ := strconv.Atoi(numStr[len(numStr)-1:]) |
|
49 | |||
50 | 1 | return result |
|
51 | } |
||
52 | |||
53 | func parseJsonIntoLang(fileName string) Lang { |
||
54 | 1 | content := getFileContent(fileName) |
|
55 | |||
56 | 1 | var result Lang |
|
57 | |||
58 | 1 | err := json.Unmarshal(content, &result) |
|
59 | |||
60 | 1 | if err != nil { |
|
61 | log.Fatal(err) |
||
62 | } |
||
63 | |||
64 | 1 | return result |
|
65 | } |
||
66 |