Skip to content

go-timecop provides "time travel", "time freezing" capabilities for testing.

License

Notifications You must be signed in to change notification settings

bluele/go-timecop

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

go-timecop provides "time travel", "time freezing" capabilities for testing.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages