Total Lines | 59 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | package timeago |
||
2 | |||
3 | import ( |
||
4 | "encoding/json" |
||
5 | "errors" |
||
6 | "fmt" |
||
7 | "log" |
||
8 | "os" |
||
9 | "time" |
||
10 | ) |
||
11 | |||
12 | func parseTimestampIntoTime(timestamp int) time.Time { |
||
13 | return time.Unix(int64(timestamp), 0) |
||
14 | } |
||
15 | |||
16 | func getTimestampOfPastDate(subDuration time.Duration) int { |
||
17 | return int(time.Now().Add(-subDuration).UnixNano() / 1000000000) |
||
18 | } |
||
19 | |||
20 | func createError(msg string, a ...interface{}) error { |
||
21 | return fmt.Errorf("[Timeago]: "+msg, a...) |
||
22 | } |
||
23 | |||
24 | func parseLangSet(fileName string) *LangSet { |
||
25 | content := fileContent(fileName) |
||
26 | |||
27 | var res LangSet |
||
28 | |||
29 | err := json.Unmarshal(content, &res) |
||
30 | |||
31 | if err != nil { |
||
32 | log.Fatalln(err) |
||
33 | } |
||
34 | |||
35 | return &res |
||
36 | } |
||
37 | |||
38 | func fileContent(filePath string) []byte { |
||
39 | content, err := os.ReadFile(filePath) |
||
40 | |||
41 | if err != nil { |
||
42 | log.Fatalln(err) |
||
43 | } |
||
44 | |||
45 | return content |
||
46 | } |
||
47 | |||
48 | func fileExists(filePath string) (bool, error) { |
||
49 | _, err := os.Stat(filePath) |
||
50 | |||
51 | if err == nil { |
||
52 | return true, nil |
||
53 | } |
||
54 | |||
55 | if errors.Is(err, os.ErrNotExist) { |
||
56 | return false, nil |
||
57 | } |
||
58 | |||
59 | return false, err |
||
60 | } |
||
61 |