gitee.com/quant1x/gox@v1.21.2/timestamp/timestamp.go (about)

     1  // Package timestamp 本地时间戳相关功能
     2  package timestamp
     3  
     4  import (
     5  	"fmt"
     6  	"time"
     7  	_ "unsafe" // for go:linkname
     8  )
     9  
    10  const (
    11  	SecondsPerMinute      = 60
    12  	SecondsPerHour        = 60 * SecondsPerMinute
    13  	SecondsPerDay         = 24 * SecondsPerHour
    14  	MillisecondsPerSecond = 1000
    15  	MillisecondsPerMinute = SecondsPerMinute * MillisecondsPerSecond
    16  	MillisecondsPerHour   = SecondsPerHour * MillisecondsPerSecond
    17  	MillisecondsPerDay    = SecondsPerDay * MillisecondsPerSecond
    18  )
    19  
    20  //go:linkname now time.now
    21  func now() (sec int64, nsec int32, mono int64)
    22  
    23  ////调用公开结构的私有方法
    24  ////
    25  ////go:linkname abstime time.Time.abs
    26  ////func abstime(t time.Time) uint64
    27  
    28  var (
    29  	defaultLocal = time.Local
    30  	// 获取偏移的秒数
    31  	zoneName, offsetInSecondsEastOfUTC = time.Now().Zone()
    32  	_                                  = zoneName
    33  	// 本地0秒
    34  	zeroTime = time.Date(1970, 1, 1, 0, 0, 0, 0, defaultLocal)
    35  	// UTC到本地的偏移秒数
    36  	utcToLocal = int64(offsetInSecondsEastOfUTC)
    37  	// 本地到UTC的偏移秒数
    38  	localToUTC = -utcToLocal
    39  	// 偏移的毫秒数
    40  	//offsetMilliseconds = offsetInSecondsEastOfUTC * MillisecondsPerSecond
    41  )
    42  
    43  // Now 获取本地当前的时间戳, 毫秒数
    44  //
    45  //	UTC 转 local
    46  func Now() int64 {
    47  	return v2Now()
    48  }
    49  
    50  func v1Now() int64 {
    51  	sec, nsec, _ := now()
    52  	sec += int64(offsetInSecondsEastOfUTC)
    53  	milli := sec*MillisecondsPerSecond + int64(nsec)/1e6%MillisecondsPerSecond
    54  	return milli
    55  }
    56  
    57  // TimeToTimestamp 获取time.Time的本地毫秒数
    58  //
    59  //	UTC 转 local
    60  func TimeToTimestamp(t time.Time) int64 {
    61  	utcMilliseconds := t.UnixMilli()
    62  	milliseconds := utcMilliseconds + utcToLocal*MillisecondsPerSecond
    63  	return milliseconds
    64  }
    65  
    66  // Time 本地毫秒数转time.Time
    67  //
    68  //	local 转 UTC
    69  func Time(milliseconds int64) time.Time {
    70  	utcMilliseconds := milliseconds + localToUTC*MillisecondsPerSecond
    71  	return time.UnixMilli(utcMilliseconds)
    72  }
    73  
    74  // SinceZero 从0点到milliseconds过去的毫秒数
    75  func SinceZero(milliseconds int64) int64 {
    76  	elapsed := milliseconds % MillisecondsPerDay
    77  	if elapsed < 0 {
    78  		elapsed += MillisecondsPerDay
    79  	}
    80  	return elapsed
    81  }
    82  
    83  // ZeroHour 零点整的毫秒数
    84  func ZeroHour(milliseconds int64) int64 {
    85  	elapsed := SinceZero(milliseconds)
    86  	diff := milliseconds - elapsed
    87  	return diff
    88  }
    89  
    90  // Since t当日0点整到t的毫秒数
    91  func Since(t time.Time) int64 {
    92  	milliseconds := TimeToTimestamp(t)
    93  	elapsed := SinceZero(milliseconds)
    94  	return elapsed
    95  }
    96  
    97  // Today 获取当日0点整的时间戳, 毫秒数
    98  //
    99  //	UTC 转 local
   100  func Today() int64 {
   101  	milliseconds := Now()
   102  	//elapsed := milliseconds - milliseconds%MillisecondsPerDay
   103  	elapsed := ZeroHour(milliseconds)
   104  	return elapsed
   105  }
   106  
   107  // CurrentDateZero t日期的0点整
   108  func CurrentDateZero(t time.Time) time.Time {
   109  	y, m, d := t.Date()
   110  	return time.Date(y, m, d, 0, 0, 0, 0, time.Local)
   111  }
   112  
   113  // TodayZero 这也是一个当日0点的用法
   114  func TodayZero() time.Time {
   115  	now := time.Now()
   116  	y, m, d := now.Date()
   117  	return time.Date(y, m, d, 0, 0, 0, 0, time.Local)
   118  }
   119  
   120  // SinceZeroHour t当天0点开始到t时的毫秒数
   121  func SinceZeroHour(t time.Time) int64 {
   122  	zero := CurrentDateZero(t)
   123  	return t.Sub(zero).Milliseconds()
   124  }
   125  
   126  const (
   127  	secondsPerMinute = 60
   128  	secondsPerHour   = 60 * secondsPerMinute
   129  	secondsPerDay    = 24 * secondsPerHour
   130  	secondsPerWeek   = 7 * secondsPerDay
   131  	daysPer400Years  = 365*400 + 97
   132  	daysPer100Years  = 365*100 + 24
   133  	daysPer4Years    = 365*4 + 1
   134  )
   135  
   136  const (
   137  	// The unsigned zero year for internal calculations.
   138  	// Must be 1 mod 400, and times before it will not compute correctly,
   139  	// but otherwise can be changed at will.
   140  	absoluteZeroYear = -292277022399
   141  
   142  	// The year of the zero Time.
   143  	// Assumed by the unixToInternal computation below.
   144  	internalYear = 1
   145  
   146  	// Offsets to convert between internal and absolute or Unix times.
   147  	absoluteToInternal int64 = (absoluteZeroYear - internalYear) * 365.2425 * secondsPerDay
   148  	internalToAbsolute       = -absoluteToInternal
   149  
   150  	unixToInternal int64 = (1969*365 + 1969/4 - 1969/100 + 1969/400) * secondsPerDay
   151  	internalToUnix int64 = -unixToInternal
   152  
   153  	wallToInternal int64 = (1884*365 + 1884/4 - 1884/100 + 1884/400) * secondsPerDay
   154  )
   155  
   156  // Timestamp 时间戳类型
   157  type Timestamp int64
   158  
   159  // abs returns the time t as an absolute time, adjusted by the zone offset.
   160  // It is called when computing a presentation property like Month or Hour.
   161  func (t Timestamp) abs() uint64 {
   162  	//l := defaultLocal
   163  	ms := int64(t / MillisecondsPerSecond)
   164  	return uint64(ms + (unixToInternal + internalToAbsolute))
   165  }
   166  
   167  // 调用公开结构的私有方法
   168  //
   169  //go:linkname absDate time.absDate
   170  func absDate(abs uint64, full bool) (year int, month time.Month, day int, yday int)
   171  
   172  // DateTime 获取日期时间毫秒
   173  func (t Timestamp) DateTime() (year, month, day, hour, minute, second, millisecond int) {
   174  	ms := int64(t)
   175  	absSeconds := t.abs()
   176  	year, m, day, yday := absDate(absSeconds, true)
   177  	_ = yday
   178  	month = int(m)
   179  	hour = int((ms % MillisecondsPerDay) / MillisecondsPerHour)
   180  	minute = int((ms % MillisecondsPerHour) / MillisecondsPerMinute)
   181  	second = int((ms % MillisecondsPerMinute) / MillisecondsPerSecond)
   182  	millisecond = int(ms % MillisecondsPerSecond)
   183  	return
   184  }
   185  
   186  func (t Timestamp) String() string {
   187  	year, month, day, hour, minute, second, millisecond := t.DateTime()
   188  	return fmt.Sprintf("%04d-%02d-%02d %02d:%02d:%02d.%03d", year, month, day, hour, minute, second, millisecond)
   189  }