-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathgodate.go
159 lines (137 loc) · 4.46 KB
/
godate.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package godate
import (
"math"
"strconv"
"strings"
"time"
)
type goDate struct {
Time time.Time
TimeZone *time.Location
FirstDayOfWeek time.Weekday
}
//IsBefore checks if the goDate is before the passed goDate
func (d *goDate) IsBefore(compare *goDate) bool {
return d.Time.Before(compare.Time)
}
//IsAfter checks if the goDate is before the passed goDate
func (d *goDate) IsAfter(compare *goDate) bool {
return d.Time.After(compare.Time)
}
//Sub subtracts the 'count' from the goDate using the unit passed
func (d goDate) Sub(count int, unit Unit) *goDate {
return d.Add(-count, unit)
}
//Add adds the 'count' from the goDate using the unit passed
func (d goDate) Add(count int, unit Unit) *goDate {
d.Time = d.Time.Add(time.Duration(unit * Unit(count)))
return &d
}
//Get the difference as a duration type
func (d *goDate) DifferenceAsDuration(compare *goDate) time.Duration {
return d.Time.Sub(compare.Time)
}
//Difference Returns the difference between the Godate and another in the specified unit
//If the difference is negative then the 'compare' date occurs after the date
//Else it occurs before the the date
func (d goDate) Difference(compare *goDate, unit Unit) int {
difference := d.DifferenceAsFloat(compare, unit)
return int(difference)
}
//Get the difference as a float
func (d goDate) DifferenceAsFloat(compare *goDate, unit Unit) float64 {
duration := d.DifferenceAsDuration(compare)
return float64(duration) / float64(time.Duration(unit))
}
//Gets the difference between the relative to the date value in the form of
//1 month before
//1 month after
func (d goDate) DifferenceForHumans(compare *goDate) string {
differenceString, differenceInt := d.AbsDifferenceForHumans(compare)
if differenceInt > 0 {
return differenceString + " before"
} else {
return differenceString + " after"
}
}
//Gets the difference between the relative to current time value in the form of
//1 month ago
//1 month from now
func (d goDate) DifferenceFromNowForHumans() string {
now := Now(d.TimeZone)
differenceString, differenceInt := now.AbsDifferenceForHumans(&d)
if differenceInt > 0 {
return differenceString + " ago"
} else {
return differenceString + " from now"
}
}
//Get the abs difference relative to compare time in the form
//1 month
//2 days
func (d goDate) AbsDifferenceForHumans(compare *goDate) (string, int) {
sentence := make([]string, 2, 2)
duration := Unit(math.Abs(float64(d.DifferenceAsDuration(compare))))
var unit Unit
if duration >= YEAR {
unit = YEAR
} else if duration < YEAR && duration >= MONTH {
unit = MONTH
} else if duration < MONTH && duration >= WEEK {
unit = WEEK
} else if duration < WEEK && duration >= DAY {
unit = DAY
} else if duration < DAY && duration >= HOUR {
unit = HOUR
} else if duration < HOUR && duration >= MINUTE {
unit = MINUTE
} else {
unit = SECOND
}
difference := d.Difference(compare, unit)
sentence[0] = strconv.Itoa(int(math.Abs(float64(difference))))
sentence[1] = unit.String()
if difference == 1 || difference == -1 {
sentence[1] = strings.TrimSuffix(sentence[1], "s")
}
return strings.Join(sentence, " "), difference
}
//MidDay gets the midday time usually 12:00 PM of the current day
func (d *goDate) MidDay() *goDate {
y, m, day := d.Time.Date()
return &goDate{time.Date(y, m, day, 12, 0, 0, 0, d.TimeZone), d.TimeZone,0}
}
//ToDateTimeString Formats and returns the goDate in the form 2006-01-02 15:04:05
func (d *goDate) ToDateTimeString() string{
return d.Format("2006-01-02 15:04:05")
}
//ToDateString Formats and returns the goDate in the form 2006-01-02
func (d *goDate) ToDateString() string{
return d.Format("2006-01-02")
}
//ToFormattedDateString Formats and returns the goDate in the form Jan 02, 2006
func (d *goDate) ToFormattedDateString() string{
return d.Format("Jan 02, 2006")
}
//ToTimeString Formats and returns the goDate in the form 15:04:05
func (d *goDate) ToTimeString() string{
return d.Format("15:04:05")
}
//ToDayTimeString Formats and returns the goDate in the form Mon, Jan 2, 2006 03:04 PM
func (d *goDate) ToDayTimeString() string{
return d.Format("Mon, Jan 2, 2006 03:04 PM")
}
//Check if this is the weekend
func (d *goDate) IsWeekend() bool {
day := d.Time.Weekday()
return day == time.Saturday || day == time.Sunday
}
func (d *goDate) Format(format string) string{
return d.Time.Format(format)
}
func (d *goDate) SetFirstDay(weekday time.Weekday){
d.FirstDayOfWeek = weekday
}
func (d goDate) String() string{
return d.Format("Mon Jan 2 15:04:05 -0700 MST 2006")
}