ttime

package
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2016 License: Apache-2.0 Imports: 1 Imported by: 0

README

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.

All methods return actual time.Time structs (if they were supposed to).

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())
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// import a ton of constants so we can act like the time library.
	Parse           = time.Parse
	ParseDuration   = time.ParseDuration
	Date            = time.Date
	ParseInLocation = time.ParseInLocation
	FixedZone       = time.FixedZone
	LoadLocation    = time.LoadLocation
	Sunday          = time.Sunday
	Monday          = time.Monday
	Tuesday         = time.Tuesday
	Wednesday       = time.Wednesday
	Thursday        = time.Thursday
	Friday          = time.Friday
	Saturday        = time.Saturday
	ANSIC           = time.ANSIC
	UnixDate        = time.UnixDate
	RubyDate        = time.RubyDate
	RFC822          = time.RFC822
	RFC822Z         = time.RFC822Z
	RFC850          = time.RFC850
	RFC1123         = time.RFC1123
	RFC1123Z        = time.RFC1123Z
	RFC3339         = time.RFC3339
	RFC3339Nano     = time.RFC3339Nano
	Kitchen         = time.Kitchen
	Stamp           = time.Stamp
	StampMilli      = time.StampMilli
	StampMicro      = time.StampMicro
	StampNano       = time.StampNano
	// constants that I really should redefine:
	NewTimer  = time.NewTimer
	NewTicker = time.NewTicker
	Unix      = time.Unix
)

Functions

func After

func After(d time.Duration) <-chan time.Time

func Freeze

func Freeze(t time.Time)

func IsFrozen

func IsFrozen() bool

func Now

func Now() time.Time

func Sleep

func Sleep(d time.Duration)

func Tick

func Tick(d time.Duration) <-chan time.Time

func Unfreeze

func Unfreeze()

Types

type Duration

type Duration time.Duration

type Location

type Location time.Location

type Month

type Month time.Month

type ParseError

type ParseError time.ParseError

type Ticker

type Ticker time.Ticker

type Time

type Time time.Time

type Timer

type Timer time.Timer

type Weekday

type Weekday time.Weekday

Jump to

Keyboard shortcuts

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