github.com/m3db/m3@v1.5.0/src/x/time/time.go (about) 1 // Copyright (c) 2016 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 // Package time implement facilities for working with time. 22 package time 23 24 import ( 25 "time" 26 ) 27 28 const ( 29 nanosPerMillis = int64(time.Millisecond) 30 ) 31 32 // Now returns the current local time, in nanoseconds elapsed since January 1, 33 // 1970 UTC. NB: This is independent of location. 34 func Now() UnixNano { 35 return ToUnixNano(time.Now()) 36 } 37 38 // Since returns the time elapsed since t. 39 // It is shorthand for time.Now().Sub(t). 40 func Since(t UnixNano) time.Duration { 41 return Now().Sub(t) 42 } 43 44 // ToNormalizedTime returns the normalized units of time given a time unit. 45 func ToNormalizedTime(t time.Time, u time.Duration) int64 { 46 return t.UnixNano() / u.Nanoseconds() 47 } 48 49 // FromNormalizedTime returns the time given the normalized time units and the time unit. 50 func FromNormalizedTime(nt int64, u time.Duration) UnixNano { 51 return UnixNano(int64(u/time.Nanosecond) * nt) 52 } 53 54 // ToNormalizedDuration returns the normalized units of duration given a time unit. 55 func ToNormalizedDuration(d time.Duration, u time.Duration) int64 { 56 return int64(d / u) 57 } 58 59 // FromNormalizedDuration returns the duration given the normalized time duration and a time unit. 60 func FromNormalizedDuration(nd int64, u time.Duration) time.Duration { 61 return time.Duration(nd) * u 62 } 63 64 // ToNanoseconds converts a time to nanoseconds. 65 func ToNanoseconds(t time.Time) int64 { 66 return t.UnixNano() 67 } 68 69 // FromNanoseconds converts nanoseconds to a time. 70 func FromNanoseconds(nsecs int64) time.Time { 71 return time.Unix(0, nsecs) 72 } 73 74 // ToUnixMillis converts a time to milliseconds since Unix epoch 75 func ToUnixMillis(t time.Time) int64 { 76 return t.UnixNano() / nanosPerMillis 77 } 78 79 // FromUnixMillis converts milliseconds since Unix epoch to a time 80 func FromUnixMillis(ms int64) time.Time { 81 return time.Unix(0, ms*nanosPerMillis) 82 } 83 84 // Ceil returns the result of rounding t up to a multiple of d since 85 // the zero time. 86 func Ceil(t time.Time, d time.Duration) time.Time { 87 res := t.Truncate(d) 88 if res.Before(t) { 89 res = res.Add(d) 90 } 91 return res 92 } 93 94 // MinTime returns the earlier one of t1 and t2. 95 func MinTime(t1, t2 time.Time) time.Time { 96 if t1.Before(t2) { 97 return t1 98 } 99 return t2 100 } 101 102 // MinUnixNano returns the earlier one of t1 and t2. 103 func MinUnixNano(t1, t2 UnixNano) UnixNano { 104 if t1.Before(t2) { 105 return t1 106 } 107 return t2 108 } 109 110 // MaxTime returns the later one of t1 and t2. 111 func MaxTime(t1, t2 time.Time) time.Time { 112 if t1.After(t2) { 113 return t1 114 } 115 return t2 116 } 117 118 // MaxUnixNano returns the later one of t1 and t2. 119 func MaxUnixNano(t1, t2 UnixNano) UnixNano { 120 if t1.After(t2) { 121 return t1 122 } 123 return t2 124 }