Skip to content
This repository has been archived by the owner on Oct 19, 2022. It is now read-only.
/ ttime Public archive
forked from ssoroka/ttime

Golang time library. Kind of like Ruby's Timecop, but in idiomatic Go

License

Notifications You must be signed in to change notification settings

Malwarebytes/ttime

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ttime

Build Status

This is an experiment in making time easier to mock in Go tests.

You should be able to alias the ttime library to time to avoid having to change all your time.Now() methods to ttime.Now() throughout your code.

example code:

import (
  time "github.com/ssoroka/ttime"
)

fmt.Printf("The starting time is %v", time.Now().UTC())

// in test this will not sleep at all, but it will advance the clock 5 seconds.
// in production, it's identical to time.Sleep
time.Sleep(5 * time.Second)
fmt.Printf("The time after sleeping for 5 seconds is %v", time.Now().UTC())

time.After(10 * time.Second, func() {
  // This will execute after 10 seconds in production and immediately in tests.
  fmt.Printf("It is now %v", time.Now().UTC())
})

example test:

func TestFreezingTime(t *testing.T) {
  time.Freeze(time.Now()) // freeze the system clock, at least as far as ttime is concerned.

  // or freeze time at a specific date/time (eg, test leap-year support!):
  now, err := time.Parse(time.RFC3339, "2012-02-29T00:00:00Z")
  if err != nil { panic("date time parse failed") }
  time.Freeze(now)
  defer time.Unfreeze()

  // test leap-year-specific code
  if !isLeapYear(time.Now()) {
    t.Error("Oh no! isLeapYear is broken!")
  }

  t.Logf("It is now %v", time.Now().UTC())
}

About

Golang time library. Kind of like Ruby's Timecop, but in idiomatic Go

Resources

License

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 100.0%