Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Instant to be a type not struct. #37

Merged
merged 2 commits into from
Jul 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [5.21.1] - 2023-06-30
### Fixed
- Instant type to not be wrapped in a struct but a type itself.

## [5.21.0] - 2023-06-30
### Added
- Instant type to make working with monotonically increasing times more convenient.
Expand Down Expand Up @@ -66,7 +70,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.21.0...HEAD
[Unreleased]: https://github.com/go-playground/pkg/compare/v5.21.1...HEAD
[5.21.0]: https://github.com/go-playground/pkg/compare/v5.21.0..v5.21.1
[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
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# pkg

![Project status](https://img.shields.io/badge/version-5.21.0-green.svg)
![Project status](https://img.shields.io/badge/version-5.21.1-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)
Expand Down
12 changes: 5 additions & 7 deletions time/instant.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,22 @@ 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
}
type Instant int64

// NewInstant returns a new Instant.
func NewInstant() Instant {
return Instant{monotonic: NanoTime()}
return Instant(NanoTime())
}

// Elapsed returns the duration since the instant was created.
func (i Instant) Elapsed() time.Duration {
return time.Duration(NanoTime() - i.monotonic)
return time.Duration(NewInstant() - i)
}

// 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 {
if instant > i {
return 0
}
return time.Duration(i.monotonic - instant.monotonic)
return time.Duration(i - instant)
}