github.com/songzhibin97/gkit@v1.2.13/tools/pointer/time_duration.go (about)

     1  package pointer
     2  
     3  import "time"
     4  
     5  // ToDurationPointer 将time.Duration类型的变量转换为对应的*time.Duration指针类型
     6  func ToDurationPointer(v time.Duration) *time.Duration {
     7  	return &v
     8  }
     9  
    10  // ToDurationPointerOrNilIfZero 将time.Duration类型的变量转换为对应的*time.Duration指针类型,如果变量的值为0的话则返回nil指针
    11  func ToDurationPointerOrNilIfZero(v time.Duration) *time.Duration {
    12  	if v == 0 {
    13  		return nil
    14  	}
    15  	return &v
    16  }
    17  
    18  // FromDurationPointer 获取*time.Duration类型的指针的实际值,如果指针为nil的话则返回0
    19  func FromDurationPointer(p *time.Duration) time.Duration {
    20  	return FromDurationPointerOrDefaultIfNil(p, 0)
    21  }
    22  
    23  // FromDurationPointerOrDefaultIfNil 获取*time.Duration类型的指针的实际值,如果指针为nil的话则返回defaultValue
    24  func FromDurationPointerOrDefaultIfNil(v *time.Duration, defaultValue time.Duration) time.Duration {
    25  	if v == nil {
    26  		return defaultValue
    27  	}
    28  	return *v
    29  }