From 51fe93021e41ae95c378224595d5718c3dacdd8e Mon Sep 17 00:00:00 2001 From: Andrei Matei Date: Fri, 8 Oct 2021 17:55:43 -0400 Subject: [PATCH] timeutil: remove detritus windows Now() implementation We carried a custom implementation of timeutil.Now() for Windows, to overcome the low precision of Go's impl [1]. It was doing so with an expensive implementation. Having this custom implementation also make it tricky to use stdlib functions like Since(), which internally use the stdlib Now(). The Golang issue has since been fixed. [1] https://github.com/golang/go/issues/8687 Release note: None --- pkg/testutils/lint/lint_test.go | 2 -- pkg/util/timeutil/BUILD.bazel | 9 +------ pkg/util/timeutil/now_unix.go | 21 ----------------- pkg/util/timeutil/now_windows.go | 40 -------------------------------- pkg/util/timeutil/time.go | 5 ++++ 5 files changed, 6 insertions(+), 71 deletions(-) delete mode 100644 pkg/util/timeutil/now_unix.go delete mode 100644 pkg/util/timeutil/now_windows.go diff --git a/pkg/testutils/lint/lint_test.go b/pkg/testutils/lint/lint_test.go index 08cb18f207a..44321a1fed8 100644 --- a/pkg/testutils/lint/lint_test.go +++ b/pkg/testutils/lint/lint_test.go @@ -723,8 +723,6 @@ func TestLint(t *testing.T) { "--", "*.go", ":!**/embedded.go", - ":!util/timeutil/now_unix.go", - ":!util/timeutil/now_windows.go", ":!util/timeutil/time.go", ":!util/timeutil/zoneinfo.go", ":!util/tracing/span.go", diff --git a/pkg/util/timeutil/BUILD.bazel b/pkg/util/timeutil/BUILD.bazel index 9c7b34e8414..71cf93609ca 100644 --- a/pkg/util/timeutil/BUILD.bazel +++ b/pkg/util/timeutil/BUILD.bazel @@ -4,8 +4,6 @@ go_library( name = "timeutil", srcs = [ "manual_time.go", - "now_unix.go", - "now_windows.go", "stopwatch.go", "time.go", "time_source.go", @@ -20,12 +18,7 @@ go_library( deps = [ "//pkg/util/syncutil", "@com_github_cockroachdb_errors//:errors", - ] + select({ - "@io_bazel_rules_go//go/platform:windows": [ - "@org_golang_x_sys//windows", - ], - "//conditions:default": [], - }), + ], ) go_test( diff --git a/pkg/util/timeutil/now_unix.go b/pkg/util/timeutil/now_unix.go deleted file mode 100644 index 085a0268833..00000000000 --- a/pkg/util/timeutil/now_unix.go +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright 2017 The Cockroach Authors. -// -// Use of this software is governed by the Business Source License -// included in the file licenses/BSL.txt. -// -// As of the Change Date specified in that file, in accordance with -// the Business Source License, use of this software will be governed -// by the Apache License, Version 2.0, included in the file -// licenses/APL.txt. - -//go:build !windows -// +build !windows - -package timeutil - -import "time" - -// Now returns the current UTC time. -func Now() time.Time { - return time.Now().UTC() -} diff --git a/pkg/util/timeutil/now_windows.go b/pkg/util/timeutil/now_windows.go deleted file mode 100644 index 1f1aac378cc..00000000000 --- a/pkg/util/timeutil/now_windows.go +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright 2017 The Cockroach Authors. -// -// Use of this software is governed by the Business Source License -// included in the file licenses/BSL.txt. -// -// As of the Change Date specified in that file, in accordance with -// the Business Source License, use of this software will be governed -// by the Apache License, Version 2.0, included in the file -// licenses/APL.txt. - -package timeutil - -import ( - "time" - - "github.com/cockroachdb/errors" - "golang.org/x/sys/windows" -) - -func init() { - if err := windows.LoadGetSystemTimePreciseAsFileTime(); err != nil { - panic(errors.Wrap(err, "CockroachDB requires Windows 8 or higher")) - } -} - -// Now returns the current UTC time. -// -// This has a higher precision than time.Now in go1.8, but is much slower -// (~2000x) and requires Windows 8+. -// -// TODO(tamird): consider removing this in go1.9. go1.9 is expected to add -// monotonic clock support to values retured from time.Now, which this -// implementation will not support. The monotonic clock support may also -// obviate the need for this, since we only need the higher precision when -// subtracting `time.Time`s. -func Now() time.Time { - var ft windows.Filetime - windows.GetSystemTimePreciseAsFileTime(&ft) - return time.Unix(0, ft.Nanoseconds()).UTC() -} diff --git a/pkg/util/timeutil/time.go b/pkg/util/timeutil/time.go index b94adec1bff..92600768f14 100644 --- a/pkg/util/timeutil/time.go +++ b/pkg/util/timeutil/time.go @@ -18,6 +18,11 @@ import ( // LibPQTimePrefix is the prefix lib/pq prints time-type datatypes with. const LibPQTimePrefix = "0000-01-01" +// Now returns the current UTC time. +func Now() time.Time { + return time.Now().UTC() +} + // Since returns the time elapsed since t. // It is shorthand for Now().Sub(t). func Since(t time.Time) time.Duration {