clock

package module
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2022 License: MIT Imports: 1 Imported by: 8

README

Clock

GitHub Releases Build Status codecov Go Report Card GoDevDoc Donate

A library for mocking time in Golang.

Prerequisites

  • Go >= 1.17

Install

go get go.nhat.io/clock

Usage

Real clock

The clock will return time using time package

package mypackage

import "go.nhat.io/clock"

type Application struct {
	clock clock.Clock
}

func (a *Application) Do() {
	ts := a.clock.Now()

	// Other logic.
}

func New(clock clock.Clock) *Application {
	return &Application{
		clock: clock,
	}
}

var app = New(clock.New())
Static clock

The clock will return a fixed timestamp.

package mypackage

import (
	"time"

	"go.nhat.io/clock"
)

type Application struct {
	clock clock.Clock
}

func (a *Application) Do() {
	ts := a.clock.Now()
	// ts is "2020-01-02T03:04:05Z"

	// Other logic.
}

func New() *Application {
	return &Application{
		clock: clock.Fix(time.Date(2020, 1, 2, 3, 4, 5, 0, time.UTC)),
	}
}

Mock

The clock is mocked using stretchr/testify/mock

package mypackage

import (
	"testing"
	"time"

	clockMock "go.nhat.io/clock/mock"
)

func TestApplication(t *testing.T) {
	t.Parallel()

	ts := time.Date(2020, 1, 2, 3, 4, 5, 0, time.UTC)

	clock := clockMock.Mock(func(c *mock.Clock) {
		c.On("Now").Return(ts).Once()
	})(t)

	app := New(clock)
	
	// assertions.
}

cucumber/godog

See godogx/clocksteps

Donation

If this project help you reduce time to develop, you can give me a cup of coffee :)

Paypal donation

paypal

       or scan this

Documentation

Overview

Package clock provides time functionalities.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Clock

type Clock interface {
	// Now returns the current local time.
	Now() time.Time
}

Clock provides time-functionalities.

type StaticClock

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

StaticClock is the clock that has a fixed timestamp.

func Fix

func Fix(timestamp time.Time) StaticClock

Fix creates a fixed clock with a given timestamp.

func (StaticClock) Clock

func (c StaticClock) Clock() Clock

Clock provides clock.Clock.

func (StaticClock) Now

func (c StaticClock) Now() time.Time

Now returns a fixed timestamp.

type TimeClock

type TimeClock struct{}

TimeClock is a clock that uses `time` package as the source.

func New

func New() TimeClock

New creates a live clock that uses `time` package.

func (TimeClock) Clock

func (c TimeClock) Clock() Clock

Clock provides clock.Clock.

func (TimeClock) Now

func (TimeClock) Now() time.Time

Now returns the current local time.

Directories

Path Synopsis
Package mock provides functionalities for mocking clock.Clock.
Package mock provides functionalities for mocking clock.Clock.
Package service provides clock as a service.
Package service provides clock as a service.

Jump to

Keyboard shortcuts

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