github.com/qioalice/ekago/v3@v3.3.2-0.20221202205325-5c262d586ee4/ekatime/till_next.go (about) 1 // Copyright © 2020. All rights reserved. 2 // Author: Ilya Stroy. 3 // Contacts: iyuryevich@pm.me, https://github.com/qioalice 4 // License: https://opensource.org/licenses/MIT 5 6 package ekatime 7 8 import ( 9 "time" 10 ) 11 12 // TillNext returns how much ns (as time.Duration) must be passed until next time 13 // 'range_' will end for the current Timestamp 'ts'. 14 // 15 // In most cases you don't need to use this method, but use any of predefined 16 // instead: TillNextMinute(), TillNextHour(), etc. 17 // 18 // Using this function you may get how much time.Duration must be passed, 19 // until next requested time is passed since now. Range must be passed in seconds. 20 // 21 // Examples: 22 // d := NewDate(2012, MONTH_JANUARY, 12).WithTime(13, 14, 15) // -> 12 Jan 2012 13:14:15 23 // d.TillNext(2 * SECONDS_IN_HOUR) // -> 45m45s (till 14:00:00, range of 2h) 24 // d.TillNext(3 * SECONDS_IN_HOUR) // -> 1h45m45s (till 15:00:00, range of 3h) 25 // d.TillNext(30 * SECONDS_IN_MINUTE) // -> 15m45s (till 13:30:00, range of 30m). 26 func (ts Timestamp) TillNext(range_ Timestamp) time.Duration { 27 return time.Duration(ts.tillNext(range_)) * time.Second 28 } 29 30 // TillNextMinute returns how much ns (as time.Duration) must be passed until 31 // next minute (for the current Timestamp 'ts') will came. 32 func (ts Timestamp) TillNextMinute() time.Duration { 33 return ts.TillNext(SECONDS_IN_MINUTE) 34 } 35 36 // TillNextHour returns how much ns (as time.Duration) must be passed until 37 // next hour (for the current Timestamp 'ts') will came. 38 func (ts Timestamp) TillNextHour() time.Duration { 39 return ts.TillNext(SECONDS_IN_HOUR) 40 } 41 42 // TillNext12h returns how much ns (as time.Duration) must be passed until 43 // next half day (12h) (for the current Timestamp 'ts') will came. 44 func (ts Timestamp) TillNext12h() time.Duration { 45 return ts.TillNext(SECONDS_IN_12H) 46 } 47 48 // TillNextNoon returns how much ns (as time.Duration) must be passed until 49 // next noon (12.00 PM) (for the current Timestamp 'ts') will came. 50 func (ts Timestamp) TillNextNoon() time.Duration { 51 d := ts.TillNext(SECONDS_IN_DAY) + 12*time.Hour 52 if d >= 24*time.Hour { 53 d -= 24 * time.Hour 54 } 55 return d 56 } 57 58 // TillNextMidnight returns how much ns (as time.Duration) must be passed until 59 // next midnight (12.00 AM) (for the current Timestamp 'ts') will came. 60 func (ts Timestamp) TillNextMidnight() time.Duration { 61 return ts.TillNextDay() 62 } 63 64 // TillNextDay returns how much ns (as time.Duration) must be passed until 65 // next day (for the current Timestamp 'ts') will came. 66 func (ts Timestamp) TillNextDay() time.Duration { 67 return ts.TillNext(SECONDS_IN_DAY) 68 } 69 70 // TillNextMonth returns how much ns (as time.Duration) must be passed until 71 // next month (for the current Timestamp 'ts') will came. 72 func (ts Timestamp) TillNextMonth() time.Duration { 73 y, m, _ := dateFromUnix(ts) 74 return ts.TillNext(InMonth(y, m)) 75 } 76 77 // TillNextYear returns how much ns (as time.Duration) must be passed until 78 // next year (for the current Timestamp 'ts') will came. 79 func (ts Timestamp) TillNextYear() time.Duration { 80 return ts.TillNext(InYear(ts.Year())) 81 } 82 83 // TillNextMinute is the same as Timestamp.TillNextMinute() but for current time. 84 func TillNextMinute() time.Duration { 85 return NewTimestampNow().TillNextMinute() 86 } 87 88 // TillNextHour is the same as Timestamp.TillNextHour() but for current time. 89 func TillNextHour() time.Duration { 90 return NewTimestampNow().TillNextHour() 91 } 92 93 // TillNext12h is the same as Timestamp.TillNext12h() but for current time. 94 func TillNext12h() time.Duration { 95 return NewTimestampNow().TillNext12h() 96 } 97 98 // TillNextNoon is the same as Timestamp.TillNextNoon() but for current time. 99 func TillNextNoon() time.Duration { 100 return NewTimestampNow().TillNextNoon() 101 } 102 103 // TillNextMidnight is the same as Timestamp.TillNextMidnight() but for current time. 104 func TillNextMidnight() time.Duration { 105 return NewTimestampNow().TillNextMidnight() 106 } 107 108 // TillNextDay is the same as Timestamp.TillNextDay() but for current time. 109 func TillNextDay() time.Duration { 110 return NewTimestampNow().TillNextDay() 111 } 112 113 // TillNextMonth is the same as Timestamp.TillNextMonth() but for current time. 114 func TillNextMonth() time.Duration { 115 return NewTimestampNow().TillNextMonth() 116 } 117 118 // TillNextYear is the same as Timestamp.TillNextYear() but for current time. 119 func TillNextYear() time.Duration { 120 return NewTimestampNow().TillNextYear() 121 } 122 123 func (ts Timestamp) tillNext(range_ Timestamp) Timestamp { 124 return ts + (range_ - ts%range_) - ts 125 }