// ------------------------------------------------------------------------ // Project atila // Active Thing (activething.com) git.activething.com/go // // File name rkey.go // Created by DEV // Modified 05/02/2024 // // Copyright 2024 activething.com // ------------------------------------------------------------------------ // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // ------------------------------------------------------------------------ package crns import ( "sync/atomic" "time" ) type ( Chrono struct { started int64 stopped int64 } ) func NewChrono(str bool) *Chrono { ch := &Chrono{} if str { ch.Start() } return ch } func (c *Chrono) Started() time.Time { return time.Unix(0,atomic.LoadInt64(&c.started)) } func (c *Chrono) Stopped() time.Time { return time.Unix(0,atomic.LoadInt64(&c.stopped)) } func (c *Chrono) Elapsed() time.Duration { st := atomic.LoadInt64(&c.started) if st == 0 { return 0 } sp := atomic.LoadInt64(&c.stopped) if sp == 0 { return time.Now().Sub(time.Unix(0, int64(st))) } return time.Duration(sp - st) } func (c *Chrono) Reset() { atomic.StoreInt64(&c.started,0) atomic.StoreInt64(&c.stopped,0) } func (c *Chrono) Start() (tm time.Time) { tm = time.Now() atomic.CompareAndSwapInt64(&c.started,0,tm.UnixNano()) return } func (c *Chrono) Stop() (tm time.Time) { tm = time.Now() atomic.CompareAndSwapInt64(&c.stopped,0,tm.UnixNano()) return } func (c *Chrono) SetTimes(str, stp time.Time) { atomic.StoreInt64(&c.started,str.UnixNano()) atomic.StoreInt64(&c.stopped,str.UnixNano()) } /* :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: I HAVE NO DESIRE TO WALK ON WATER," SAID SIDDHARTA. "LET THE OLD SHRAMANAS SATISFY THEMSELVES WITH SUCH SKILLS. SIDDHARTA - HERMANN HESSE - ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */