Passed
Push — v3.0.0 ( 99da91...e43130 )
by Serhii
01:30
created

timeago.timestampFromPastDate   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
dl 0
loc 2
rs 10
c 0
b 0
f 0
nop 1
1
package timeago
2
3
import (
4
	"encoding/json"
5
	"errors"
6
	"fmt"
7
	"log"
8
	"os"
9
	"time"
10
)
11
12
func unixToTime(timestamp int) time.Time {
13
	return time.Unix(int64(timestamp), 0)
14
}
15
16
func timestampFromPastDate(subDuration time.Duration) int {
17
	return int(time.Now().Add(-subDuration).UnixNano() / 1000000000)
18
}
19
20
func errorf(msg string, a ...interface{}) error {
21
	return fmt.Errorf("[Timeago]: "+msg, a...)
22
}
23
24
func parseLangSet(fileName string) *LangSet {
25
	content := readFile(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 readFile(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 isFilePresent(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