timecop

package module
v0.0.0-...-b95363d Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2020 License: MIT Imports: 2 Imported by: 5

README

go-timecop

go-timecop provides "time travel", "time freezing" capabilities for testing. This is inspired by RubyGems timecop.

Getting started

Installation

$ go get -u github.com/bluele/go-timecop

Example

// examples/examples.go
package main

import (
  "fmt"
  "github.com/bluele/go-timecop"
)

func main() {
  t := timecop.Now()
  fmt.Printf("current: %v\n", t)
  fmt.Println("Dive into the future!")
  timecop.Travel(t.AddDate(1, 0, 0))

  for i := 0; i < 3; i++ {
    fmt.Printf("future: %v\n", timecop.Now())
  }

  fmt.Println("Return to the current.")
  timecop.Return()

  fmt.Printf("current: %v\n", timecop.Now())
}
$ go run examples/examples.go
current: 2015-05-25 09:48:16.413680379 +0900 JST
Dive into the future!
future: 2016-05-25 09:48:16.413681008 +0900 JST
future: 2016-05-25 09:48:16.41369027 +0900 JST
future: 2016-05-25 09:48:16.413697228 +0900 JST
Return to the current.
current: 2015-05-25 09:48:16.414099205 +0900 JST

Difference between go-timecop and timecop.

Ruby can be integrated with "time module" using metaprogramming, but the same behavior is not possible using golang.

Therefore, it is necessary to replace the time.Now and time.Since with function timecop.Now and timecop.Since which is provided by this library.

Author

Jun Kimura

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Freeze

func Freeze(t time.Time)

func Now

func Now() time.Time

func Return

func Return()

func Since

func Since(t time.Time) time.Duration

func Travel

func Travel(t time.Time)

func UnUseLock

func UnUseLock()

func UseLock

func UseLock()

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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