pax_global_header00006660000000000000000000000064142354730020014512gustar00rootroot0000000000000052 comment=b3c36b859cfb2a8e202fc4c06131a1735b2278a7 clock-0.3.0/000077500000000000000000000000001423547300200126055ustar00rootroot00000000000000clock-0.3.0/.github/000077500000000000000000000000001423547300200141455ustar00rootroot00000000000000clock-0.3.0/.github/FUNDING.yml000066400000000000000000000000151423547300200157560ustar00rootroot00000000000000github: [bep]clock-0.3.0/.github/workflows/000077500000000000000000000000001423547300200162025ustar00rootroot00000000000000clock-0.3.0/.github/workflows/test.yml000066400000000000000000000021371423547300200177070ustar00rootroot00000000000000on: push: branches: [ main ] pull_request: name: Test jobs: test: strategy: matrix: go-version: [1.18.x] platform: [ubuntu-latest, macos-latest, windows-latest] runs-on: ${{ matrix.platform }} steps: - name: Install Go uses: actions/setup-go@v3 with: go-version: ${{ matrix.go-version }} - name: Install staticcheck run: go install honnef.co/go/tools/cmd/staticcheck@latest shell: bash - name: Install golint run: go install golang.org/x/lint/golint@latest shell: bash - name: Update PATH run: echo "$(go env GOPATH)/bin" >> $GITHUB_PATH shell: bash - name: Checkout code uses: actions/checkout@v1 - name: Fmt if: matrix.platform != 'windows-latest' # :( run: "diff <(gofmt -d .) <(printf '')" shell: bash - name: Vet run: go vet ./... # Go 1.18 support is coming: https://github.com/dominikh/go-tools/issues/1200 #- name: Staticcheck # run: staticcheck ./... - name: Lint run: golint ./... - name: Test run: go test -race ./...clock-0.3.0/.gitignore000066400000000000000000000004151423547300200145750ustar00rootroot00000000000000# Binaries for programs and plugins *.exe *.exe~ *.dll *.so *.dylib # Test binary, built with `go test -c` *.test # Output of the go coverage tool, specifically when used with LiteIDE *.out # Dependency directories (remove the comment below to include it) # vendor/ clock-0.3.0/LICENSE000066400000000000000000000020651423547300200136150ustar00rootroot00000000000000MIT License Copyright (c) 2022 Bjørn Erik Pedersen Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. clock-0.3.0/README.md000066400000000000000000000020411423547300200140610ustar00rootroot00000000000000[![Tests on Linux, MacOS and Windows](https://github.com/bep/clock/workflows/Test/badge.svg)](https://github.com/bep/clock/actions?query=workflow:Test) [![Go Report Card](https://goreportcard.com/badge/github.com/bep/clock)](https://goreportcard.com/report/github.com/bep/clock) [![GoDoc](https://godoc.org/github.com/bep/clock?status.svg)](https://godoc.org/github.com/bep/clock) This package provides a _ticking clock_ that allows you to set the start time. It also provides a system clock, both implementing this interface: ```go // Clock provides the sub set of methods in time.Time that this package provides. type Clock interface { Now() time.Time Since(t time.Time) time.Duration Until(t time.Time) time.Duration // Offset returns the offset of this clock relative to the system clock. Offset() time.Duration } ``` Note that this only support a subset of all the methods in `time.Time` (see above) and is by design very simple. If you're looking for a more advanced time mocking library, have a look at https://github.com/benbjohnson/clock. clock-0.3.0/clock.go000066400000000000000000000030061423547300200142260ustar00rootroot00000000000000package clock import ( "time" ) // Clock provides the sub set of methods in time.Time that this package provides. type Clock interface { Now() time.Time Since(t time.Time) time.Duration Until(t time.Time) time.Duration // Offset returns the offset of this clock relative to the system clock. Offset() time.Duration } // Start creates a new Clock starting at t. func Start(t time.Time) Clock { return &clock{ offset: t.Sub(time.Now()), } } type clock struct { offset time.Duration } // Now returns the current time relative to the configured start time. func (c *clock) Now() time.Time { return time.Now().Add(c.offset) } // Since returns the time elapsed since t. func (c *clock) Since(t time.Time) time.Duration { return c.Now().Sub(t) } // Until returns the duration until t. func (c *clock) Until(t time.Time) time.Duration { return t.Sub(c.Now()) } // Offset returns the offset of this clock relative to the system clock. // This can be used to convert to/from system time. func (c *clock) Offset() time.Duration { return c.offset } var goClock = &systemClock{} // System is a Clock that uses the system clock, meaning it just delegates to time.Now() etc. func System() Clock { return goClock } type systemClock struct { } func (c *systemClock) Now() time.Time { return time.Now() } func (c *systemClock) Since(t time.Time) time.Duration { return time.Since(t) } func (c *systemClock) Until(t time.Time) time.Duration { return time.Until(t) } func (c *systemClock) Offset() time.Duration { return 0 } clock-0.3.0/clock_test.go000066400000000000000000000041371423547300200152730ustar00rootroot00000000000000package clock import ( "testing" "time" qt "github.com/frankban/quicktest" "github.com/google/go-cmp/cmp" ) const timeLayout = "2006-01-02-15:04:05" var durationEq = qt.CmpEquals( cmp.Comparer(func(x, y time.Duration) bool { return x.Truncate(1*time.Second) == y.Truncate(1*time.Second) }), ) func TestClock(t *testing.T) { c := qt.New(t) c.Run("Past", func(c *qt.C) { c.Parallel() start, _ := time.Parse(timeLayout, "2019-10-11-02:50:01") clock := Start(start) c.Assert(toString(clock.Now()), qt.Equals, "2019-10-11-02:50:01") time.Sleep(1 * time.Second) c.Assert(toString(clock.Now()), qt.Equals, "2019-10-11-02:50:02") }) c.Run("Future", func(c *qt.C) { c.Parallel() start, _ := time.Parse(timeLayout, "2053-10-11-02:50:01") clock := Start(start) c.Assert(toString(clock.Now()), qt.Equals, "2053-10-11-02:50:01") time.Sleep(1 * time.Second) c.Assert(toString(clock.Now()), qt.Equals, "2053-10-11-02:50:02") }) c.Run("Offset", func(c *qt.C) { c.Parallel() clock := Start(time.Now().Add(5010 * time.Millisecond)) c.Assert(clock.Offset(), durationEq, time.Duration(5*time.Second)) }) c.Run("Since", func(c *qt.C) { c.Parallel() start, _ := time.Parse(timeLayout, "2019-10-11-02:50:01") clock := Start(start) time.Sleep(1 * time.Second) c.Assert(clock.Since(start), durationEq, time.Duration(1*time.Second)) }) c.Run("Until", func(c *qt.C) { c.Parallel() start, _ := time.Parse(timeLayout, "2019-10-11-02:50:01") clock := Start(start) then := clock.Now().Add(3010 * time.Millisecond) c.Assert(clock.Until(then), durationEq, time.Duration(3*time.Second)) }) } func TestSystemClock(t *testing.T) { t.Parallel() c := qt.New(t) c.Assert(toString(System().Now()), qt.Equals, toString(time.Now())) c.Assert(System().Since(time.Now().Add(-10*time.Hour)), durationEq, time.Since(time.Now().Add(-10*time.Hour))) c.Assert(System().Until(time.Now().Add(10*time.Hour)), durationEq, time.Until(time.Now().Add(10*time.Hour))) c.Assert(System().Offset(), qt.Equals, time.Duration(0)) } func toString(t time.Time) string { return t.UTC().Format(timeLayout) } clock-0.3.0/go.mod000066400000000000000000000005201423547300200137100ustar00rootroot00000000000000module github.com/bep/clock go 1.18 require ( github.com/frankban/quicktest v1.14.2 github.com/google/go-cmp v0.5.7 ) require ( github.com/kr/pretty v0.3.0 // indirect github.com/kr/text v0.2.0 // indirect github.com/rogpeppe/go-internal v1.6.1 // indirect golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 // indirect ) clock-0.3.0/go.sum000066400000000000000000000030441423547300200137410ustar00rootroot00000000000000github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/frankban/quicktest v1.14.2 h1:SPb1KFFmM+ybpEjPUhCCkZOM5xlovT5UbrMvWnXyBns= github.com/frankban/quicktest v1.14.2/go.mod h1:mgiwOwqx65TmIk1wJ6Q7wvnVMocbUorkibMOrVTHZps= github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o= github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k= github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=