/
time.go
120 lines (101 loc) · 2.51 KB
/
time.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package wtime
import (
"fmt"
"strconv"
"strings"
"time"
)
//DateForm basic date format
const DateForm = "01/2006"
// GetDurantionString returns duration converted to a string with s/m/h
func GetDurantionString(duration int64) string {
d, err := time.ParseDuration(strconv.FormatInt(duration, 10) + "s")
if err != nil {
return ""
}
return d.String()
}
// GetStartEndMonth gets first second of a month described in startD and first second of next month.
// XXX Error Handling
func GetStartEndMonth(startD time.Time) (int64, int64) {
var eyear int
var emonth time.Month
emonth = startD.Month() + 1
eyear = startD.Year()
if startD.Month() == 12 {
emonth = time.January
eyear = startD.Year() + 1
}
start := time.Date(startD.Year(), startD.Month(), 0, 0, 0, 0, 1, time.UTC).Unix()
end := time.Date(eyear, emonth, 0, 0, 0, 0, 0, time.UTC).Unix()
return start, end
}
// CompareStartDate @todo Add Error Handling
func CompareStartDate(startDate string, entryDate int64) bool {
var sd time.Time
var err error
if strings.TrimLeft(startDate, "<>@=") == "" {
sd = time.Now()
} else {
sd, err = time.Parse(DateForm, strings.TrimLeft(startDate, "<>@="))
if err != nil {
panic(err)
}
}
switch string(startDate[0]) {
case ">":
if entryDate >= sd.Unix() {
return true
}
case "<":
if entryDate <= sd.Unix() {
return true
}
case "=":
start, end := GetStartEndMonth(sd)
if entryDate >= (start+1) && entryDate < end {
return true
}
case "@":
// @ means to print only entries related to current month
start, end := GetStartEndMonth(sd)
if entryDate >= start && entryDate < end {
return true
}
default:
return false
}
return false
}
// DehumanizeDuration converts string in format 1w1d1h1m into number of seconds
func DehumanizeDuration(dura string) (int64, error) {
var num string
var base int64
var duration int64
for i := 0; i < len(dura); i++ {
// Check if current char is a number or string
if _, err := strconv.Atoi(string(dura[i])); err == nil {
num += string(dura[i])
} else {
// Convert existing num to number as we got our first char
if d, err := strconv.Atoi(num); err == nil {
base = int64(d)
num = ""
}
switch string(dura[i]) {
case "w":
duration += (base * 604800)
case "d":
duration += (base * 86400)
case "h":
duration += (base * 3600)
case "m":
duration += (base * 60)
default:
return 0, fmt.Errorf("wrong char only w,d,h,m are accepted: %s", string(dura[i]))
}
base = 0
}
}
return duration, nil
}