Want to take your software engineering career to the next level? Join the mailing list for career tips & advice Click here


Clock is a small library for mocking time in Go.

Subscribe to updates I use clock

Statistics on clock

Number of watchers on Github 70
Number of open issues 1
Main language Go
Open pull requests 1+
Closed pull requests 0+
Last commit over 5 years ago
Repo Created over 5 years ago
Repo Last Updated over 2 years ago
Size 99 KB
Organization / Authorfacebookgo
Page Updated
Do you use clock? Leave a review!
View clock activity
View on github
Fresh, new opensource launches 🚀🚀🚀
Software engineers: It's time to get promoted. Starting NOW! Subscribe to my mailing list and I will equip you with tools, tips and actionable advice to grow in your career.
Evaluating clock for your project? Score Explanation
Commits Score (?)
Issues & PR Score (?)

clock Build Status Coverage Status GoDoc Project status

Clock is a small library for mocking time in Go. It provides an interface around the standard library's time package so that the application can use the realtime clock while tests can use the mock clock.


Realtime Clock

Your application can maintain a Clock variable that will allow realtime and mock clocks to be interchangable. For example, if you had an Application type:

import "github.com/benbjohnson/clock"

type Application struct {
    Clock clock.Clock

You could initialize it to use the realtime clock like this:

var app Application
app.Clock = clock.New()

Then all timers and time-related functionality should be performed from the Clock variable.

Mocking time

In your tests, you will want to use a Mock clock:

import (


func TestApplication_DoSomething(t *testing.T) {
    mock := clock.NewMock()
    app := Application{Clock: mock}

Now that you've initialized your application to use the mock clock, you can adjust the time programmatically. The mock clock always starts from the Unix epoch (midnight, Jan 1, 1970 UTC).

Controlling time

The mock clock provides the same functions that the standard library's time package provides. For example, to find the current time, you use the Now() function:

mock := clock.NewMock()

// Find the current time.
mock.Now().UTC() // 1970-01-01 00:00:00 +0000 UTC

// Move the clock forward.
mock.Add(2 * time.Hour)

// Check the time again. It's 2 hours later!
mock.Now().UTC() // 1970-01-01 02:00:00 +0000 UTC

Timers and Tickers are also controlled by this same mock clock. They will only execute when the clock is moved forward:

mock := clock.NewMock()
count := 0

// Kick off a timer to increment every 1 mock second.
go func() {
    ticker := clock.Ticker(1 * time.Second)
    for {

// Move the clock forward 10 second.
mock.Add(10 * time.Second)

// This prints 10.
clock open pull requests (View All Pulls)
  • correct spelling mistake
clock questions on Stackoverflow (View All Questions)
  • alarm clock like sleep cycle ios swift
  • Timing algorithm: clock() vs time() in C++
  • com.android.internal.R.id.content cannot be resolved to a variable in alarm clock code
  • Pomodoro Clock not Stopping after countdown
  • (Solved) How to make a simple countdown timer in meteor, with timer on server and clock on client
  • OpenGL analog clock doesn't show correctly?
  • Is there a way to ensure ntp synced clock never moves backwards?
  • Come back to app from an other app(example:com back to navigation from an timer out clock)
  • Pomodoro Clock break timer not starting
  • Generating customized clock using Image Button and TextView
  • World Clock in Javascript not getting the hours properly
  • Canvas Clock with transparent background
  • clock pulse generator for a PLC
  • Display digital clock in edit or static text box
  • How to make an interactive clock on MVC and get the user input time?
  • How do you create a TimePicker like the default Android clock app?
  • Why the clock under Windows 7 shows a quick tick for every 6 slow ticks?
  • Is the JobScheduler API over engineering for an alarm clock app?
  • Is there any consistent (monotonic) Clock implementation in Java?
  • Set 24-hour clock format in Jenkins
  • How can I add minutes to a pomodoro clock?
  • How to reset three.js clock?
  • Getting CPU clock time AVAudioEngine
  • How do I make an automatically-updating digital clock in AngularJS?
  • How to convert custom analog clock app to widget?
  • Word Clock - Most efficient method of telling time
  • Windows IoT and DS3231 RTC clock
  • C++ obtaining milliseconds time on Linux -- clock() doesn't seem to work properly
  • I2C slave - clock stretching
  • How to make a countdown clock in Python on Sublime Text 2
clock list of languages used
Other projects in Go