github.com/GeniusesGroup/libgo@v0.0.0-20220929090155-5ff932cb408e/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  	"github.com/GeniusesGroup/libgo/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  func Now() (t Time) { t.Now(); return }
    26  
    27  // Provided by package runtime.
    28  //
    29  //go:linkname now time.now
    30  func now() (sec int64, nsec int32, mono int64)
    31  
    32  // A Time specifies second elapsed of January 1 of the absolute year.
    33  // January 1 of the absolute year(1970), like January 1 of 2001, was a Monday.
    34  type Time struct {
    35  	sec  int64
    36  	nsec int32
    37  }
    38  
    39  func (t *Time) Epoch() protocol.TimeEpoch { return protocol.TimeEpoch_Unix }
    40  func (t *Time) SecondElapsed() int64      { return t.sec }
    41  func (t *Time) NanoSecondElapsed() int32  { return t.nsec }
    42  func (t *Time) ToString() string {
    43  	// TODO:::
    44  	return ""
    45  }
    46  
    47  func (t *Time) ChangeTo(sec SecElapsed, nsecElapsed int32) { t.sec, t.nsec = int64(sec), nsecElapsed }
    48  func (t *Time) Now()                                       { t.sec, t.nsec, _ = now() }
    49  func (t *Time) NowAtomic() {
    50  	var sec, nsec, _ = now()
    51  	atomic.AddInt64(&t.sec, sec)
    52  	atomic.AddInt32(&t.nsec, nsec)
    53  }
    54  
    55  func (t Time) Pass(from Time) (pass bool) {
    56  	if (t.sec > from.sec) || (t.sec == from.sec && t.nsec > from.nsec) {
    57  		pass = true
    58  	}
    59  	return
    60  }
    61  func (t Time) AddDuration(d protocol.Duration) (new Time) {
    62  	var secPass = d / Second
    63  	new.sec += int64(secPass)
    64  	new.nsec += int32(d % (secPass * Second))
    65  	return
    66  }
    67  
    68  func (t Time) NanoElapsed() NanoElapsed   { return NanoElapsed((t.sec * 1e9) + int64(t.nsec)) }
    69  func (t Time) MicroElapsed() MicroElapsed { return MicroElapsed((t.sec * 1e6) + int64(t.nsec/1e3)) }
    70  func (t Time) MilliElapsed() MilliElapsed { return MilliElapsed((t.sec * 1e3) + int64(t.nsec/1e6)) }
    71  func (t Time) SecElapsed() SecElapsed     { return SecElapsed(t.sec) }
    72  func (t Time) HourElapsed() HourElapsed   { return HourElapsed(t.sec / (60 * 60)) }
    73  func (t Time) DayElapsed() DayElapsed     { return DayElapsed(t.sec / (24 * 60 * 60)) }