github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/util/timeutil/now_windows.go (about)

     1  // Copyright 2017 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package timeutil
    12  
    13  import (
    14  	"time"
    15  
    16  	"github.com/cockroachdb/errors"
    17  	"golang.org/x/sys/windows"
    18  )
    19  
    20  func init() {
    21  	if err := windows.LoadGetSystemTimePreciseAsFileTime(); err != nil {
    22  		panic(errors.Wrap(err, "CockroachDB requires Windows 8 or higher"))
    23  	}
    24  }
    25  
    26  // Now returns the current UTC time.
    27  //
    28  // This has a higher precision than time.Now in go1.8, but is much slower
    29  // (~2000x) and requires Windows 8+.
    30  //
    31  // TODO(tamird): consider removing this in go1.9. go1.9 is expected to add
    32  // monotonic clock support to values retured from time.Now, which this
    33  // implementation will not support. The monotonic clock support may also
    34  // obviate the need for this, since we only need the higher precision when
    35  // subtracting `time.Time`s.
    36  func Now() time.Time {
    37  	var ft windows.Filetime
    38  	windows.GetSystemTimePreciseAsFileTime(&ft)
    39  	return time.Unix(0, ft.Nanoseconds()).UTC()
    40  }