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

     1  package pointer
     2  
     3  import "time"
     4  
     5  // NowTimePointer 返回当前时间的指针表示形式 *time.Time
     6  func NowTimePointer() *time.Time {
     7  	f := time.Now()
     8  	return &f
     9  }
    10  
    11  // ToTimePointer 将time.Time类型转换为指针,如果时间为零值则返回空指针
    12  func ToTimePointer(t time.Time) *time.Time {
    13  	if t.IsZero() {
    14  		return nil
    15  	}
    16  	return &t
    17  }
    18  
    19  // FromTimePointer 从time.Time类型的指针中读取时间,如果为空指针,则读取到零值
    20  func FromTimePointer(t *time.Time) time.Time {
    21  	return FromTimePointerOrDefault(t, time.Time{})
    22  }
    23  
    24  // FromTimePointerOrDefault 从time.Time类型的指针中读取时间,如果为空指针,则返回defaultValue
    25  func FromTimePointerOrDefault(t *time.Time, defaultValue time.Time) time.Time {
    26  	if t == nil {
    27  		return defaultValue
    28  	}
    29  	return *t
    30  }