gitlab.com/jfprevost/gitlab-runner-notlscheck@v11.11.4+incompatible/common/time.go (about)

     1  // Copyright 2009 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 https://github.com/golang/go/blob/master/LICENSE.
     4  // The original code can be found in https://github.com/golang/go/blob/master/src/time/time.go
     5  // TODO: remove file after upgrading to Go 1.9+
     6  
     7  package common
     8  
     9  import "time"
    10  
    11  const (
    12  	minDuration time.Duration = -1 << 63
    13  	maxDuration time.Duration = 1<<63 - 1
    14  )
    15  
    16  // roundDuration does exactly the same as time.Duration#Round in go.1.9+ since we are
    17  // still on go1.8 we do not have this available. You can check the actual
    18  // implementation in
    19  // https://github.com/golang/go/blob/dev.boringcrypto.go1.9/src/time/time.go#L819-L841
    20  // and the it can be found in go1.9 change log https://golang.org/doc/go1.9
    21  func roundDuration(d time.Duration, m time.Duration) time.Duration {
    22  	if m <= 0 {
    23  		return d
    24  	}
    25  	r := d % m
    26  	if d < 0 {
    27  		r = -r
    28  		if lessThanHalf(r, m) {
    29  			return d + r
    30  		}
    31  		if d1 := d - m + r; d1 < d {
    32  			return d1
    33  		}
    34  		return minDuration // overflow
    35  	}
    36  	if lessThanHalf(r, m) {
    37  		return d - r
    38  	}
    39  	if d1 := d + m - r; d1 > d {
    40  		return d1
    41  	}
    42  	return maxDuration // overflow
    43  }
    44  
    45  // lessThanHalf reports whether x+x < y but avoids overflow,
    46  // assuming x and y are both positive (Duration is signed).
    47  func lessThanHalf(x, y time.Duration) bool {
    48  	return uint64(x)+uint64(x) < uint64(y)
    49  }