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

     1  /* For license and copyright information please see LEGAL file in repository */
     2  
     3  package utc
     4  
     5  import (
     6  	"sync/atomic"
     7  
     8  	"../../protocol"
     9  	"../unix"
    10  )
    11  
    12  // Elapsed types specified time elapsed of January 1 of the absolute year.
    13  // January 1 of the absolute year(0001), like January 1 of 2001, was a Monday.
    14  type (
    15  	MonthElapsed int64 // utc.Now().MonthElapsed()
    16  	DayElapsed   int64 // utc.Now().DayElapsed()
    17  	HourElapsed  int64 // utc.Now().HourElapsed()
    18  	SecElapsed   int64 // utc.Now().SecondElapsed()
    19  	MilliElapsed int64
    20  	MicroElapsed int64
    21  	NanoElapsed  int64
    22  )
    23  
    24  func Now() (t Time) { t.Now(); return }
    25  
    26  type Time struct {
    27  	sec  int64
    28  	nsec int32
    29  }
    30  
    31  func (t *Time) Epoch() protocol.TimeEpoch { return protocol.TimeEpoch_UTC }
    32  func (t *Time) SecondElapsed() int64      { return t.sec }
    33  func (t *Time) NanoSecondElapsed() int32  { return t.nsec }
    34  func (t *Time) ToString() string {
    35  	// TODO:::
    36  	return ""
    37  }
    38  
    39  func (t Time) NanoElapsed() NanoElapsed           { return NanoElapsed((t.sec * 1e9) + int64(t.nsec)) }
    40  func (t Time) MicroElapsed() MicroElapsed         { return MicroElapsed((t.sec * 1e6) + int64(t.nsec/1e3)) }
    41  func (t Time) MilliElapsed() MilliElapsed         { return MilliElapsed((t.sec * 1e3) + int64(t.nsec/1e6)) }
    42  func (t Time) SecElapsed() SecElapsed             { return SecElapsed(t.sec) }
    43  func (t Time) HourElapsed() (hour HourElapsed)    { return HourElapsed(t.sec / (60 * 60)) }
    44  func (t Time) DayElapsed() (day DayElapsed)       { return DayElapsed(t.sec / (24 * 60 * 60)) }
    45  func (t Time) MonthElapsed() (month MonthElapsed) { return MonthElapsed(t.sec / (30 * 24 * 60 * 60)) }
    46  func (t Time) TropicalYearElapsed() (year int64)  { return t.sec / TropicalYear }
    47  func (t Time) CallendarYearElapsed() (year int64) { return } // TODO:::
    48  
    49  func (t *Time) ChangeTo(sec SecElapsed, nsecElapsed int32) { t.sec, t.nsec = int64(sec), nsecElapsed }
    50  func (t *Time) Now() {
    51  	t.sec, t.nsec, _ = unix.HardwareNow()
    52  	// TODO:::
    53  }
    54  func (t *Time) NowAtomic() {
    55  	var sec, nsec, _ = unix.HardwareNow()
    56  	// TODO:::
    57  	atomic.AddInt64(&t.sec, sec)
    58  	atomic.AddInt32(&t.nsec, nsec)
    59  }
    60  
    61  // Until return time duration until to given time!
    62  func (t Time) Until(to Time) (until Time) {
    63  	until = Time{
    64  		sec:  t.sec - to.sec,
    65  		nsec: t.nsec - to.nsec,
    66  	}
    67  	return
    68  }
    69  
    70  // UntilTo return second duration until to given time!
    71  func (t Time) UntilTo(to Time) (duration protocol.Duration) {
    72  	return protocol.Duration(t.Until(to).NanoElapsed())
    73  }
    74  
    75  // Pass check if time pass from given time
    76  func (t Time) Pass(from Time) (pass bool) {
    77  	if (t.sec > from.sec) || (t.sec == from.sec && t.nsec > from.nsec) {
    78  		pass = true
    79  	}
    80  	return
    81  }
    82  
    83  // AddDuration return given time plus given duration
    84  func (t Time) AddDuration(d protocol.Duration) (new Time) {
    85  	var secPass = d / Second
    86  	new.sec += int64(secPass)
    87  	new.nsec += int32(d % (secPass * Second))
    88  	return
    89  }
    90  
    91  // Local change given time to local time by OS set time zone
    92  func (t *Time) Local() (loc Time) {
    93  	// TODO:::
    94  	return
    95  }
    96  
    97  func (t *Time) DayHours() (hour DayHours) {
    98  	var secPassDay = t.sec % (24 * 60 * 60)
    99  	var dayHour = secPassDay / (60 * 60)
   100  	hour = (1 << dayHour)
   101  	return
   102  }
   103  func (t *Time) Weekdays() (day Weekdays) {
   104  	var week = t.sec % (7 * 24 * 60 * 60)
   105  	var weekDay = week / (24 * 60 * 60)
   106  	// weekDay index from Thursday so change it to Monday as Weekdays
   107  	if weekDay < 4 {
   108  		weekDay += 3
   109  	} else {
   110  		weekDay -= 4 // Due to WeekdaysNone must -4 instead -3
   111  	}
   112  	day = (1 << weekDay)
   113  	return
   114  }