ello-golang-clock

module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2025 License: Apache-2.0

README

Clock

Clock is an interface to provide current time in a way that can easily be mocked.

Usage

package main

import (
	"fmt"
	"github.com/ellogroup/ello-golang-clock/clock"
	"time"
)

func main() {
	// Returns a system clock
	system := clock.NewSystem()
	fmt.Printf("System time: %s\n\n", system.Now().String())

	// Returns a fixed clock
	fixed := clock.NewFixed(time.Date(2030, 1, 2, 12, 34, 56, 0, time.UTC))
	fmt.Printf("Fixed time: %s\n\n", fixed.Now().String())
}
Interface

The Clock interface includes methods related to the current time.

type Clock interface {
	Now() time.Time
	Since(t time.Time) time.Duration
	Until(t time.Time) time.Duration
}
Implementations
System

Returns the system clock, which uses time.Now().

system := clock.NewSystem()
Fixed

Returns a fixed clock, which always returns the specified time.

fixed := clock.NewFixed(time.Date(2030, 1, 2, 12, 34, 56, 0, time.UTC))

Directories

Path Synopsis
example command

Jump to

Keyboard shortcuts

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