Source file src/syscall/timestruct.go

     1  // Copyright 2016 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  //go:build unix || (js && wasm) || wasip1
     6  
     7  package syscall
     8  
     9  // TimespecToNsec returns the time stored in ts as nanoseconds.
    10  func TimespecToNsec(ts Timespec) int64 { return ts.Nano() }
    11  
    12  // NsecToTimespec converts a number of nanoseconds into a [Timespec].
    13  func NsecToTimespec(nsec int64) Timespec {
    14  	sec := nsec / 1e9
    15  	nsec = nsec % 1e9
    16  	if nsec < 0 {
    17  		nsec += 1e9
    18  		sec--
    19  	}
    20  	return setTimespec(sec, nsec)
    21  }
    22  
    23  // TimevalToNsec returns the time stored in tv as nanoseconds.
    24  func TimevalToNsec(tv Timeval) int64 { return tv.Nano() }
    25  
    26  // NsecToTimeval converts a number of nanoseconds into a [Timeval].
    27  func NsecToTimeval(nsec int64) Timeval {
    28  	nsec += 999 // round up to microsecond
    29  	usec := nsec % 1e9 / 1e3
    30  	sec := nsec / 1e9
    31  	if usec < 0 {
    32  		usec += 1e6
    33  		sec--
    34  	}
    35  	return setTimeval(sec, usec)
    36  }
    37  

View as plain text