Passed
Push — v3.0.0 ( 268334 )
by Serhii
01:47
created

timeago.calculateTimeAgo   A

Complexity

Conditions 3

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 12
nop 1
dl 0
loc 25
rs 9.8
c 0
b 0
f 0
1
package timeago
2
3
import (
4
	"fmt"
5
	"log"
6
	"math"
7
	"time"
8
9
	"github.com/SerhiiCho/timeago/v3/config"
10
	"github.com/SerhiiCho/timeago/v3/ctx"
11
	"github.com/SerhiiCho/timeago/v3/langset"
12
	"github.com/SerhiiCho/timeago/v3/option"
13
)
14
15
var cachedJsonResults = map[string]langset.LangSet{}
16
var options = []option.Option{}
17
18
var conf = &config.Config{
19
	Language:     "en",
20
	Location:     "",
21
	Translations: []config.Translation{},
22
}
23
24
// Parse coverts given datetime into `x time ago` format.
25
// First argument can have 3 types:
26
// 1. int (Unix timestamp)
27
// 2. time.Time (Type from Go time package)
28
// 3. string (Datetime string in format 'YYYY-MM-DD HH:MM:SS')
29
func Parse(datetime interface{}, opts ...option.Option) string {
30
	var input time.Time
31
32
	switch date := datetime.(type) {
33
	case int:
34
		input = parseTimestampIntoTime(date)
35
	case string:
36
		input = parseStrIntoTime(date)
37
	default:
38
		input = datetime.(time.Time)
39
	}
40
41
	enableOptions(opts)
42
43
	return calculateTimeAgo(input)
44
}
45
46
func Configure(c *config.Config) {
47
	if c.Language != "" {
48
		conf.Language = c.Language
49
	}
50
51
	if c.Location != "" {
52
		conf.Location = c.Location
53
	}
54
55
	if len(c.Translations) > 0 {
56
		conf.Translations = c.Translations
57
	}
58
}
59
60
func parseStrIntoTime(datetime string) time.Time {
61
	if !conf.LocationIsSet() {
62
		parsedTime, _ := time.Parse("2006-01-02 15:04:05", datetime)
63
		return parsedTime
64
	}
65
66
	location, err := time.LoadLocation(conf.Location)
67
68
	if err != nil {
69
		log.Fatalf("Error in timeago package: %v", err)
70
	}
71
72
	parsedTime, _ := time.ParseInLocation("2006-01-02 15:04:05", datetime, location)
73
74
	return parsedTime
75
}
76
77
func calculateTimeAgo(datetime time.Time) string {
78
	now := time.Now()
79
80
	if conf.LocationIsSet() {
81
		now = applyLocationToTime(now)
82
	}
83
84
	seconds := now.Sub(datetime).Seconds()
85
86
	if seconds < 0 {
87
		enableOption(option.Upcoming)
88
		seconds = math.Abs(seconds)
89
	}
90
91
	context := ctx.New(conf, options)
92
	langSet := langset.New(context)
93
94
	fmt.Printf("-------> %#v\n", langSet)
95
	// switch {
96
	// case seconds < 60 && optionIsEnabled(option.Online):
97
	// 	return trans().Online
98
	// case seconds < 0:
99
	// 	return getWords("seconds", 0)
100
	// }
101
	return ""
102
}
103
104
func applyLocationToTime(datetime time.Time) time.Time {
105
	location, err := time.LoadLocation(conf.Location)
106
107
	if err != nil {
108
		log.Fatalf("Location error in timeago package: %v\n", err)
109
	}
110
111
	return datetime.In(location)
112
}
113
114
func getTimeCalculations(seconds float64) (int, int, int, int, int, int) {
115
	minutes := math.Round(seconds / 60)
116
	hours := math.Round(seconds / 3600)
117
	days := math.Round(seconds / 86400)
118
	weeks := math.Round(seconds / 604800)
119
	months := math.Round(seconds / 2629440)
120
	years := math.Round(seconds / 31553280)
121
122
	return int(minutes), int(hours), int(days), int(weeks), int(months), int(years)
123
}
124