datetime

package
v0.0.0-...-04719fa Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2016 License: MIT Imports: 3 Imported by: 2

README

datetime

A collection of cross-project date/time utilities.

TimeProvider

Do not use the go built-in Time.Now(). Instead, inject into your app something that implements interface TimeProvider - for example NowTimeProvider to get and manipulate the current date/time, so you can test aspects of the system whose behaviour changes with the passage of time (e.g. expiring entities).

Documentation

Overview

Package datetime implements a variety of date/time utilities that make it easier to build and test applications which use dates and times.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ITime

type ITime time.Time

ITime implements json.Marshaler interface, and is a replacement for time.Time which is to be used solely in structs which are intended to be passed to json.Marshal(). Ordinarily, Go by default uses the RFC3339 Nano format, which includes floating point second precision. Use this instead when you want to write integer-second UTC timestamps.

func (ITime) MarshalJSON

func (t ITime) MarshalJSON() ([]byte, error)

MarshalJSON implements interface json.Marshaler

type NowTimeProvider

type NowTimeProvider struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

NowTimeProvider implements interface TimeProvider to provide the real value of 'now' (which is always changing), or an artificially-set value (which does not change over time automatically).

func NewNowTimeProvider

func NewNowTimeProvider() *NowTimeProvider

NewNowTimeProvider constructs a new NowTimeProvider initialised to return the current (changing) time

func (*NowTimeProvider) Now

func (p *NowTimeProvider) Now() (t time.Time)

Now implements interface TimeProvider. If SetNow() has not been called, then successive calls to Now() will return the actual real (changing) time. If SetNow() has been called, then the value returned will always remain the same - that passed to SetNow().

func (*NowTimeProvider) SetNow

func (p *NowTimeProvider) SetNow(t time.Time)

SetNow implements interface TimeProvider. If called, then all future calls to Now() will return the value passed.

func (*NowTimeProvider) Since

func (p *NowTimeProvider) Since(t time.Time) time.Duration

Since returns the duration that has elapsed since the given time to now. A negative duration is returned if the given time is in the future.

func (*NowTimeProvider) Until

func (p *NowTimeProvider) Until(t time.Time) time.Duration

Until returns the duration remaining between now and the time specified. If the time given is already in the past, a negative duration is returned.

type TimeProvider

type TimeProvider interface {
	SetNow(t time.Time)

	Now() (t time.Time)

	Until(t time.Time) time.Duration

	Since(t time.Time) time.Duration
}

TimeProvider is the interface that wraps methods which allow the setting and getting of 'now' which, for the purposes of testing, might have been set to an explicit value.

Jump to

Keyboard shortcuts

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