Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
github.com/jujili/clock
clock 中的 realClock
和 simulator
结构体,都实现了 clock.Clock
接口。需要实际时间时,使用前者;需要人为地操纵时间的场合(比如:测试)中,使用后者。
在 time
和 context
标准库中,有一些函数和方法依赖于系统的当前时间。Clock
接口就是由这些内容组成。
clock 模块分为真实与虚拟的两个部分。真实的部分是以 time 和 context 标准库为基础,封装成了 Clock 接口。 虚拟部分也实现了 Clock 接口,只是这一部分可以人为的操控时间的改变。
在命令行中输入以下内容,可以获取到最新版
go get -u github.com/jujili/clock
c := clock.NewRealClock()
// 输出操作系统的当前时间
fmt.Println(c.Now())
真实的 Clock 就是把 time
和 context
标准库中的相关函数,封装成了 realClock
的方法。
now,_ := time.Parse("06-01-02", "20-01-26")
c := clock.NewSimulator(now)
// 输出: 2020-01-26 00:00:00 +0000 UTC
fmt.Println(c.Now())
*Simulator
和 contextSim
虽然是模拟的。但是,实现了与 time
和 context
标准库中同名函数一样的行为。
在 clock 目录下,输入
$ mockgen -source=./interface.go -destination=./clock_mock_test.go -package=clock
$
FAQs
Unknown package
Did you know?
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.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.