github.com/binkynet/BinkyNet@v1.12.1-0.20240421190447-da4e34c20be0/proto_vendor/golang.org/x/sys/unix/timestruct.go (about) 1 // Copyright 2017 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // +build aix darwin dragonfly freebsd linux netbsd openbsd solaris 6 7 package unix 8 9 import "time" 10 11 // TimespecToNsec converts a Timespec value into a number of 12 // nanoseconds since the Unix epoch. 13 func TimespecToNsec(ts Timespec) int64 { return int64(ts.Sec)*1e9 + int64(ts.Nsec) } 14 15 // NsecToTimespec takes a number of nanoseconds since the Unix epoch 16 // and returns the corresponding Timespec value. 17 func NsecToTimespec(nsec int64) Timespec { 18 sec := nsec / 1e9 19 nsec = nsec % 1e9 20 if nsec < 0 { 21 nsec += 1e9 22 sec-- 23 } 24 return setTimespec(sec, nsec) 25 } 26 27 // TimeToTimespec converts t into a Timespec. 28 // On some 32-bit systems the range of valid Timespec values are smaller 29 // than that of time.Time values. So if t is out of the valid range of 30 // Timespec, it returns a zero Timespec and ERANGE. 31 func TimeToTimespec(t time.Time) (Timespec, error) { 32 sec := t.Unix() 33 nsec := int64(t.Nanosecond()) 34 ts := setTimespec(sec, nsec) 35 36 // Currently all targets have either int32 or int64 for Timespec.Sec. 37 // If there were a new target with floating point type for it, we have 38 // to consider the rounding error. 39 if int64(ts.Sec) != sec { 40 return Timespec{}, ERANGE 41 } 42 return ts, nil 43 } 44 45 // TimevalToNsec converts a Timeval value into a number of nanoseconds 46 // since the Unix epoch. 47 func TimevalToNsec(tv Timeval) int64 { return int64(tv.Sec)*1e9 + int64(tv.Usec)*1e3 } 48 49 // NsecToTimeval takes a number of nanoseconds since the Unix epoch 50 // and returns the corresponding Timeval value. 51 func NsecToTimeval(nsec int64) Timeval { 52 nsec += 999 // round up to microsecond 53 usec := nsec % 1e9 / 1e3 54 sec := nsec / 1e9 55 if usec < 0 { 56 usec += 1e6 57 sec-- 58 } 59 return setTimeval(sec, usec) 60 } 61 62 // Unix returns ts as the number of seconds and nanoseconds elapsed since the 63 // Unix epoch. 64 func (ts *Timespec) Unix() (sec int64, nsec int64) { 65 return int64(ts.Sec), int64(ts.Nsec) 66 } 67 68 // Unix returns tv as the number of seconds and nanoseconds elapsed since the 69 // Unix epoch. 70 func (tv *Timeval) Unix() (sec int64, nsec int64) { 71 return int64(tv.Sec), int64(tv.Usec) * 1000 72 } 73 74 // Nano returns ts as the number of nanoseconds elapsed since the Unix epoch. 75 func (ts *Timespec) Nano() int64 { 76 return int64(ts.Sec)*1e9 + int64(ts.Nsec) 77 } 78 79 // Nano returns tv as the number of nanoseconds elapsed since the Unix epoch. 80 func (tv *Timeval) Nano() int64 { 81 return int64(tv.Sec)*1e9 + int64(tv.Usec)*1000 82 }