Skip to content

Commit 9a02754

Browse files
author
Dean Karn
authored
Add Instant (#36)
A way to conveniently represent and work with monotonically increasing time doesn't exist in Go and so added the new `Instant` type.
1 parent 8acc0b9 commit 9a02754

File tree

4 files changed

+60
-2
lines changed

4 files changed

+60
-2
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
## [5.21.0] - 2023-06-30
10+
### Added
11+
- Instant type to make working with monotonically increasing times more convenient.
12+
913
## [5.20.0] - 2023-06-17
1014
### Added
1115
- 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
6266
### Added
6367
- Added `timext.NanoTime` for fast low level monotonic time with nanosecond precision.
6468

65-
[Unreleased]: https://github.com/go-playground/pkg/compare/v5.20.0...HEAD
69+
[Unreleased]: https://github.com/go-playground/pkg/compare/v5.21.0...HEAD
70+
[5.21.0]: https://github.com/go-playground/pkg/compare/v5.20.0..v5.21.0
6671
[5.20.0]: https://github.com/go-playground/pkg/compare/v5.19.0..v5.20.0
6772
[5.19.0]: https://github.com/go-playground/pkg/compare/v5.18.0..v5.19.0
6873
[5.18.0]: https://github.com/go-playground/pkg/compare/v5.17.2..v5.18.0

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# pkg
22

3-
![Project status](https://img.shields.io/badge/version-5.20.0-green.svg)
3+
![Project status](https://img.shields.io/badge/version-5.21.0-green.svg)
44
[![Lint & Test](https://github.com/go-playground/pkg/actions/workflows/go.yml/badge.svg)](https://github.com/go-playground/pkg/actions/workflows/go.yml)
55
[![Coverage Status](https://coveralls.io/repos/github/go-playground/pkg/badge.svg?branch=master)](https://coveralls.io/github/go-playground/pkg?branch=master)
66
[![GoDoc](https://godoc.org/github.com/go-playground/pkg?status.svg)](https://pkg.go.dev/mod/github.com/go-playground/pkg/v5)

time/instant.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//go:build go1.18
2+
// +build go1.18
3+
4+
package timeext
5+
6+
import "time"
7+
8+
// Instant represents a monotonic instant in time.
9+
//
10+
// Instants are opaque types that can only be compared with one another and allows measuring of duration.
11+
type Instant struct {
12+
monotonic int64
13+
}
14+
15+
// NewInstant returns a new Instant.
16+
func NewInstant() Instant {
17+
return Instant{monotonic: NanoTime()}
18+
}
19+
20+
// Elapsed returns the duration since the instant was created.
21+
func (i Instant) Elapsed() time.Duration {
22+
return time.Duration(NanoTime() - i.monotonic)
23+
}
24+
25+
// Since returns the duration elapsed from another Instant, or zero is that Instant is later than this one.
26+
func (i Instant) Since(instant Instant) time.Duration {
27+
if instant.monotonic > i.monotonic {
28+
return 0
29+
}
30+
return time.Duration(i.monotonic - instant.monotonic)
31+
}

time/instant_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//go:build go1.18
2+
// +build go1.18
3+
4+
package timeext
5+
6+
import (
7+
"testing"
8+
)
9+
10+
func TestInstant(t *testing.T) {
11+
i := NewInstant()
12+
if i.Elapsed() < 0 {
13+
t.Fatalf("elapsed time should be always be monotonically increasing")
14+
}
15+
i2 := NewInstant()
16+
if i2.Since(i) < 0 {
17+
t.Fatalf("time since instant should always be after")
18+
}
19+
if i.Since(i2) != 0 {
20+
t.Fatalf("time since instant should be zero")
21+
}
22+
}

0 commit comments

Comments
 (0)