github.com/turingchain2020/turingchain@v1.1.21/types/time.go (about)

     1  // Copyright Turing Corp. 2018 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  package types
     6  
     7  import (
     8  	"sync/atomic"
     9  	"time"
    10  )
    11  
    12  var deltaTime int64
    13  
    14  //NtpHosts ntp hosts
    15  var NtpHosts = []string{
    16  	"ntp.aliyun.com:123",
    17  	"time1.cloud.tencent.com:123",
    18  	"time.ustc.edu.cn:123",
    19  	"cn.ntp.org.cn:123",
    20  	"time.apple.com:123",
    21  }
    22  
    23  //SetTimeDelta realtime - localtime
    24  //超过60s 不做修正
    25  //为了系统的安全,我们只做小范围时间错误的修复
    26  func SetTimeDelta(dt int64) {
    27  	if dt > 300*int64(time.Second) || dt < -300*int64(time.Second) {
    28  		dt = 0
    29  	}
    30  	atomic.StoreInt64(&deltaTime, dt)
    31  }
    32  
    33  //Now 获取当前时间戳
    34  func Now() time.Time {
    35  	dt := time.Duration(atomic.LoadInt64(&deltaTime))
    36  	return time.Now().Add(dt)
    37  }
    38  
    39  //Since Since时间
    40  func Since(t time.Time) time.Duration {
    41  	return Now().Sub(t)
    42  }