github.com/billybanfield/evergreen@v0.0.0-20170525200750-eeee692790f7/util/time.go (about) 1 package util 2 3 import ( 4 "time" 5 ) 6 7 // ZeroTime represents 0 in epoch time 8 var ZeroTime time.Time = time.Unix(0, 0) 9 10 // IsZeroTime checks that a time is either equal to golang ZeroTime or 11 // UTC ZeroTime. 12 func IsZeroTime(t time.Time) bool { 13 return t.Equal(ZeroTime) || t.IsZero() 14 } 15 16 // fromNanoSeconds returns milliseconds of a duration for queries in the database. 17 func FromNanoseconds(duration time.Duration) int64 { 18 return int64(duration) / 1000000 19 } 20 21 // fromNanoSeconds returns milliseconds of a duration for queries in the database. 22 func ToNanoseconds(duration time.Duration) time.Duration { 23 return duration * 1000000 24 } 25 26 // FromPythonTime returns a time.Time that corresponds to the float style 27 // python time which is <seconds>.<fractional_seconds> from unix epoch. 28 func FromPythonTime(pyTime float64) time.Time { 29 sec := int64(pyTime) 30 toNano := int64(1000000000) 31 asNano := int64(pyTime * float64(toNano)) 32 nano := asNano % toNano 33 res := time.Unix(sec, nano) 34 return res 35 } 36 37 // ToPythonTime returns a number in the format that python's time.time() returns 38 // as a float with <seconds>.<fractional_seconds> 39 func ToPythonTime(t time.Time) float64 { 40 if IsZeroTime(t) { 41 return float64(0) 42 } 43 timeAsInt64 := t.UnixNano() 44 fromNano := float64(1000000000) 45 46 res := float64(timeAsInt64) / fromNano 47 return res 48 }