From 25490dff22ffdd389cc8934bf77f2acb6a0cb79b Mon Sep 17 00:00:00 2001 From: Dean Karn Date: Fri, 30 Jun 2023 08:02:11 -0700 Subject: [PATCH 1/5] Add Instant --- CHANGELOG.md | 7 ++++++- README.md | 2 +- time/instant.go | 31 +++++++++++++++++++++++++++++++ time/instant_test.go | 22 ++++++++++++++++++++++ 4 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 time/instant.go create mode 100644 time/instant_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index b524115..2c6d19c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [5.21.0] - 2023-06-30 +### Added +- Instant type to make working with monotonically increasing times more convenient. + ## [5.20.0] - 2023-06-17 ### Added - Expanded Option type SQL Value support to handle value custom types and honour the `driver.Valuer` interface. @@ -62,7 +66,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Added `timext.NanoTime` for fast low level monotonic time with nanosecond precision. -[Unreleased]: https://github.com/go-playground/pkg/compare/v5.20.0...HEAD +[Unreleased]: https://github.com/go-playground/pkg/compare/v5.21.0...HEAD +[5.21.0]: https://github.com/go-playground/pkg/compare/v5.20.0..v5.21.0 [5.20.0]: https://github.com/go-playground/pkg/compare/v5.19.0..v5.20.0 [5.19.0]: https://github.com/go-playground/pkg/compare/v5.18.0..v5.19.0 [5.18.0]: https://github.com/go-playground/pkg/compare/v5.17.2..v5.18.0 diff --git a/README.md b/README.md index b0e92f1..b7ee92a 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # pkg -![Project status](https://img.shields.io/badge/version-5.20.0-green.svg) +![Project status](https://img.shields.io/badge/version-5.21.0-green.svg) [![Lint & Test](https://github.com/go-playground/pkg/actions/workflows/go.yml/badge.svg)](https://github.com/go-playground/pkg/actions/workflows/go.yml) [![Coverage Status](https://coveralls.io/repos/github/go-playground/pkg/badge.svg?branch=master)](https://coveralls.io/github/go-playground/pkg?branch=master) [![GoDoc](https://godoc.org/github.com/go-playground/pkg?status.svg)](https://pkg.go.dev/mod/github.com/go-playground/pkg/v5) diff --git a/time/instant.go b/time/instant.go new file mode 100644 index 0000000..218f58a --- /dev/null +++ b/time/instant.go @@ -0,0 +1,31 @@ +//go:build go1.18 +// +build go1.18 + +package timeext + +import "time" + +// Instant represents a monotonic instant in time. +// +// Instants are opaque types that can only be compared with one another and allows measuring of duration. +type Instant struct { + monotonic int64 +} + +// NewInstant returns a new Instant. +func NewInstant() Instant { + return Instant{monotonic: NanoTime()} +} + +// Elapsed returns the duration since the instant was created. +func (i Instant) Elapsed() time.Duration { + return time.Duration(NanoTime() - i.monotonic) +} + +// Since returns the duration elapsed from another Instant, or zero is that Instant is later than this one. +func (i Instant) Since(instant Instant) time.Duration { + if instant.monotonic > i.monotonic { + return 0 + } + return time.Duration(i.monotonic - instant.monotonic) +} diff --git a/time/instant_test.go b/time/instant_test.go new file mode 100644 index 0000000..45999ad --- /dev/null +++ b/time/instant_test.go @@ -0,0 +1,22 @@ +//go:build go1.18 +// +build go1.18 + +package timeext + +import ( + "testing" +) + +func TestInstant(t *testing.T) { + i := NewInstant() + if i.Elapsed() < 0 { + t.Fatalf("elapsed time should be always be monotonically increasing") + } + i2 := NewInstant() + if i2.Since(i) <= 0 { + t.Fatalf("time since instant should always be after") + } + if i.Since(i2) != 0 { + t.Fatalf("time since instant should be zero") + } +} From 7509c36864c5c764cfc53fc09e19f72b6480b925 Mon Sep 17 00:00:00 2001 From: Dean Karn Date: Fri, 30 Jun 2023 08:09:16 -0700 Subject: [PATCH 2/5] code running too fast --- time/instant_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/time/instant_test.go b/time/instant_test.go index 45999ad..aa6ffd4 100644 --- a/time/instant_test.go +++ b/time/instant_test.go @@ -5,6 +5,7 @@ package timeext import ( "testing" + "time" ) func TestInstant(t *testing.T) { @@ -13,6 +14,7 @@ func TestInstant(t *testing.T) { t.Fatalf("elapsed time should be always be monotonically increasing") } i2 := NewInstant() + time.Sleep(time.Millisecond) if i2.Since(i) <= 0 { t.Fatalf("time since instant should always be after") } From 3c0da72374a0863a1df900e2057a936928e539dc Mon Sep 17 00:00:00 2001 From: Dean Karn Date: Fri, 30 Jun 2023 08:11:48 -0700 Subject: [PATCH 3/5] still too fast --- time/instant_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/time/instant_test.go b/time/instant_test.go index aa6ffd4..0c4e63b 100644 --- a/time/instant_test.go +++ b/time/instant_test.go @@ -14,7 +14,7 @@ func TestInstant(t *testing.T) { t.Fatalf("elapsed time should be always be monotonically increasing") } i2 := NewInstant() - time.Sleep(time.Millisecond) + time.Sleep(time.Second) if i2.Since(i) <= 0 { t.Fatalf("time since instant should always be after") } From b219a4dd432e98ed62e6f1c0feb365ff6e1e198d Mon Sep 17 00:00:00 2001 From: Dean Karn Date: Fri, 30 Jun 2023 08:14:07 -0700 Subject: [PATCH 4/5] ah windows --- time/instant_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/time/instant_test.go b/time/instant_test.go index 0c4e63b..6a3a7a1 100644 --- a/time/instant_test.go +++ b/time/instant_test.go @@ -14,7 +14,7 @@ func TestInstant(t *testing.T) { t.Fatalf("elapsed time should be always be monotonically increasing") } i2 := NewInstant() - time.Sleep(time.Second) + time.Sleep(time.Second * 2) if i2.Since(i) <= 0 { t.Fatalf("time since instant should always be after") } From eda1eef34f630ec30f0d9dbc9b0c8cf6f521a356 Mon Sep 17 00:00:00 2001 From: Dean Karn Date: Fri, 30 Jun 2023 08:19:22 -0700 Subject: [PATCH 5/5] relax test for windows --- time/instant_test.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/time/instant_test.go b/time/instant_test.go index 6a3a7a1..4a26b02 100644 --- a/time/instant_test.go +++ b/time/instant_test.go @@ -5,7 +5,6 @@ package timeext import ( "testing" - "time" ) func TestInstant(t *testing.T) { @@ -14,8 +13,7 @@ func TestInstant(t *testing.T) { t.Fatalf("elapsed time should be always be monotonically increasing") } i2 := NewInstant() - time.Sleep(time.Second * 2) - if i2.Since(i) <= 0 { + if i2.Since(i) < 0 { t.Fatalf("time since instant should always be after") } if i.Since(i2) != 0 {