coarsetime

package
v1.20.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 31, 2025 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package coarsetime provides fast, coarse-grained time access. It's a faster alternative to time.Now() with configurable precision.

This package is inspired by Linux's CLOCK_REALTIME_COARSE and CLOCK_MONOTONIC_COARSE, providing both wall clock time and monotonic time with reduced system call overhead.

Examples

```go
// Use default instance (100ms precision)
now := coarsetime.Now()

// Create instance with default precision (100ms)
ct := coarsetime.New()  // Uses default 100ms precision
defer ct.Stop()
now := ct.Now()

// Use custom precision
ct2 := coarsetime.New(10 * time.Millisecond)  // 10ms precision
defer ct2.Stop()
now2 := ct2.Now()

// Use monotonic time for performance measurement
start := coarsetime.Monotonic()
// ... do work ...
elapsed := coarsetime.Since(start)
```

Index

Constants

This section is empty.

Variables

View Source
var (
	// Fast10ms provides 10ms precision (higher precision, more CPU usage)
	// Suitable for applications requiring finer-grained time resolution.
	Fast10ms = New(10 * time.Millisecond)

	// Standard100ms provides 100ms precision (balanced, default)
	// Suitable for most applications requiring coarse-grained time.
	Standard100ms = Default

	// Coarse1s provides 1s precision (lowest precision, least CPU usage)
	// Suitable for applications where time precision is not critical.
	Coarse1s = New(1 * time.Second)
)

Common precision presets for convenience

View Source
var Default = New(100 * time.Millisecond)

Default instance with 100ms precision (balanced performance and accuracy)

Functions

func Ceiling

func Ceiling() time.Time

Ceiling returns the current time rounded up using the default instance.

func CeilingTimeNow

func CeilingTimeNow() time.Time

CeilingTimeNow returns the current time rounded up to the next frequency boundary. This is a legacy function for backward compatibility. For new code, use Ceiling() instead.

func Floor

func Floor() time.Time

Floor returns the current time rounded down using the default instance.

func FloorTimeNow

func FloorTimeNow() time.Time

FloorTimeNow returns the current time rounded down to the nearest frequency boundary. This is a legacy function for backward compatibility. For new code, use Now() or Floor() instead.

func Monotonic

func Monotonic() time.Time

Monotonic returns the current monotonic time using the default instance. This time is not affected by system clock adjustments.

func Now

func Now() time.Time

Now returns the current wall clock time using the default instance. This is faster than time.Now() but less precise (100ms precision).

func Since

func Since(t time.Time) time.Duration

Since returns the time elapsed since t using monotonic time from the default instance.

func Until

func Until(t time.Time) time.Duration

Until returns the duration until t using monotonic time from the default instance.

Types

type CoarseTime

type CoarseTime struct {
	// contains filtered or unexported fields
}

CoarseTime provides fast, coarse-grained time access. It's a faster alternative to time.Now() with configurable precision.

CoarseTime maintains two types of time:

  • Wall Clock Time: Affected by system clock adjustments, suitable for logging and timestamps
  • Monotonic Time: Not affected by system clock adjustments, suitable for performance measurement and timeouts

func New

func New(frequency ...time.Duration) *CoarseTime

New creates a new CoarseTime instance with the specified frequency. If no frequency is provided, it defaults to 100ms (industry standard for coarse-grained time). If frequency <= 0, it also defaults to 100ms.

The frequency determines how often the time is updated. Smaller values provide higher precision but use more CPU. Typical values are 10ms, 100ms, or 1s.

Examples:

  • New() - uses default 100ms precision
  • New(10 * time.Millisecond) - uses 10ms precision

func (*CoarseTime) After

func (ct *CoarseTime) After(t time.Time) bool

After reports whether the time instant ct.Now() is after t.

func (*CoarseTime) Before

func (ct *CoarseTime) Before(t time.Time) bool

Before reports whether the time instant ct.Now() is before t.

func (*CoarseTime) Ceiling

func (ct *CoarseTime) Ceiling() time.Time

Ceiling returns the current time rounded up to the next frequency boundary.

func (*CoarseTime) Floor

func (ct *CoarseTime) Floor() time.Time

Floor returns the current time rounded down to the nearest frequency boundary. This is equivalent to Now().

func (*CoarseTime) Frequency

func (ct *CoarseTime) Frequency() time.Duration

Frequency returns the update frequency of this CoarseTime instance.

func (*CoarseTime) IsStopped

func (ct *CoarseTime) IsStopped() bool

IsStopped returns whether this CoarseTime instance has been stopped.

func (*CoarseTime) Monotonic

func (ct *CoarseTime) Monotonic() time.Time

Monotonic returns the current monotonic time (coarse-grained). This time is not affected by system clock adjustments.

Monotonic time is suitable for:

  • Performance measurements
  • Timeout calculations
  • Duration measurements

Note: Monotonic time should only be used for relative time calculations, not for representing absolute wall clock time.

func (*CoarseTime) Now

func (ct *CoarseTime) Now() time.Time

Now returns the current wall clock time (coarse-grained). This is faster than time.Now() but less precise.

The returned time is affected by system clock adjustments. For time measurements that should not be affected by clock adjustments, use Monotonic() instead.

func (*CoarseTime) Since

func (ct *CoarseTime) Since(t time.Time) time.Duration

Since returns the time elapsed since t using monotonic time. This is more accurate than time.Since() for coarse-grained measurements when system clock adjustments might occur.

func (*CoarseTime) Stop

func (ct *CoarseTime) Stop()

Stop stops the update goroutine and releases resources. After calling Stop, the time values will no longer be updated. It's safe to call Stop multiple times.

func (*CoarseTime) Until

func (ct *CoarseTime) Until(t time.Time) time.Duration

Until returns the duration until t using monotonic time.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL