github.com/haraldrudell/parl@v0.4.176/ptime/epoch-value.go (about) 1 /* 2 © 2023–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/) 3 ISC License 4 */ 5 6 package ptime 7 8 import ( 9 "sync/atomic" 10 "time" 11 ) 12 13 // EpochValue is a timestamp with Thread-Safe atomic access. 14 type EpochValue Epoch 15 16 // Get returns the current Epoch value 17 // - zero epoch is returned as zero-time Time.IsZero, time.Time{} 18 // - to get time.Time: epochValue.Get().Time() 19 // - to check if non-zero: epochValue.Get().IsValid() 20 func (ev *EpochValue) Get() (epoch Epoch) { 21 return Epoch(atomic.LoadInt64((*int64)(ev))) 22 } 23 24 // Set updates the Epoch value returning the old value 25 // - 0 is the zero-value 26 func (ev *EpochValue) Set(epoch Epoch) (oldEpoch Epoch) { 27 return Epoch(atomic.SwapInt64((*int64)(ev), int64(epoch))) 28 } 29 30 // SetTime updates the Epoch value to a time.Time value returning the old Epoch value 31 // - default time value is time.Now() 32 // - time.IsZero or time.Time{} is the zero-value 33 func (ev *EpochValue) SetTime(t ...time.Time) (oldEpoch Epoch) { 34 return Epoch(atomic.SwapInt64((*int64)(ev), int64(EpochNow(t...)))) 35 }