Skip to main content

time.go

time.go - Overview

Provides interfaces and implementations for managing time, including a real clock and a mock clock for testing purposes.

Detailed Documentation

Clock Interface

type Clock interface {
Now() time.Time
}
  • Purpose: Interface for getting the current time.
  • Methods:
    • Now(): Returns the current time as a time.Time object.
  • Returns: time.Time

RealClock struct

type RealClock struct{}
  • Purpose: Implementation of the Clock interface that returns the real current time.

RealClock.Now()

func (RealClock) Now() time.Time
  • Purpose: Returns the real current time.
  • Returns: The current time as a time.Time object.

MockClock struct

type MockClock struct {
CurrTime time.Time
}
  • Purpose: Implementation of the Clock interface that allows setting and getting a mock current time, useful for testing.
  • Fields:
    • CurrTime: A time.Time object representing the mock current time.

MockClock.Now()

func (mc MockClock) Now() time.Time
  • Purpose: Returns the mock current time.
  • Returns: The mock current time as a time.Time object.

MockClock.SetTime()

func (mc *MockClock) SetTime(t time.Time)
  • Purpose: Sets the mock current time.
  • Parameters:
    • t: A time.Time object representing the new mock current time.
  • Returns: None

MockClock.GetTime()

func (mc *MockClock) GetTime() time.Time
  • Purpose: Gets the mock current time.
  • Returns: The mock current time as a time.Time object.

CurrentTime Variable

var (
CurrentTime Clock = RealClock{}
)
  • Purpose: Global variable of type Clock that defaults to an instance of RealClock. This allows for easy switching between real and mock time.

GetCurrentTime()

func GetCurrentTime() time.Time
  • Purpose: Returns the current time using the global CurrentTime clock.
  • Returns: The current time as a time.Time object.

AddSecondsToUnixEpoch()

func AddSecondsToUnixEpoch(second int64) int64
  • Purpose: Adds a specified number of seconds to the current Unix epoch time.
  • Parameters:
    • second: An int64 representing the number of seconds to add.
  • Returns: An int64 representing the new Unix epoch time.

Getting Started Relevance