🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Sign inDemoInstall
Socket

github.com/zajann/timer

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/zajann/timer

v0.0.0-20210430040320-cdda3a3a01a1
Source
Go
Version published
Created
Source

timer

timer is a simple Go package to replace time package.

When you want to deal with time, calling time.Now() every time can be a little inefficient.

Let's suppose you're trying to write logs with timestamp. If 10k logs that should be written per sec are, you'll make 10k new time.Time object per sec. (In simple way)

So, I propose a way for derivering value from periodially updated internal time.Time.

Usage

func main() {
  if err := timer.Init(0); err != nil {		// required
    // error
  }
  year := timer.GetYear()
  month := timer.GetMonth()
  monthStr := timer.GetMonthString()
  day := timer.GetDay()
  hour := timer.GetHour()
  min := timer.GetMinute()
  sec := timer.GetSecond()
  nano := timer.GetNanoSecnd()
  unix := timer.GetUnix()
  unixnano := timer.GetUnixNano()
  timeStamp1 := timer.GetTimeFormat(time.RFC1123)
  timeStamp2 := timer.GetTimeFormat("2006-01-02 15:04:05")
  t := timer.GetTime()
}

You can set update cycle by Init(d time.Duration) , 0 means default: 100ms.

Once you've init timer by Init() , you can use globally. It has no worries about thread-safety because it has only read action.

Benchmark

You can becnchmark with timer_test.go in package.

$ go test -bench=. -benchmem
goos: linux
goarch: amd64
pkg: github.com/zajann/timer
BenchmarkGetFormatStringWithTimer-16             2350820               463 ns/op              32 B/op          1 allocs/op
BenchmarkGetFormatStringWithoutTimer-16          1804837               643 ns/op              32 B/op          1 allocs/op
BenchmarkGetMonthStringWithTimer-16             395061864                3.08 ns/op            0 B/op          0 allocs/op
BenchmarkGetMonthStringWithoutTimer-16          12656670                88.1 ns/op             0 B/op          0 allocs/op
PASS
ok      github.com/zajann/timer 6.956s

FAQs

Package last updated on 30 Apr 2021

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