-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdateutils_test.go
68 lines (59 loc) · 2.34 KB
/
dateutils_test.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
package dateutil
import (
"fmt"
"math/rand"
"testing"
"time"
)
func TestDifference(t *testing.T) {
from := time.Date(2018, 1, 31, 5, 0, 0, 0, time.UTC)
to := time.Date(2019, 2, 1, 1, 30, 59, 0, time.UTC)
if !checkDate(from, to) {
t.Error("Incorrect, Did't get same to when difference added with from time")
}
from = time.Date(2019, 1, 31, 5, 0, 0, 0, time.UTC)
to = time.Date(2019, 2, 1, 1, 30, 59, 0, time.UTC)
if !checkDate(from, to) {
t.Error("Incorrect, Did't get same to when difference added with from time")
}
from = time.Date(1993, 4, 8, 22, 55, 49, 0, time.UTC)
to = time.Date(2019, 4, 22, 10, 30, 59, 0, time.UTC)
if !checkDate(from, to) {
t.Error("Incorrect, Did't get same to when difference added with from time")
}
from = time.Date(1993, 12, 6, 0, 55, 49, 0, time.UTC)
to = time.Date(2019, 4, 22, 10, 30, 59, 0, time.UTC)
if !checkDate(from, to) {
t.Error("Incorrect, Did't get same to when difference added with from time")
}
from = time.Date(2019, 12, 22, 10, 30, 59, 0, time.UTC)
to = time.Date(2019, 12, 22, 10, 30, 59, 0, time.UTC)
if !checkDate(from, to) {
t.Error("Incorrect, Did't get same to when difference added with from time")
}
from = time.Date(2019, 12, 22, 10, 30, 59, 0, time.UTC)
tempT := from.Add(time.Duration(rand.Int()))
to = time.Date(tempT.Year(), tempT.Month(), tempT.Day(), tempT.Hour(), tempT.Minute(), tempT.Second(), 0, time.UTC)
if !checkDate(from, to) {
t.Error("Incorrect, Did't get same to when difference added with from time")
}
to = time.Date(2019, 12, 22, 10, 30, 59, 0, time.UTC)
tempT = to.Add(time.Duration(rand.Int()) * -1)
from = time.Date(tempT.Year(), tempT.Month(), tempT.Day(), tempT.Hour(), tempT.Minute(), tempT.Second(), 0, time.UTC)
if !checkDate(from, to) {
t.Error("Incorrect, Did't get same to when difference added with from time")
}
}
func checkDate(from, to time.Time) bool {
dif := Difference(from, to)
fmt.Println(dif)
fmt.Println(from)
fmt.Println(to)
fmt.Printf("%d Years %d Months %d Days %d H %d M %d S\n", dif.Years, dif.Months, dif.Days, dif.Hours, dif.Minutes, dif.Seconds)
check := from.AddDate(dif.Years, dif.Months, dif.Days)
check = check.Add(time.Hour * time.Duration(dif.Hours))
check = check.Add(time.Minute * time.Duration(dif.Minutes))
check = check.Add(time.Second * time.Duration(dif.Seconds))
fmt.Println(check)
return to.Equal(check)
}