-
Notifications
You must be signed in to change notification settings - Fork 1
/
dtime.go
499 lines (439 loc) · 15 KB
/
dtime.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
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
// Package dtime enables easy human input of times, dates, and durations. It
// also includes many convenience functions for rouding time duration
// boundaries as is frequently needed for scheduling and time-based search
// applications.
//
// Pointers to time.Time are used throughout the package since <nil> is usually
// a more desirable zero value than that for time.Time without a pointer. */
package dtime
import (
"fmt"
"time"
)
const (
SECOND float64 = 1000000000
MINUTE float64 = 60000000000
HOUR float64 = 3600000000000
DAY float64 = 86400000000000
WEEK float64 = 604800000000000
YEAR float64 = 31536000000000000
)
// DefaultTime can be set instead of time.Now().
var DefaultTime *time.Time
func _deftime() *time.Time {
if DefaultTime == nil {
now := time.Now()
return &now
}
return DefaultTime
}
func dump(i interface{}) {
fmt.Printf("%v\n", i)
}
// Span parses the htime format string and returns one or two time pointers.
// The first is the start time, the second is the last time. If a date and time
// are detected in the string the first is set. If the offset is detected in
// the string the second will be set. If no date and time are detected
// DefaultTime or time.Now() is assumed.
//
// Spans the main function in this package and provides a minimal format for
// entering date and time information in a practical way. It's primary use case
// is when a user needs to enter such data quickly and regularly from the
// command line or into mobile and other devices where human input speed is
// limited to what can be tapped out on the screen. The characters used in the
// formatting only characters that appear on most default keyboards without
// shifting or switching to symbolic input. The package provides no method of
// specifying timezone, which falls out of the scope of this package. All times
// are therefore assumed to be local. For a precise specification of the format
// see the htime.peg file included with the package source code.
func Span(s string) (first *time.Time, last *time.Time) {
p := new(spanParser)
p.Buffer = s
p.Init()
p.Parse()
// p.Execute() // no need with -inline
if !p.start.IsZero() {
first = &p.start
}
if p.offset != 0 {
t := p.start.Add(time.Duration(int64(p.offset)))
last = &t
}
if !p.stop.IsZero() {
last = &p.stop
}
return
}
// MinuteOf returns the start of the given minute.
func MinuteOf(t *time.Time) *time.Time {
nt := time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), 0, 0, t.Location())
return &nt
}
// HourOf returns the start of the given hour.
func HourOf(t *time.Time) *time.Time {
nt := time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), 0, 0, 0, t.Location())
return &nt
}
// NextHourOf returns the start of the given hour.
func NextHourOf(t *time.Time) *time.Time {
nt := time.Date(t.Year(), t.Month(), t.Day(), t.Hour()+1, 0, 0, 0, t.Location())
return &nt
}
// DayOf returns the start of the given day.
func DayOf(t *time.Time) *time.Time {
nt := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
return &nt
}
// WeekOf returns the start of the given week.
func WeekOf(t *time.Time) *time.Time {
return MondayOf(t)
}
// MonthOf returns the start of the month.
func MonthOf(t *time.Time) *time.Time {
nt := time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, t.Location())
return &nt
}
// YearOf returns the start of the month.
func YearOf(t *time.Time) *time.Time {
nt := time.Date(t.Year(), 1, 1, 0, 0, 0, 0, t.Location())
return &nt
}
// Today returns the start of the current date.
func Today() *time.Time {
return DayOf(_deftime())
}
// Tomorrow returns the start of the next day.
func Tomorrow() *time.Time {
t := *_deftime()
t = t.Add(time.Hour * 24)
return DayOf(&t)
}
// Yesterday returns the start of the previous day.
func Yesterday() *time.Time {
t := *_deftime()
t = t.Add(time.Hour * -24)
return DayOf(&t)
}
// JanuaryOf returns the beginning of the first day of January of the given
// year.
func JanuaryOf(t *time.Time) *time.Time {
return MonthOf(SameTimeInJanuaryOf(t))
}
// FebruaryOf returns the beginning of the first day of February of the given
// year.
func FebruaryOf(t *time.Time) *time.Time {
return MonthOf(SameTimeInFebruaryOf(t))
}
// MarchOf returns the beginning of the first day of March of the given
// year.
func MarchOf(t *time.Time) *time.Time {
return MonthOf(SameTimeInMarchOf(t))
}
// AprilOf returns the beginning of the first day of April of the given
// year.
func AprilOf(t *time.Time) *time.Time {
return MonthOf(SameTimeInAprilOf(t))
}
// MayOf returns the beginning of the first day of May of the given
// year.
func MayOf(t *time.Time) *time.Time {
return MonthOf(SameTimeInMayOf(t))
}
// JuneOf returns the beginning of the first day of June of the given
// year.
func JuneOf(t *time.Time) *time.Time {
return MonthOf(SameTimeInJuneOf(t))
}
// JulyOf returns the beginning of the first day of July of the given
// year.
func JulyOf(t *time.Time) *time.Time {
return MonthOf(SameTimeInJulyOf(t))
}
// AugustOf returns the beginning of the first day of August of the given
// year.
func AugustOf(t *time.Time) *time.Time {
return MonthOf(SameTimeInAugustOf(t))
}
// SeptemberOf returns the beginning of the first day of September of the given
// year.
func SeptemberOf(t *time.Time) *time.Time {
return MonthOf(SameTimeInSeptemberOf(t))
}
// OctoberOf returns the beginning of the first day of October of the given
// year.
func OctoberOf(t *time.Time) *time.Time {
return MonthOf(SameTimeInOctoberOf(t))
}
// NovemberOf returns the beginning of the first day of November of the given
// year.
func NovemberOf(t *time.Time) *time.Time {
return MonthOf(SameTimeInNovemberOf(t))
}
// DecemberOf returns the beginning of the first day of December of the given
// year.
func DecemberOf(t *time.Time) *time.Time {
return MonthOf(SameTimeInDecemberOf(t))
}
func samemonth(t *time.Time, month int) *time.Time {
d := time.Date(
t.Year(),
time.Month(month),
t.Day(),
t.Hour(),
t.Minute(),
t.Second(),
t.Nanosecond(),
t.Location(),
)
return &d
}
// SameTimeInJanuaryOf returns the exact same month day and time but for the
// month of January instead.
func SameTimeInJanuaryOf(t *time.Time) *time.Time {
return samemonth(t, 1)
}
// SameTimeInFebruaryOf returns the exact same month day and time but for the
// month of February instead.
func SameTimeInFebruaryOf(t *time.Time) *time.Time {
return samemonth(t, 2)
}
// SameTimeInMarchOf returns the exact same month day and time but for the
// month of March instead.
func SameTimeInMarchOf(t *time.Time) *time.Time {
return samemonth(t, 3)
}
// SameTimeInAprilOf returns the exact same month day and time but for the
// month of April instead.
func SameTimeInAprilOf(t *time.Time) *time.Time {
return samemonth(t, 4)
}
// SameTimeInMayOf returns the exact same month day and time but for the
// month of May instead.
func SameTimeInMayOf(t *time.Time) *time.Time {
return samemonth(t, 5)
}
// SameTimeInJuneOf returns the exact same month day and time but for the
// month of June instead.
func SameTimeInJuneOf(t *time.Time) *time.Time {
return samemonth(t, 6)
}
// SameTimeInJulyOf returns the exact same month day and time but for the
// month of July instead.
func SameTimeInJulyOf(t *time.Time) *time.Time {
return samemonth(t, 7)
}
// SameTimeInAugustOf returns the exact same month day and time but for the
// month of August instead.
func SameTimeInAugustOf(t *time.Time) *time.Time {
return samemonth(t, 8)
}
// SameTimeInSeptemberOf returns the exact same month day and time but for the
// month of September instead.
func SameTimeInSeptemberOf(t *time.Time) *time.Time {
return samemonth(t, 9)
}
// SameTimeInOctoberOf returns the exact same month day and time but for the
// month of October instead.
func SameTimeInOctoberOf(t *time.Time) *time.Time {
return samemonth(t, 10)
}
// SameTimeInNovemberOf returns the exact same month day and time but for the
// month of November instead.
func SameTimeInNovemberOf(t *time.Time) *time.Time {
return samemonth(t, 11)
}
// SameTimeInDecemberOf returns the exact same month day and time but for the
// month of December instead.
func SameTimeInDecemberOf(t *time.Time) *time.Time {
return samemonth(t, 12)
}
// MonthOfYear returns the beginning of the first day of the specified month of the
// given year.
func MonthOfYear(t *time.Time, month string) *time.Time {
switch month {
case "jan", "january", "Jan", "January":
return JanuaryOf(t)
case "feb", "Feb", "february", "February":
return FebruaryOf(t)
case "mar", "Mar", "march", "March":
return MarchOf(t)
case "apr", "Apr", "april", "April":
return AprilOf(t)
case "may", "May":
return MayOf(t)
case "jun", "Jun", "june", "June":
return JuneOf(t)
case "jul", "Jul", "july", "July":
return JulyOf(t)
case "aug", "Aug", "august", "August":
return AugustOf(t)
case "sep", "Sep", "september", "September":
return SeptemberOf(t)
case "oct", "Oct", "october", "October":
return OctoberOf(t)
case "nov", "Nov", "november", "November":
return NovemberOf(t)
case "dec", "Dec", "december", "December":
return DecemberOf(t)
}
return nil
}
// SameTimeInMonthOfYear returns the exact same month day and time but for the
// specified month instead.
func SameTimeInMonthOfYear(t *time.Time, month string) *time.Time {
switch month {
case "jan", "Jan", "january", "January":
return SameTimeInJanuaryOf(t)
case "feb", "Feb", "february", "February":
return SameTimeInFebruaryOf(t)
case "mar", "Mar", "march", "March":
return SameTimeInMarchOf(t)
case "apr", "Apr", "april", "April":
return SameTimeInAprilOf(t)
case "may", "May":
return SameTimeInMayOf(t)
case "jun", "Jun", "june", "June":
return SameTimeInJuneOf(t)
case "jul", "Jul", "july", "July":
return SameTimeInJulyOf(t)
case "aug", "Aug", "august", "August":
return SameTimeInAugustOf(t)
case "sep", "Sep", "september", "September":
return SameTimeInSeptemberOf(t)
case "oct", "Oct", "october", "October":
return SameTimeInOctoberOf(t)
case "nov", "Nov", "november", "November":
return SameTimeInNovemberOf(t)
case "dec", "Dec", "december", "December":
return SameTimeInDecemberOf(t)
}
return nil
}
// MondayOf returns the Monday of the week passed as time rounded to the
// beginning of the day.
func MondayOf(t *time.Time) *time.Time {
return DayOf(SameTimeOnMondayOf(t))
}
// TuesdayOf returns the Tuesday of the week passed as time rounded to the
// beginning of the day.
func TuesdayOf(t *time.Time) *time.Time {
return DayOf(SameTimeOnTuesdayOf(t))
}
// WednesdayOf returns the Wednesday of the week passed as time rounded to the
// beginning of the day.
func WednesdayOf(t *time.Time) *time.Time {
return DayOf(SameTimeOnWednesdayOf(t))
}
// ThursdayOf returns the Thursday of the week passed as time rounded to the
// beginning of the day.
func ThursdayOf(t *time.Time) *time.Time {
return DayOf(SameTimeOnThursdayOf(t))
}
// FridayOf returns the Friday of the week passed as time rounded to the
// beginning of the day.
func FridayOf(t *time.Time) *time.Time {
return DayOf(SameTimeOnFridayOf(t))
}
// SaturdayOf returns the Saturday of the week passed as time rounded to the
// beginning of the day.
func SaturdayOf(t *time.Time) *time.Time {
return DayOf(SameTimeOnSaturdayOf(t))
}
// SundayOf returns the Sunday of the week passed as time rounded to the
// beginning of the day.
func SundayOf(t *time.Time) *time.Time {
return DayOf(SameTimeOnSundayOf(t))
}
// SameTimeOnMondayOf returns the exact same time but on the Monday of the week
// indicated. For beginning of day use without SameTimeOn.
func SameTimeOnMondayOf(t *time.Time) *time.Time {
nt := t.Add((time.Duration(time.Monday-t.Weekday()) * 24 * time.Hour))
return &nt
}
// SameTimeOnTuesdayOf returns the exact same time but on the Tuesday of the week
// indicated. For beginning of day use without SameTimeOn.
func SameTimeOnTuesdayOf(t *time.Time) *time.Time {
nt := t.Add((time.Duration(time.Tuesday-t.Weekday()) * 24 * time.Hour))
return &nt
}
// SameTimeOnWednesdayOf returns the exact same time but on the Wednesday of the week
// indicated. For beginning of day use without SameTimeOn.
func SameTimeOnWednesdayOf(t *time.Time) *time.Time {
nt := t.Add((time.Duration(time.Wednesday-t.Weekday()) * 24 * time.Hour))
return &nt
}
// SameTimeOnThursdayOf returns the exact same time but on the Thursday of the week
// indicated. For beginning of day use without SameTimeOn.
func SameTimeOnThursdayOf(t *time.Time) *time.Time {
nt := t.Add((time.Duration(time.Thursday-t.Weekday()) * 24 * time.Hour))
return &nt
}
// SameTimeOnFridayOf returns the exact same time but on the Friday of the week
// indicated. For beginning of day use without SameTimeOn.
func SameTimeOnFridayOf(t *time.Time) *time.Time {
nt := t.Add((time.Duration(time.Friday-t.Weekday()) * 24 * time.Hour))
return &nt
}
// SameTimeOnSaturdayOf returns the exact same time but on the Saturday of the week
// indicated. For beginning of day use without SameTimeOn.
func SameTimeOnSaturdayOf(t *time.Time) *time.Time {
nt := t.Add((time.Duration(time.Saturday-t.Weekday()) * 24 * time.Hour))
return &nt
}
// SameTimeOnSundayOf returns the exact same time but on the Sunday of the week
// indicated. For beginning of day use without SameTimeOn.
func SameTimeOnSundayOf(t *time.Time) *time.Time {
nt := t.Add((time.Duration(time.Sunday-t.Weekday()+7) * 24 * time.Hour))
return &nt
}
// DayOfWeek returns the day of the week passed rounded to the beginning of the
// weekday indicated.
func DayOfWeek(t *time.Time, day string) *time.Time {
switch day {
case "mon", "monday", "Mon", "Monday":
return MondayOf(t)
case "tue", "tuesday", "Tue", "Tuesday":
return TuesdayOf(t)
case "wed", "wednesday", "Wed", "Wednesday":
return WednesdayOf(t)
case "thu", "thursday", "Thu", "Thursday":
return ThursdayOf(t)
case "fri", "friday", "Fri", "Friday":
return FridayOf(t)
case "sat", "saturday", "Sat", "Saturday":
return SaturdayOf(t)
case "sun", "sunday", "Sun", "Sunday":
return SundayOf(t)
}
return nil
}
// SameTimeOnDayOfWeek returns the day of the week passed rounded to the
// beginning of the week day indicated.
func SameTimeOnDayOfWeek(t *time.Time, day string) *time.Time {
switch day {
case "mon", "monday", "Mon", "Monday":
return SameTimeOnMondayOf(t)
case "tue", "tuesday", "Tue", "Tuesday":
return SameTimeOnTuesdayOf(t)
case "wed", "wednesday", "Wed", "Wednesday":
return SameTimeOnWednesdayOf(t)
case "thu", "thursday", "Thu", "Thursday":
return SameTimeOnThursdayOf(t)
case "fri", "friday", "Fri", "Friday":
return SameTimeOnFridayOf(t)
case "sat", "saturday", "Sat", "Saturday":
return SameTimeOnSaturdayOf(t)
case "sun", "sunday", "Sun", "Sunday":
return SameTimeOnSundayOf(t)
}
return nil
}
// Until takes a time and a function that takes a time and returns a time and
// converts the result into a duration offset between them. Returns nil
// if unable to determine. This is useful when a duration is needed for
// any of the functions in this package that match that signature. The
// duration is positive or negative depending on the relation between
// the times.
func Until(fn func(*time.Time) *time.Time, t *time.Time) time.Duration {
return (*(fn(t))).Sub(*t)
}