github.com/go-playground/pkg/v5@v5.29.1/time/instant.go (about)

     1  //go:build go1.18
     2  // +build go1.18
     3  
     4  package timeext
     5  
     6  import "time"
     7  
     8  // Instant represents a monotonic instant in time.
     9  //
    10  // Instants are opaque types that can only be compared with one another and allows measuring of duration.
    11  type Instant int64
    12  
    13  // NewInstant returns a new Instant.
    14  func NewInstant() Instant {
    15  	return Instant(NanoTime())
    16  }
    17  
    18  // Elapsed returns the duration since the instant was created.
    19  func (i Instant) Elapsed() time.Duration {
    20  	return time.Duration(NewInstant() - i)
    21  }
    22  
    23  // Since returns the duration elapsed from another Instant, or zero is that Instant is later than this one.
    24  func (i Instant) Since(instant Instant) time.Duration {
    25  	if instant > i {
    26  		return 0
    27  	}
    28  	return time.Duration(i - instant)
    29  }