github.com/zerjioang/time32@v0.0.0-20211102104504-b756043b9843/time32.go (about)

     1  //
     2  // Created by zerjioang
     3  // https://github/zerjioang
     4  // Copyright (c) 2020. All rights reserved.
     5  //
     6  // SPDX-License-Identifier: GPL-3.0
     7  //
     8  
     9  // Package time32 implements a time.Now() features of Go language in order to fetch Epoch time our UTC time
    10  // without the need of using internal pointers for UTC location data ( *loc )
    11  package time32
    12  
    13  /*
    14  Time32 Defines our own time unit which will always hold epoch time
    15  in millis. Example: 1588228661
    16  4294967295
    17  int64 size: 8 bytes
    18  uint32 size: 4 bytes
    19  
    20  x2 size reduction changing data size
    21  */
    22  type Time32 uint32
    23  
    24  func (t Time32) AddDate(days int) Time32 {
    25  	v := int(t) + (days * 86400)
    26  	return Time32(v)
    27  }
    28  
    29  func (t *Time32) setTime(now uint32) {
    30  	*t = Time32(now)
    31  }
    32  
    33  // Epoch Returns current server epoch millis time without
    34  // GC dealing with *loc pointers
    35  func Epoch() Time32 {
    36  	return Time32(get_now())
    37  }
    38  
    39  // Epoch Returns current server epoch millis time without
    40  // GC dealing with *loc pointers
    41  func get_now() uint32 {
    42  	sec, nsec, mono := time_now()
    43  	mono -= startNano
    44  	var wall uint64
    45  	var ext int64
    46  	var unsec = uint64(nsec)
    47  	sec += unixToInternal - minWall
    48  	var usec = uint64(sec)
    49  	if usec>>33 != 0 {
    50  		wall, ext = unsec, sec+minWall
    51  	} else {
    52  		wall, ext = hasMonotonic|usec<<nsecShift|unsec, mono
    53  	}
    54  	tsec := ext
    55  	if wall&hasMonotonic != 0 {
    56  		tsec = wallToInternal + int64(wall<<1>>(nsecShift+1))
    57  	}
    58  	return uint32(tsec + internalToUnix)
    59  }