You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

github.com/jychri/timer

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/jychri/timer

v0.0.2
Source
Go
Version published
Created
Source

timer

timer is a small package, used by git-in-sync.

Package timer records Moments inside of a Timer.

// Moment marks moments in time.
type Moment struct {
	Name  string
	Time  time.Time
	Start time.Duration // duration since start
	Split time.Duration // duration since last moment
}

// Timer collects Moments.
type Timer struct {
	Moments []Moment
}

A Timer is initialized with a Start Moment.

// Init initializes a *Timer with a Start Moment.
func Init() *Timer {
	ti := new(Timer)
	st := Moment{Name: "Start", Time: time.Now()} // start
	ti.Moments = append(ti.Moments, st)
	return ti
}

When a Moment is added to the Timer, its Start and Split values are calculated against the previous Moment and the Start Moment.

// Mark marks a moment in time as a Moment and appends t.Moments.
func (ti *Timer) Mark(s string) {
	sm := ti.Moments[0]                          // starting moment
	lm := ti.Moments[len(ti.Moments)-1]          // last moment
	m := Moment{Name: s, Time: time.Now()}       // name and time
	m.Start = time.Since(sm.Time).Truncate(1000) // duration since start
	m.Split = m.Start - lm.Start                 // duration since last moment
	ti.Moments = append(ti.Moments, m)           // append Moment
}

You can get information about the last Moment with Time and Split methods. Get provides access to a specific Moment.

// Time returns the elapsed time at the last recorded moment in *Timer.
func (ti *Timer) Time() time.Duration {
	lm := ti.Moments[len(ti.Moments)-1] // last moment
	return lm.Start
}

// Split returns the split time for the last recorded moment in *Timer.
func (ti *Timer) Split() time.Duration {
	lm := ti.Moments[len(ti.Moments)-1] // last moment
	return lm.Split
}

// Get returns a Moment and an error value from a *Timer.
func (ti *Timer) Get(s string) (Moment, error) {
	for _, m := range ti.Moments {
		if m.Name == s {
			return m, nil
		}
	}

	var em Moment // empty moment
	return em, errors.New("no moment found")
}

FAQs

Package last updated on 19 Jul 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.