-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtill_next.go
125 lines (105 loc) · 4.32 KB
/
till_next.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
// Copyright © 2020. All rights reserved.
// Author: Ilya Stroy.
// Contacts: [email protected], https://github.com/qioalice
// License: https://opensource.org/licenses/MIT
package ekatime
import (
"time"
)
// TillNext returns how much ns (as time.Duration) must be passed until next time
// 'range_' will end for the current Timestamp 'ts'.
//
// In most cases you don't need to use this method, but use any of predefined
// instead: TillNextMinute(), TillNextHour(), etc.
//
// Using this function you may get how much time.Duration must be passed,
// until next requested time is passed since now. Range must be passed in seconds.
//
// Examples:
// d := NewDate(2012, MONTH_JANUARY, 12).WithTime(13, 14, 15) // -> 12 Jan 2012 13:14:15
// d.TillNext(2 * SECONDS_IN_HOUR) // -> 45m45s (till 14:00:00, range of 2h)
// d.TillNext(3 * SECONDS_IN_HOUR) // -> 1h45m45s (till 15:00:00, range of 3h)
// d.TillNext(30 * SECONDS_IN_MINUTE) // -> 15m45s (till 13:30:00, range of 30m).
func (ts Timestamp) TillNext(range_ Timestamp) time.Duration {
return time.Duration(ts.tillNext(range_)) * time.Second
}
// TillNextMinute returns how much ns (as time.Duration) must be passed until
// next minute (for the current Timestamp 'ts') will came.
func (ts Timestamp) TillNextMinute() time.Duration {
return ts.TillNext(SECONDS_IN_MINUTE)
}
// TillNextHour returns how much ns (as time.Duration) must be passed until
// next hour (for the current Timestamp 'ts') will came.
func (ts Timestamp) TillNextHour() time.Duration {
return ts.TillNext(SECONDS_IN_HOUR)
}
// TillNext12h returns how much ns (as time.Duration) must be passed until
// next half day (12h) (for the current Timestamp 'ts') will came.
func (ts Timestamp) TillNext12h() time.Duration {
return ts.TillNext(SECONDS_IN_12H)
}
// TillNextNoon returns how much ns (as time.Duration) must be passed until
// next noon (12.00 PM) (for the current Timestamp 'ts') will came.
func (ts Timestamp) TillNextNoon() time.Duration {
d := ts.TillNext(SECONDS_IN_DAY) + 12*time.Hour
if d >= 24*time.Hour {
d -= 24 * time.Hour
}
return d
}
// TillNextMidnight returns how much ns (as time.Duration) must be passed until
// next midnight (12.00 AM) (for the current Timestamp 'ts') will came.
func (ts Timestamp) TillNextMidnight() time.Duration {
return ts.TillNextDay()
}
// TillNextDay returns how much ns (as time.Duration) must be passed until
// next day (for the current Timestamp 'ts') will came.
func (ts Timestamp) TillNextDay() time.Duration {
return ts.TillNext(SECONDS_IN_DAY)
}
// TillNextMonth returns how much ns (as time.Duration) must be passed until
// next month (for the current Timestamp 'ts') will came.
func (ts Timestamp) TillNextMonth() time.Duration {
y, m, _ := dateFromUnix(ts)
return ts.TillNext(InMonth(y, m))
}
// TillNextYear returns how much ns (as time.Duration) must be passed until
// next year (for the current Timestamp 'ts') will came.
func (ts Timestamp) TillNextYear() time.Duration {
return ts.TillNext(InYear(ts.Year()))
}
// TillNextMinute is the same as Timestamp.TillNextMinute() but for current time.
func TillNextMinute() time.Duration {
return NewTimestampNow().TillNextMinute()
}
// TillNextHour is the same as Timestamp.TillNextHour() but for current time.
func TillNextHour() time.Duration {
return NewTimestampNow().TillNextHour()
}
// TillNext12h is the same as Timestamp.TillNext12h() but for current time.
func TillNext12h() time.Duration {
return NewTimestampNow().TillNext12h()
}
// TillNextNoon is the same as Timestamp.TillNextNoon() but for current time.
func TillNextNoon() time.Duration {
return NewTimestampNow().TillNextNoon()
}
// TillNextMidnight is the same as Timestamp.TillNextMidnight() but for current time.
func TillNextMidnight() time.Duration {
return NewTimestampNow().TillNextMidnight()
}
// TillNextDay is the same as Timestamp.TillNextDay() but for current time.
func TillNextDay() time.Duration {
return NewTimestampNow().TillNextDay()
}
// TillNextMonth is the same as Timestamp.TillNextMonth() but for current time.
func TillNextMonth() time.Duration {
return NewTimestampNow().TillNextMonth()
}
// TillNextYear is the same as Timestamp.TillNextYear() but for current time.
func TillNextYear() time.Duration {
return NewTimestampNow().TillNextYear()
}
func (ts Timestamp) tillNext(range_ Timestamp) Timestamp {
return ts + (range_ - ts%range_) - ts
}