github.com/geniusesgroup/libgo@v0.0.0-20220713101832-828057a9d3d4/time/unix/time.go (about)

     1  /* For license and copyright information please see LEGAL file in repository */
     2  
     3  package unix
     4  
     5  import (
     6  	"sync/atomic"
     7  	_ "unsafe" // for go:linkname
     8  
     9  	"../../protocol"
    10  )
    11  
    12  const Base = "00:00:00 UTC on 1 January 1970" // Thursday
    13  
    14  // UnixTime specifies time elapsed of January 1 of the absolute year.
    15  // January 1 of the absolute year(1970), like January 1 of 2001, was a Monday.
    16  type (
    17  	DayElapsed   int64 // fast way: unix.Now().DayElapsed()
    18  	HourElapsed  int64 // fast way: unix.Now().HourElapsed()
    19  	SecElapsed   int64 // fast way: unix.Now().SecElapsed()		|| Go way: time.Now().Unix()
    20  	MilliElapsed int64 // fast way: unix.Now().MilliElapsed()	|| Go way: time.Now().UnixMilli()
    21  	MicroElapsed int64 // fast way: unix.Now().MicroElapsed()	|| Go way: time.Now().UnixMicro()
    22  	NanoElapsed  int64 // fast way: unix.Now().NanoElapsed()	|| Go way: time.Now().UnixNano()
    23  )
    24  
    25  // Provided by package runtime.
    26  //go:linkname HardwareNow time.now
    27  func HardwareNow() (sec int64, nsec int32, mono int64)
    28  func Now() (t Time) { t.Now(); return }
    29  
    30  // A Time specifies second elapsed of January 1 of the absolute year.
    31  // January 1 of the absolute year(1970), like January 1 of 2001, was a Monday.
    32  type Time struct {
    33  	sec  int64
    34  	nsec int32
    35  }
    36  
    37  func (t *Time) Epoch() protocol.TimeEpoch { return protocol.TimeEpoch_Unix }
    38  func (t *Time) SecondElapsed() int64      { return t.sec }
    39  func (t *Time) NanoSecondElapsed() int32  { return t.nsec }
    40  func (t *Time) ToString() string {
    41  	// TODO:::
    42  	return ""
    43  }
    44  
    45  func (t *Time) ChangeTo(sec SecElapsed, nsecElapsed int32) { t.sec, t.nsec = int64(sec), nsecElapsed }
    46  func (t *Time) Now()                                       { t.sec, t.nsec, _ = HardwareNow() }
    47  func (t *Time) NowAtomic() {
    48  	var sec, nsec, _ = HardwareNow()
    49  	atomic.AddInt64(&t.sec, sec)
    50  	atomic.AddInt32(&t.nsec, nsec)
    51  }
    52  
    53  func (t Time) Pass(from Time) (pass bool) {
    54  	if (t.sec > from.sec) || (t.sec == from.sec && t.nsec > from.nsec) {
    55  		pass = true
    56  	}
    57  	return
    58  }
    59  func (t Time) AddDuration(d protocol.Duration) (new Time) {
    60  	var secPass = d / Second
    61  	new.sec += int64(secPass)
    62  	new.nsec += int32(d % (secPass * Second))
    63  	return
    64  }
    65  
    66  func (t Time) NanoElapsed() NanoElapsed   { return NanoElapsed((t.sec * 1e9) + int64(t.nsec)) }
    67  func (t Time) MicroElapsed() MicroElapsed { return MicroElapsed((t.sec * 1e6) + int64(t.nsec/1e3)) }
    68  func (t Time) MilliElapsed() MilliElapsed { return MilliElapsed((t.sec * 1e3) + int64(t.nsec/1e6)) }
    69  func (t Time) SecElapsed() SecElapsed     { return SecElapsed(t.sec) }
    70  func (t Time) HourElapsed() HourElapsed   { return HourElapsed(t.sec / (60 * 60)) }
    71  func (t Time) DayElapsed() DayElapsed     { return DayElapsed(t.sec / (24 * 60 * 60)) }