forked from nleeper/goment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiff.go
82 lines (65 loc) · 1.95 KB
/
diff.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
package goment
import (
"math"
"time"
)
type diff struct {
Start *Goment
End *Goment
}
// InYears returns the duration in number of years.
func (d diff) InYears() int {
return d.monthDiff() / 12
}
// InMonths returns the duration in number of months.
func (d diff) InMonths() int {
return d.monthDiff()
}
// InWeeks returns the duration in number of weeks.
func (d diff) InWeeks() int {
return absFloor(math.Floor(float64(d.InDays() / 7)))
}
// InDays returns the duration in number of days.
func (d diff) InDays() int {
return absFloor(math.Floor(float64(d.InHours()) / 24))
}
// InHours returns the duration in number of hours.
func (d diff) InHours() int {
return absFloor(d.subtract().Hours())
}
// InMinutes returns the duration in number of minutes.
func (d diff) InMinutes() int {
return absFloor(d.subtract().Minutes())
}
// InSeconds returns the duration in number of seconds.
func (d diff) InSeconds() int {
return absFloor(d.subtract().Seconds())
}
func (d diff) subtract() time.Duration {
return d.Start.ToTime().Sub(d.End.ToTime())
}
func (d diff) monthDiff() int {
startYear, startMonth := d.Start.Year(), d.Start.Month()
endYear, endMonth := d.End.Year(), d.End.Month()
wholeMonthDiff := ((endYear - startYear) * 12) + int(endMonth-startMonth)
anchor := d.Start.Clone().Add(wholeMonthDiff, "months")
var adjust int
var anchor2 *Goment
if d.End.ToTime().Sub(anchor.ToTime()) < 0 {
anchor2 = d.Start.Clone().Add(wholeMonthDiff-1, "months")
adjust = int(d.End.ToTime().Sub(anchor.ToTime()).Seconds() / (anchor.ToTime().Sub(anchor2.ToTime())).Seconds())
} else {
anchor2 = d.Start.Clone().Add(wholeMonthDiff+1, "months")
adjust = int(d.End.ToTime().Sub(anchor.ToTime()).Seconds() / (anchor2.ToTime().Sub(anchor.ToTime())).Seconds())
}
if wholeMonthDiff == 0 {
return 0
}
return -(wholeMonthDiff + adjust)
}
func absFloor(number float64) int {
if number < 0 {
return int(math.Ceil(number))
}
return int(math.Floor(number))
}