clock

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2018 License: Apache-2.0 Imports: 1 Imported by: 1

README

Another Memory Clock

Go Report Card Go Doc Coveralls github Release license

This library contains interface and set of implementations for getting current time. For production usage time must be mostly real time. For testing purposes it's often much easier to use mocked time so it will return required time each time.

Getting Started

Prerequisites

The project is tested with go 1.9 but probably will work with earlier versions.

Installing

Simple go get it

go get github.com/anothermemory/clock
See it in action
Mocked clock for testing
package clock_test

import (
    "time"
    
    "github.com/anothermemory/clock"
)

func ExampleMock() {
	var dummyTime = time.Date(2017, 11, 24, 17, 0, 0, 0, time.Local)
	c := clock.NewMock(dummyTime)
	c.Now() // Will return dummyTime each time
}

func ExampleMockPartial() {
	firstTime := time.Date(2017, 11, 24, 17, 0, 0, 0, time.Local)
    secondTime := time.Date(2018, 11, 24, 17, 0, 0, 0, time.Local)

    c := clock.NewMockPartial(firstTime, secondTime)
    c.Now() // This will return firstTime
    c.Now() // This will return secondTime
    c.Now() // This will return real time
}
Real ID for production
package clock_test

import (
    "time"
    
    "github.com/anothermemory/clock"
)

func ExampleReal() {
    c := clock.NewReal()
    c.Now() // Will return real current time 
}

Built With

  • dep - The dependency management tool for Go

Contributing

Please read CODE_OF_CONDUCT.md for details on our code of conduct, and CONTRIBUTING.md for details on the process for submitting pull requests to us.

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

Authors

  • Vyacheslav Enis

See also the list of contributors who participated in this project.

License

This project is licensed under the Apache License 2.0 - see the LICENSE.md file for details

Documentation

Overview

Package clock contains interface and set of implementations for getting current time. For production usage time must be mostly real time. For testing purposes it's often much easier to use mocked time so it will return required time each time.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Clock

type Clock interface {
	// Now returns current time. Actual returned time depends on clock implementation
	Now() time.Time
}

Clock represents interface which can be used to get current time

func NewMock

func NewMock(t ...time.Time) Clock

NewMock returns Clock which will return passed time each time

Example
package main

import (
	"fmt"
	"time"

	"github.com/anothermemory/clock"
)

func main() {
	c := clock.NewMock(time.Date(2017, 11, 24, 17, 0, 0, 0, time.UTC))
	fmt.Println(c.Now())
	fmt.Println(c.Now())
}
Output:

2017-11-24 17:00:00 +0000 UTC
2017-11-24 17:00:00 +0000 UTC

func NewMockPartial

func NewMockPartial(t ...time.Time) Clock

NewMockPartial returns Clock which will return passed times one by one and then real time each time

Example
package main

import (
	"time"

	"github.com/anothermemory/clock"
)

func main() {
	firstTime := time.Date(2017, 11, 24, 17, 0, 0, 0, time.Local)
	secondTime := time.Date(2018, 11, 24, 17, 0, 0, 0, time.Local)

	c := clock.NewMockPartial(firstTime, secondTime)
	c.Now() // This will return firstTime
	c.Now() // This will return secondTime
	c.Now() // This will return real time
}

func NewReal

func NewReal() Clock

NewReal returns Clock which will return actual real time

Example
package main

import (
	"fmt"

	"github.com/anothermemory/clock"
)

func main() {
	c := clock.NewReal()
	fmt.Println(c.Now())
}

Jump to

Keyboard shortcuts

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