-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrelative_functions.go
More file actions
393 lines (363 loc) · 10.7 KB
/
relative_functions.go
File metadata and controls
393 lines (363 loc) · 10.7 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
package gotime
import "time"
// -----------------Year Functions-----------------
// YearStart returns the first day of the year for the given date.
// If no date is provided, it uses the current time.
//
// Example:
//
// start := gotime.YearStart() // First day of current year
// // start: 2025-01-01 00:00:00
//
// someDate := time.Date(2024, 6, 15, 0, 0, 0, 0, time.UTC)
// start = gotime.YearStart(someDate)
// // start: 2024-01-01 00:00:00
func YearStart(dt ...time.Time) time.Time {
var t time.Time
if len(dt) > 0 {
t = dt[0]
} else {
t = time.Now()
}
start := time.Date(t.Year(), 1, 1, 0, 0, 0, 0, t.Location())
return start
}
// YearEnd returns the last day and last second of the year for the given date.
// If no date is provided, it uses the current time.
//
// Example:
//
// end := gotime.YearEnd() // Last day of current year
// // end: 2025-12-31 23:59:59.999999999
//
// someDate := time.Date(2024, 6, 15, 0, 0, 0, 0, time.UTC)
// end = gotime.YearEnd(someDate)
// // end: 2024-12-31 23:59:59.999999999
func YearEnd(dt ...time.Time) time.Time {
var t time.Time
if len(dt) > 0 {
t = dt[0]
} else {
t = time.Now()
}
end := time.Date(t.Year(), 12, 31, 23, 59, 59, 999999999, t.Location())
return end
}
// Years returns the date after adding the specified number of years to the given date.
// If no date is provided, it uses the current time.
// If years is 0, it returns the original date unchanged.
//
// Example:
//
// future := gotime.Years(2) // 2 years from now
// // future: 2027-07-08 (current time + 2 years)
//
// someDate := time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)
// result := gotime.Years(5, someDate)
// // result: 2025-01-01
func Years(years int, dt ...time.Time) time.Time {
var t time.Time
if len(dt) > 0 {
t = dt[0]
} else {
t = time.Now()
}
if years == 0 {
return t
}
return t.AddDate(years, 0, 0)
}
// LastYear returns the date one year ago from the current time.
//
// Example:
//
// lastYear := gotime.LastYear()
// // lastYear: 2024-07-08 (if current time is 2025-07-08)
func LastYear() time.Time {
return time.Now().AddDate(-1, 0, 0)
}
// NextYear returns the date one year from the current time.
//
// Example:
//
// nextYear := gotime.NextYear()
// // nextYear: 2026-07-08 (if current time is 2025-07-08)
func NextYear() time.Time {
return time.Now().AddDate(1, 0, 0)
}
//-----------------Month Functions-----------------
// MonthStart returns the first day of the month for the given date.
// If no date is provided, it uses the current time.
//
// Example:
//
// start := gotime.MonthStart() // First day of current month
// // start: 2025-07-01 00:00:00
//
// someDate := time.Date(2024, 6, 15, 0, 0, 0, 0, time.UTC)
// start = gotime.MonthStart(someDate)
// // start: 2024-06-01 00:00:00
func MonthStart(dt ...time.Time) time.Time {
var t time.Time
if len(dt) > 0 {
t = dt[0]
} else {
t = time.Now()
}
start := time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, t.Location())
return start
}
// MonthEnd returns the last day and last second of the month for the given date.
// If no date is provided, it uses the current time.
//
// Example:
//
// end := gotime.MonthEnd() // Last day of current month
// // end: 2025-07-31 23:59:59.999999999
//
// someDate := time.Date(2024, 2, 15, 0, 0, 0, 0, time.UTC)
// end = gotime.MonthEnd(someDate)
// // end: 2024-02-29 23:59:59.999999999 (leap year)
func MonthEnd(dt ...time.Time) time.Time {
var t time.Time
if len(dt) > 0 {
t = dt[0]
} else {
t = time.Now()
}
end := time.Date(t.Year(), t.Month()+1, 0, 23, 59, 59, 999999999, t.Location())
return end
}
// LastMonth returns the date one month ago from the current time.
//
// Example:
//
// lastMonth := gotime.LastMonth()
// // lastMonth: 2025-06-08 (if current time is 2025-07-08)
func LastMonth() time.Time {
return time.Now().AddDate(0, -1, 0)
}
// NextMonth returns the date one month from the current time.
//
// Example:
//
// nextMonth := gotime.NextMonth()
// // nextMonth: 2025-08-08 (if current time is 2025-07-08)
func NextMonth() time.Time {
return time.Now().AddDate(0, 1, 0)
}
// Months returns the date after adding the specified number of months to the given date.
// If no date is provided, it uses the current time.
// If months is 0, it returns the original date unchanged.
//
// Example:
//
// future := gotime.Months(3) // 3 months from now
// // future: 2025-10-08 (current time + 3 months)
//
// someDate := time.Date(2020, 1, 31, 0, 0, 0, 0, time.UTC)
// result := gotime.Months(1, someDate)
// // result: 2020-02-29 (handles month-end edge cases)
func Months(months int, dt ...time.Time) time.Time {
var t time.Time
if len(dt) > 0 {
t = dt[0]
} else {
t = time.Now()
}
if months == 0 {
return t
}
return t.AddDate(0, months, 0)
}
//-----------------Week Functions-----------------
// WeekStart returns the first day of the week (Sunday) for the given date.
// If no date is provided, it uses the current time.
//
// Example:
//
// start := gotime.WeekStart() // Sunday of current week
// // start: 2025-07-06 00:00:00 (if current is 2025-07-08)
//
// someDate := time.Date(2025, 7, 10, 15, 30, 0, 0, time.UTC) // Thursday
// start = gotime.WeekStart(someDate)
// // start: 2025-07-06 00:00:00 (Sunday of that week)
func WeekStart(dt ...time.Time) time.Time {
var t time.Time
if len(dt) > 0 {
t = dt[0]
} else {
t = time.Now()
}
start := t.AddDate(0, 0, -int(t.Weekday()))
return time.Date(start.Year(), start.Month(), start.Day(), 0, 0, 0, 0, start.Location())
}
// WeekStartOn returns the first occurrence of the specified weekday for the given date's week.
// If no date is provided, it uses the current time.
//
// Example:
//
// start := gotime.WeekStartOn(time.Monday) // Monday of current week
// // start: 2025-07-07 00:00:00 (if current is 2025-07-08)
//
// someDate := time.Date(2025, 7, 10, 0, 0, 0, 0, time.UTC) // Thursday
// start = WeekStartOn(time.Wednesday, someDate)
// // start: 2025-07-09 00:00:00 (Wednesday of that week)
func WeekStartOn(day time.Weekday, dt ...time.Time) time.Time {
var t time.Time
if len(dt) > 0 {
t = dt[0]
} else {
t = time.Now()
}
start := t.AddDate(0, 0, -int(t.Weekday())+int(day))
return time.Date(start.Year(), start.Month(), start.Day(), 0, 0, 0, 0, start.Location())
}
// WeekEnd returns the last day and the last second of the week.
//
// # Arguments
//
// dt: (time.Time) The date to be used to calculate the last day of the week.
//
// # Note
//
// If the date is not provided, it will return the last day of the week from the current date.
func WeekEnd(dt ...time.Time) time.Time {
var t time.Time
if len(dt) > 0 {
t = dt[0]
} else {
t = time.Now()
}
end := t.AddDate(0, 0, 6-int(t.Weekday()))
return time.Date(end.Year(), end.Month(), end.Day(), 23, 59, 59, 999999999, end.Location())
}
// WeekEndOn returns the last day and the last second of the week on the given day.
// For example, WeekEndOn(time.Sunday) returns the last day of the week (Sunday).
//
// # Arguments
//
// day: (time.Weekday) The day to be used to calculate the last day of the week.
//
// dt: (time.Time) The date to be used to calculate the last day of the week.
//
// # Note
//
// If the date is not provided, it will return the last day of the week from the current date.
func WeekEndOn(day time.Weekday, dt ...time.Time) time.Time {
var t time.Time
if len(dt) > 0 {
t = dt[0]
} else {
t = time.Now()
}
end := t.AddDate(0, 0, 6-int(t.Weekday())+int(day))
return time.Date(end.Year(), end.Month(), end.Day(), 23, 59, 59, 999999999, end.Location())
}
// LastWeek returns the last week's time.Time corresponding to the current time.
func LastWeek() time.Time {
return time.Now().AddDate(0, 0, -7)
}
// NextWeek returns the next week's time.Time corresponding to the current time.
func NextWeek() time.Time {
return time.Now().AddDate(0, 0, 7)
}
// Weeks returns the date of the given number of weeks from the current date.
// If the value is negative, it will return the date of the previous week.
//
// # Arguments
//
// weeks: (int) The number of weeks to be added to the date.
//
// dt: (time.Time) The date to be used to calculate the date of the given number of weeks. (Only takes the first date if multiple dates are provided)
//
// # Note
//
// If the weeks parameter is 0 it will panic.
func Weeks(weeks int, dt ...time.Time) time.Time {
var t time.Time
if len(dt) > 0 {
t = dt[0]
} else {
t = time.Now()
}
if weeks == 0 {
return t
}
return t.AddDate(0, 0, weeks*7)
}
//-----------------Day Functions-----------------
// EoD returns the end of the day for the given time.
//
// It calculates the start of the day for the given time using the SoD function, and adds 24 hours to it.
// To get the end of the day, it subtracts one nanosecond from the resulting time.Time value,
// since the SoD function returns the first nanosecond of the day.
//
// Example:
//
// t := time.Date(2022, time.December, 30, 16, 30, 0, 0, time.UTC)
// endOfDay := gotime.EoD(t)
// // endOfDay == time.Date(2022, time.December, 30, 23, 59, 59, 999999999, time.UTC)
func EoD(t ...time.Time) time.Time {
var dt time.Time
if len(t) > 0 {
dt = t[0]
} else {
dt = time.Now()
}
return time.Date(
dt.Year(), dt.Month(), dt.Day(),
23, 59, 59, 999999999,
dt.Location(),
)
}
// SoD returns the start of the day for the given time.
//
// It constructs a new time.Time value using the year, month, and day of the given time.Time value,
// and setting the hour, minute, second, and nanosecond fields to 0. It also sets the location field to the
// same location as the input time.Time value. The resulting time.Time value represents the start of the day
// for the given time.
//
// Example:
//
// t := time.Date(2022, time.December, 30, 16, 30, 0, 0, time.UTC)
// startOfDay := gotime.SoD(t)
// // startOfDay == time.Date(2022, time.December, 30, 0, 0, 0, 0, time.UTC)
func SoD(t ...time.Time) time.Time {
if len(t) > 0 {
return time.Date(t[0].Year(), t[0].Month(), t[0].Day(), 0, 0, 0, 0, t[0].Location())
}
dt := time.Now()
return time.Date(dt.Year(), dt.Month(), dt.Day(), 0, 0, 0, 0, dt.Location())
}
// Yesterday returns the yesterday's time.Time corresponding to the current time.
func Yesterday() time.Time {
return time.Now().AddDate(0, 0, -1)
}
// Tomorrow returns the tomorrow's time.Time corresponding to the current time.
func Tomorrow() time.Time {
return time.Now().AddDate(0, 0, 1)
}
// Days returns the date of the given number of days from the date provided,
//
// # Arguments
//
// days: (int) The number of days to be added to the date.
//
// dt: (time.Time) The date to be used to calculate the date of the given number of days. (Only takes the first date if multiple dates are provided)
//
// # Note
//
// If the days parameter is 0 it will panic.
func Days(days int, dt ...time.Time) time.Time {
var t time.Time
if len(dt) > 0 {
t = dt[0]
} else {
t = time.Now()
}
if days == 0 {
return t
}
return t.AddDate(0, 0, days)
}